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
About these ads

7 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s