0

I am new to programming and have been following some tutorials for learning the basics of Python 2.7.9 syntax. I am also using Ubuntu 14.04.2 LTS.

The tutorial is discussing how to create, save, import, and test custom modules for Python in Windows OS. I have been successful at doing so in Ubuntu, but only after adding a temporary directory path in shell for PYTHONPATH to search.

The default sys.path directories are:

['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']`

As you can see, the default paths all fall under the /usr directory.

2 solutions come to mind to fix this problem:

  1. How can I save under the /usr path directory?
  2. How can I permanently add a new path directory?

However, attempting to save the file testmod.py under /usr prompts the I/O Error message:

[Errno 13] Permission denied: 'usr/local/lib/Python-2.7.9/testmod.py'
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Have you read e.g. http://stackoverflow.com/q/3722248/3001761, or considered writing a `setup.py` and installing your module? – jonrsharpe Mar 15 '15 at 20:05

1 Answers1

0

You need sudo to save in /usr but another way is to simply add the path to your modules to ~/.bashrc or ~/.profile:

export PYTHONPATH="$PYTHONPATH:$HOME/my_mods"

where my_mods is in your home directory, .profile gets executed automatically by the DisplayManager during the start-up process desktop session as well as by the login shell when one logs in from the textual console

.bashrc will work when you use the bash shell to start a program.

You will need to logout and login after setting .profile or use source .bashrc if you go with the bashrc option for the settings to take effect.

environment variables

To check your PYTHONPATH use echo $PYTHONPATH:

~$ echo $PYTHONPATH
/home/padraic/mymods/
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321