Setting Your PYTHONPATH environment variable (Linux/Unix/OsX)

Environment variables are lovely little shortcuts that your system uses to give many programs the ability to find certain shared resources easily.  In the *nix environments, these variables are set up for you when you log in to your terminal.  In order to customize these, you will need to modify a little shell script that runs every time you log in. In Linux/Unix this script is usually named something like “.bashrc” or “.bash_profile“, and Macs usually use “.profile” (note the preceding period).  These actually have slightly different purposes (one is for when you log in remotely, the other for log ins from the X-windowing system or some such).   The same process works for all file types, and most people rig them so that one always calls the other which means you only have to maintain one file.  If you have a Mac, just use .profile.

Open your file with a text editor.  You should see something like this:

PATH="${PATH}:/path/to/some/cool/package/:/path/to/another/cool/package/"
export PATH

PYTHONPATH="${PYTHONPATH}:/path/to/some/cool/python/package/:/path/to/another/cool/python/package/"
export PYTHONPATH

Along with some other stuff that we won’t go over today.  If you do not have a PYTHONPATH set up yet you can simply copy the one above (lines 4 AND 5) into your file and replace the useless paths that I include for paths to where your python packages live.

Some common places look like this:

# Linux
/usr/lib/python2.x/site-packages/

# Mac
/Library/Frameworks/Python.framework/Versions/2.x/lib/python2.x/site-packages/

You will want to look for where your system stores its python site-packages. You can also add any other folder that has python modules to this list of paths to allow them to be called by any python instance running in your shells.

These changes will not take effect until you start a new shell or reload the config file by running this on the command line:

source ~/.bashrc # or .bash_profile or .profile if you altered those

23 thoughts on “Setting Your PYTHONPATH environment variable (Linux/Unix/OsX)

  1. oliverwolfson

    Your work sounds interesting, from the description. I live in Thailand, so we would be happy if you can make vegetarian mosquito. Could I ask you a couple of questions about setting the PYTHONPATH up? I’m not quite getting it.

    Reply
  2. stan

    Hi I am using python3 on my debian distro; when giving the command $python3 script.py – the application executes, but displays this on the terminal “open /dev/sequencer or /dev/snd/seq: No such file or directory

    please help – thanks!

    Reply
  3. Satish Lakhani

    How can i export list as an environment variable?
    Ex: export item=[‘hello’, ‘world’]

    it should be list or tuple…

    Reply
  4. demian

    the only thing that fail with me is that the owner ship of the folder was set for apache and didn’t let me use the modules, I needed to change the ownership to root. and then it let me access the modules =(

    Reply
  5. demian

    i might be doing something wrong, if i just open the terminal and do python i can import my modules, but if i cd to some other folder, open python and try to load my modules it fail. look like the .bash_profile only lives in my home and i can only launch the modules from there =( any tips? thanks guys.

    Reply
  6. Pingback: Python:“ImportError: No module named” when trying to run Python script – IT Sprite

  7. Pingback: Python:Python Script not running in crontab calling pysaunter – IT Sprite

  8. Pingback: 当试图运行Python脚本时,"ImportError:No module named" | CODE问答

  9. limkokhole

    For future reader, you probably want to append line:

    Defaults env_keep += “PYTHONPATH”

    by using `sudo visudo`, in order to make sudo able to use package installed from PYTHONPATH. Or alternatively run `sudo -E python3 cript.py` to inherit environment.

    Reply
  10. limkokhole

    Alternative to `sudo -H` and `env_keep`, prefix sudo, e.g. `sudo python3 -m pip install ` if want to install globally, but be sure only for trusted and not arbitrary(potential malicious) package.

    Reply
  11. Pingback: “ImportError: No module named” when trying to run Python script - iZZiSwift

  12. Pingback: 尝试运行Python脚本时出现“ ImportError:未命名模块”|Python问答

  13. Pingback: “ImportError: No module named” when trying to run Python script

  14. Pingback: "ImportError: No module named" when trying to run Python script - PhotoLens

  15. Pingback: Fix Python – “ImportError: No module named” when trying to run Python script - Python Fix Issues

  16. Pingback: "ImportError: No module named" when trying to run Python script

  17. Pingback: [Python] "ImportError: No module named" when trying to run Python script - Pixorix

Leave a reply to afrodev Cancel reply