well this is the first time i am trying to make a module, i saved the .py file into a new directory called MyModules and tried to import it in IDLE, but it returned the following error;

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import polymod
ImportError: No module named polymod

i found some advice about using the linux terminal and a command called setenv, but it says it doesn't exist. i know this is a really stupid question but i cant find an answer, sorry and thank you for any help.

If you want to prepare package called MyModules, and the parent directory is in your PATH or PYTHONPATH, then you should create an empty file called __init___.py in the directory and import the module as MyModules.polymod.

If you want to import all your modules from this directory and they are unrelated, you could add the directory to your PYTHONPATH or PATH. If you add it to your path it is simple to start the script anywhere, notice though that the current directory will then point to the directory you start the script from, not the directory of the script.

The most simple way to import stuff is not to separate the module in subdirectory, but have a directory say 'polynoms' or 'pythonprograms', which contain both the main file of the program and the modules. Then you simply CD to the directory before starting the program and importing should succeed from current directory also, so it does not need to be added to PATH or PYTHONPATH.

http://docs.python.org/tutorial/modules.html#the-module-search-path

If you want to prepare package called MyModules, and the parent directory is in your PATH or PYTHONPATH, then you should create an empty file called __init___.py in the directory and import the module as MyModules.polymod.

If you want to import all your modules from this directory and they are unrelated, you could add the directory to your PYTHONPATH or PATH. If you add it to your path it is simple to start the script anywhere, notice though that the current directory will then point to the directory you start the script from, not the directory of the script.

The most simple way to import stuff is not to separate the module in subdirectory, but have a directory say 'polynoms' or 'pythonprograms', which contain both the main file of the program and the modules. Then you simply CD to the directory before starting the program and importing should succeed from current directory also, so it does not need to be added to PATH or PYTHONPATH.

http://docs.python.org/tutorial/modules.html#the-module-search-path

how do i add the directory to my PYTHOPATH?
thank you

Add the following line to your file ~/.bashrc

export PYTHONPATH="$HOME/MyModules:$HOME/fooo/bar/baz/qux/someotherdirifyouwant"

then restart a bash shell (or a terminal). You don't need an __init__.py in MyModules because it is not a python package.

Add the following line to your file ~/.bashrc

export PYTHONPATH="$HOME/MyModules:$HOME/fooo/bar/baz/qux/someotherdirifyouwant"

then restart a bash shell (or a terminal). You don't need an __init__.py in MyModules because it is not a python package.

im sorry i dont understand, could you explain, then i will try it again?

im sorry i dont understand, could you explain, then i will try it again?

If you are in linux, your home directory contains a file named '.bashrc' (notice the dot). Type ls ~/.bashrc or ls -a ~ in a terminal to check. Then open this file with your favorite editor and add the line

export PYTHONPATH="/path/to/your/directory/MyModules"

then save the file and exit the editor, and type 'bash' in your terminal to restart the shell. If your python IDE is opened, you need to restart it as well.

If you are in linux, your home directory contains a file named '.bashrc' (notice the dot). Type ls ~/.bashrc or ls -a ~ in a terminal to check. Then open this file with your favorite editor and add the line

export PYTHONPATH="/path/to/your/directory/MyModules"

then save the file and exit the editor, and type 'bash' in your terminal to restart the shell. If your python IDE is opened, you need to restart it as well.

how do i open bashrc? i tried to in the terminal but it said permission denied?!

how do i open bashrc? i tried to in the terminal but it said permission denied?!

if your desktop is gnome, type

cd
gedit .bashrc

in the terminal. If your desktop is KDE, type

cd
kwrite .bashrc

if your desktop is gnome, type

cd
gedit .bashrc

in the terminal. If your desktop is KDE, type

cd
kwrite .bashrc

i did as you said but it still wont load the module in IDLE. im going to have to do the rest tomorrow, its almost 7am. thank you for the help though, much appreciated

You may need to restart your session. If you want, you can post a part of your .bashrc to see if it's OK.

You may need to restart your session. If you want, you can post a part of your .bashrc to see if it's OK.

thanks for your help, when i started up today i created a new directory on my desktop with the name MyModules, worked immediately. i get the feeling there was something more t the problems i was having yesterday, the name of the directory i had My_Modules in last night doesn't come up right in the terminal and wont open when i put in the name as it appears or as it actually is, strange. but now i have a new directory and it works properly, thank you for your time.

seems i was mistaken.. i just restarted python and tried again, didn't work again?! i opened the module file and ran it, then tried import and it worked, but this is obviously not going to be of much use. i dont understand why it wont work.

thanks for your help, when i started up today i created a new directory on my desktop with the name MyModules, worked immediately. i get the feeling there was something more t the problems i was having yesterday, the name of the directory i had My_Modules in last night doesn't come up right in the terminal and wont open when i put in the name as it appears or as it actually is, strange. but now i have a new directory and it works properly, thank you for your time.

seems i was mistaken.. i just restarted python and tried again, didn't work again?! i opened the module file and ran it, then tried import and it worked, but this is obviously not going to be of much use. i dont understand why it wont work.

It's impossible, the PYTHONPATH must work. Can you post your ~/.bashrc ?

It's impossible, the PYTHONPATH must work. Can you post your ~/.bashrc ?

here it is, i added the line to the bottom of the file, saved it, the ran the 'bash' command in the terminal.

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

