# Usage of PYTHONPATH

**URL:** <https://discuss.python.org/t/usage-of-pythonpath/18086>\
**Category:** Python Help\
**Created:** [August 9, 2022, 12:27pm UTC](https://discuss.python.org/t/usage-of-pythonpath/18086 "2022-08-09T12:27:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pejamide](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@Pejamide](https://discuss.python.org/u/Pejamide)\
**Post date:** [August 9, 2022, 12:27pm UTC](https://discuss.python.org/t/usage-of-pythonpath/18086/1 "2022-08-09T12:27:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mholscher](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mholscher/32/15325_2.png) [@Mholscher](https://discuss.python.org/u/Mholscher)\
**Post date:** [August 9, 2022, 12:38pm UTC](https://discuss.python.org/t/usage-of-pythonpath/18086/2 "2022-08-09T12:38:55Z")

</div>

> [@Pejamide](#):
>
> I am the beginner. I am still confused with the use of 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?

> [@Pejamide](#):
>
> I wonder, why they are NOT the same.

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.

---

<div class="post-metadata">

**Author:** ![Pejamide](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@Pejamide](https://discuss.python.org/u/Pejamide)\
**Post date:** [August 9, 2022, 5:40pm UTC](https://discuss.python.org/t/usage-of-pythonpath/18086/3 "2022-08-09T17:40:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [August 9, 2022, 9:19pm UTC](https://discuss.python.org/t/usage-of-pythonpath/18086/4 "2022-08-09T21:19:21Z")

</div>

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:

````nohighlight
```
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.

---

<div class="post-metadata">

**Author:** ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)\
**Post date:** [August 9, 2022, 10:46pm UTC](https://discuss.python.org/t/usage-of-pythonpath/18086/5 "2022-08-09T22:46:03Z")

</div>

> [@Pejamide](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![Pejamide](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@Pejamide](https://discuss.python.org/u/Pejamide)\
**Post date:** [August 10, 2022, 12:42pm UTC](https://discuss.python.org/t/usage-of-pythonpath/18086/6 "2022-08-10T12:42:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [August 10, 2022, 2:01pm UTC](https://discuss.python.org/t/usage-of-pythonpath/18086/7 "2022-08-10T14:01:53Z")

</div>

> [@Pejamide](#):
>
> 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’.

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:

> **[12. Virtual Environments and Packages](https://docs.python.org/3/tutorial/venv.html)**
>
> Introduction: Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the ...

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`

---

<div class="post-metadata">

**Author:** ![PythonCHB](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pythonchb/32/2551_2.png) [@PythonCHB](https://discuss.python.org/u/PythonCHB)\
**Post date:** [August 10, 2022, 4:37pm UTC](https://discuss.python.org/t/usage-of-pythonpath/18086/8 "2022-08-10T16:37:00Z")

</div>

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.
