Usage of PYTHONPATH

I am the beginner. I am still confused with the use of PYTHONPATH. The results of my research are as follows:

  • I create the environement key ‘PYTHONPATH’ with the following values:
    C:\Program Files\Python\DLLs
    C:\Program Files\Python\Lib
    C:\Program Files\Python
    C:\Program Files\Python\Lib\site-packages
    C:\Users\…\AppData\Roaming\Python\Python310\site-packages

  • I created a program to display the list of the function sys.path gives the following:
    [‘D:\Server\Python’, ‘C:\Program Files\Python\python310.zip’, ‘C:\Program Files\Python\DLLs’, ‘C:\Program Files\Python\lib’, ‘C:\Program Files\Python’, ‘C:\Program Files\Python\lib\site-packages’]

I wonder, why they are NOT the same. Help, please tell me, HOW I can tell each program to use the contents of my PYTHONPATH.

Then do not mess with it. In the 7 years I am programming Python now, I never needed to change something on it. Python sets it to sensible values.

Why do you want to change it?

Well, e.g. the interpreter prepends the location of your started script to it. Very handy when you are developing a program which has more than one module.

1 Like

I want to use the PYTHONPATH, because it does work fine in my web system written in Python. However, in each program I have to code the contents of PYTHONPATH in order to copy them to sys.path. It is indeed cumbersome, because the PYHTHONPATH does not work. Without that code my web system does NOT work. Hence, my question HOW to use the PYTHONPATH instead of such a cumbersome code. Or is there another better solution?

It may be easier to help you if you show how exactly you start your program and the complete error messages - copy your starting command from your terminal including the complete error backtrace. Put the text between triple backticks so it does not get mangled by Markdown:

```
Your text from the terminal will be here.
```

I suppose an import command is failing. Tell us in which directory is the module you want to import.

The PYTHONPATH environment variable is not for setting the standard library and site packages directories. Python adds those on its own. By setting them in PYTHONPATH, you’re needlessly creating problems for any other Python installation. The same applies to PYTHONHOME. Use PYTHONPATH to add directories that contain packages that haven’t been installed to the system or user site packages. It’s rarely needed.

The main Python program of my web system is located at the folder ‘D:\Server\Python’ which is called by my XAMMP Appache server.

At first I have installed the XAMMP system at the folder ‘C:\Xampp’, and then installed the Python system at the folder ‘C:\Program Files\Python’. Both systems works very fine without MySQL.
Then the command ‘pip install mysql-connector’ in the ‘Command Prompt’ has been excuted at the folder ‘C:\ Users\…’. The function mysql.connector has apparently installed at the folder ‘C:\Users\…\AppData\Roaming\Python\Python310\site-packages’.

My system environment variable ‘PYTHONPATH’ has manually been created by me with the contents ‘C:\Program Files\Python\DLLs;C:\Program Files\Python\Lib;C:\Program Files\Python;C:\Program Files\Python\Lib\site-packages;C:\Users\…\AppData\Roaming\Python\Python310\site-packages’,
And my system environment variable ‘Path’ has manually been added by me ‘%PYTHONPATH%;’ at the front.

The part of my Python code before the command ‘import mysql.connector’ contains:
#-----------------------------
logging.error(‘-0-’)
logging.error(sys.path)
#----------------------------
which gives the result:
[‘D:\Server\Python’, ‘C:\Program Files\Python\python310.zip’, ‘C:\Program Files\Python\DLLs’, ‘C:\Program Files\Python\lib’, ‘C:\Program Files\Python’, ‘C:\Program Files\Python\lib\site-packages’]

In such a way the function ‘mysql.connector’ cannot be found, because the called path does not contain ‘;C:\Users\…\AppData\Roaming\Python\Python310\site-packages’!

Hence I have recreated the value of sys,path byb the following code:
#--------------------------
xxx =
xxx.append(‘C:\Program Files\Python\DLLs’)
xxx.append(‘C:\Program Files\Python\Lib’)
xxx.append(‘C:\Program Files\Python\Lib\site-packages’)
xxx.append(‘C:\Program Files\Python’)
xxx.append(‘C:\Users\DeVrije\AppData\Roaming\Python\Python310\site-packages’)
sys.path = xxx.copy()
#--------------------------
which gives the result, that all my Mysql queries have been executed successfully.

Hopefully all my above described information will be clear enough for your possible solution HOW and where I can create the correct path.

That is the mistake. You installed the package as a certain user but you run your Python program as a different user or with a different environment. Do not use PYTHONPATH to fix the mistake.


It would be probably best if you install the packages you need into a venv:
https://docs.python.org/3/tutorial/venv.html

A venv is a directory which contains all the packages you use in the context of the venv (isolated from other packages). To execute python programs using a venv you have basically two options:
a) activate the venv first or, then execute the Python programs as usual (but within the venv)
b) call the programs using the python executable inside the venv.

For example if the venv directory is directory/my_venv then the two options are:
a) source directory/my_venv/bin/activate (or a slightly different Windows variant in the doc)
b) directory/my_venv/bin/python your_program.py

I’m going to add a couple notes here. Some are repeats of what’s been already said.

  1. PYTHONPATH is for adding new entries to sys.path, not setting sys.path.

  2. I’m going one farther than PYTHONPATH is rarely needed to: PYTHONPATH is never the right way to solve a problem – it dates back to way back when when there was not-as-good package management, no virtual environments, and the assumption that there was only one Python installation on a system.

So how do you solve your problem?

A) a virtual environment of some sort may be helpful (I actually use conda most of the time – even for production web servers, but any number is isolated environment systems can be helpful (even docker). But if you have only one Python application running on the system (such as in a Docker image), then using the main Python install is fine.

B) The “right” way to get a package or module on sys.path is to make a package out of it and install it – this is what all the web frameworks do, etc. It really is the way to go. Unfortunately, virtually all the documentation out there about packaging assumes you want to distribute it – but making a package is really helpful even if you are never going to distribute it to anyone but yourself. And it’s a light lift if you don’t need all the dependencies and metadata and all that set up right for distribution.

C) If you really want to mess with sys.path, do it directly in your code, though again, I advise against that.

1 Like