export PYTHONPATH="/home/wolf/Desktop/MyModules"

If you changed the ~/.bashrc since you logged in, did you start python from the terminal where you ran the bash command after changing .bashrc ?

You can also type 'echo $PYTHONPATH' in the terminal to check that the pythonpath is correct.

If you changed the ~/.bashrc since you logged in, did you start python from the terminal where you ran the bash command after changing .bashrc ?

no i start python using synapse, should i be starting python from the terminal?

the python path is as it should be

wolf@p:~$ echo $PYTHONPATH
/home/wolf/Desktop/MyModules

no i start python using synapse, should i be starting python from the terminal?

the python path is as it should be

wolf@p:~$ echo $PYTHONPATH
/home/wolf/Desktop/MyModules

Normally no, you don't need to start python from the terminal, but if you changed your .bashrc since the last login, it may be different. Does it work in the terminal ?

You can also check this from python to see if PYTHONPATH worked

>>> import os
>>> print os.environ["PYTHONPATH"]

and also this, to see if MyModules is on the path

import sys
print sys.path

Normally no, you don't need to start python from the terminal, but if you changed your .bashrc since the last login, it may be different. Does it work in the terminal ?

You can also check this from python to see if PYTHONPATH worked

>>> import os
>>> print os.environ["PYTHONPATH"]

and also this, to see if MyModules is on the path

import sys
print sys.path

i tried the os pythonpath thing, but it returned as follows;

print os.environ["PYTHONPATH"]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print os.environ["PYTHONPATH"]
  File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'PYTHONPATH'

im going to restart my machine and try again.

You can also try

python -c "import polymod"

in the terminal to see if it complains.

Great information! I’ve been looking for something like this for a while now. Thanks!

commented: spam -3

You can also try

python -c "import polymod"

in the terminal to see if it complains.

i ran the command, didn't do anything. after restarting my machine python still doesn't import polymod. according to the terminal the PYTHONPATH is set right, so why wont it work? i dont get it

i ran the command, didn't do anything. after restarting my machine python still doesn't import polymod. according to the terminal the PYTHONPATH is set right, so why wont it work? i dont get it

If the command didn't complain, it means that it worked. Type python in the terminal, and then

>>> import polymod
>>> dir(polymod)

to see if it works. Then you can try

python /usr/lib64/python2.7/idlelib/idle.py

in the terminal and then import polymod (replace the path to wherever your idle.py is).

If the command didn't complain, it means that it worked. Type python in the terminal, and then

>>> import polymod
>>> dir(polymod)

to see if it works. Then you can try

python /usr/lib64/python2.7/idlelib/idle.py

in the terminal and then import polymod (replace the path to wherever your idle.py is).

it worked, but when i tried the

python /usr/lib64/python2.7/idlelib/idle.py

bit, it didn't work.

it worked, but when i tried the

python /usr/lib64/python2.7/idlelib/idle.py

bit, it didn't work.

Does the file exist ? what was the error message ? Try

>>> import idlelib
>>> print idlelib

Does the file exist ? what was the error message ? Try

>>> import idlelib
>>> print idlelib

the file does exist, its in the same place you had

/usr/lib/python2.7/idlelib/idle.py
i checked and its there without a doubt.
the error was as follows;

wolf@p:~$ python
Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import polymod
>>> dir(polymod)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'poly']
>>> python /usr/lib64/python2.7/idlelib/idle.py
  File "<stdin>", line 1
    python /usr/lib64/python2.7/idlelib/idle.py
                              ^
SyntaxError: invalid syntax
>>>

when i tried that it returned as follows;

wolf@p:~$ python
Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import idlelib
>>> print idlelib
<module 'idlelib' from '/usr/lib/python2.7/idlelib/__init__.pyc'>
>>>

The command must be run in the terminal, not in python.

The command must be run in the terminal, not in python.

sorry, my mistake
it returned as follows;

wolf@p:~$ /usr/lib/python2.7/idlelib/idle.py
bash: /usr/lib/python2.7/idlelib/idle.py: Permission denied
wolf@p:~$

sorry, my mistake
it returned as follows;

wolf@p:~$ /usr/lib/python2.7/idlelib/idle.py
bash: /usr/lib/python2.7/idlelib/idle.py: Permission denied
wolf@p:~$

It's

wolf@p:~$ python /usr/lib/python2.7/idlelib/idle.py

It's

wolf@p:~$ python /usr/lib/python2.7/idlelib/idle.py

okay, it opened idle and successfully imported the module. but then i opened python with synapse and it returned an error, is the only way to open python like this?

either way, thank you very much, your help as been much appreciated!

I can't tell you about synapse, because I don't use it (I'm on mandriva most of the time), but you could try this: write an executable file named "myidle" which you store in a place where you store executable files (for example in /usr/local/bin), containing the following

#!/usr/bin/env bash
python /usr/lib/python2.7/idlelib/idle.py "$@"&

Then use synapse to start myidle instead of idle. It should work.

I can't tell you about synapse, because I don't use it (I'm on mandriva most of the time), but you could try this: write an executable file named "myidle" which you store in a place where you store executable files (for example in /usr/local/bin), containing the following

#!/usr/bin/env bash
python /usr/lib/python2.7/idlelib/idle.py "$@"&

Then use synapse to start myidle instead of idle. It should work.

thanks, i will try that, i made an alias so it works pretty quickly as is. i will mark this as solved now, and again, thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.