How to Set Your Python Path

Python is a free, object-oriented programming language suitable for beginning and advanced programmers. When working with Python, you can set an environmental variable, called "PYTHONPATH" to automatically load Python modules every time you access Python. This saves you time by eliminating the need for you to type a module's complete path every time you call it.. In Mac OS X, Unix and Linux systems, you add the "PYTHONPATH" variable to a shell script. In Windows, you add PYTHONPATH on the Environmental Variables screen in your System Settings.

Man Programming Code On Computers
Man writing code and viewing it on his computer.
credit: AndreyPopov/iStock/Getty Images

Setting PYTHONPATH in Mac OS X, Unix and Linux systems

Step

Open the shell script that runs every time you access your terminal in a text editor.

Step

In Unix and Linux environments, this script is called ",bashrc" or "bash_profile."

Step

In Mac OS X environments, the file is called ".profile."

Step

Type:

Step

PYTHONPATH = "$ {PYTHONPATH} : /path/where/python/package/is/located/ export PYTHONPATH

Step

For example, a typical Linux or Unix path is "/usr/lib/python2.7/site-packages/".

Step

For Mac OS X a typical path is "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages".

Step

Save the file. Changes to your path will take effect when you start a new shell.

Setting PYTHONPATH in Windows

Step

Access "System Settings" from your Control Panel.

Step

Click on the "Advanced" tab.

Step

Click on the "Environmental Variables" button on the bottom of the screen.

Step

Click the "New" button under the "System Variables" section.

Step

Type "PYTHONPATH" in the "Variable" field. Type the path for Python modules in the value field. Click "OK" when you are finished setting the PYTHONPATH environmental variable.