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
pyTony
pyMod
6,330 posts since Apr 2010
Reputation Points: 879
Solved Threads: 989
Skill Endorsements: 27
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.
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
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.
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
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
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
You may need to restart your session. If you want, you can post a part of your .bashrc to see if it's OK.
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
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 ?
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
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.
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
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
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
You can also try
python -c "import polymod"
in the terminal to see if it complains.
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11