How to Set Your Python Path

Man writing code and viewing it on his computer.
Image Credit: AndreyPopov/iStock/Getty Images

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.

Setting PYTHONPATH in Mac OS X, Unix and Linux systems

Step 1

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

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

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

Step 2


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

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

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

Step 3

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

Setting PYTHONPATH in Windows

Step 1

Access "System Settings" from your Control Panel.

Step 2

Click on the "Advanced" tab.

Step 3

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

Step 4

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

Step 5

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.