Permanently adding a file path to sys.path in Python
Asked Answered
P

5

153

I had a file called example_file.py, which I wanted to use from various other files, so I decided to add example_file.py to sys.path and import this file in another file to use the file. To do so, I ran the following in IPython.

import sys
sys.path
sys.path.append('/path/to/the/example_file.py')
print(sys.path)

I could see the path I had just added, and when I tried to import this file from another directory path like this:

import example_file

it worked just fine, but once I came out of IPython, entered it again, and checked the sys.path, I saw that the path which I had added was not present, so how do I add a path to sys.path permanently in Python?

Phinney answered 4/9, 2012 at 6:15 Comment(4)
Make your code a proper package, make it easy_installable and be happy for the rest of your life (instead of tinkering). Apart from that: every documentation tells you CLEARLY that sys.path.append() accepts a directory containing file AND NOT SINGLE PATH TO A FILE.Deckle
possible duplicate of Adding folder to Python's path permanentlyOrmolu
docs.python.org/using/cmdline.html#envvar-PYTHONPATHDeckle
Don't confuse the system path with PYTHONPATH.Liebermann
E
144

There are a few ways. One of the simplest is to create a my-paths.pth file (as described here). This is just a file with the extension .pth that you put into your system site-packages directory. On each line of the file you put one directory name, so you can put a line in there with /path/to/the/ and it will add that directory to the path.

You could also use the PYTHONPATH environment variable, which is like the system PATH variable but contains directories that will be added to sys.path. See the documentation.

Note that no matter what you do, sys.path contains directories not files. You can't "add a file to sys.path". You always add its directory and then you can import the file.

Elfland answered 4/9, 2012 at 6:20 Comment(8)
ok thanks thats fine i have added my directory which consists of file and its working when i imported the file from another location, bu the problem when i came out of ipython and again looed in to check my path, it does n't appear, i dont want to do it each time when i entered the code, i want to add the directory path to sys.path permanatly, how to do thisPhinney
@Kouripm: Your comment suggests you did not actually follow my suggestion, nor the suggestions in the duplicate post linked above.Elfland
The site package was perfect.Fence
fixed my issue with sys.path.append('C:\\python34\\lib\\site-packages') thank you!Edisonedit
For posterity: the .pth file is what the site-module documentation calls a path configuration file. Of note about the path configuration file: no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.Roi
@ZeinabAbbasimazar site.addsitedir("/path/to/directory") adds it but not permanentlyBraise
Thank you, @MinhTran, for that comment! Somehow there's a no-win situation occurring for me. Whatever creates the .pth files writes them as 1 long line. But then something eventually fails installing cuz the line is too long. Suggested solutions are to delete the .pth file, or break onto multiple lines. But, something else keeps messing with my system -> next, new problem (for some inexplicable reason) modules already installed are no longer installed; but couldn't install because ModuleNotFoundError. Need the .pth, but I remade it multi-line. Wasn't working; needed to be one-line.Slime
Pedantically, sys.path can contain paths to files... if they are zip archives; Python can transparently use .py files stored in the archive. Aside from that, there might not be a "system site-packages" directory, especially on Linux; it might be necessary to use a user-level one instead.Eden
L
11

This way worked for me:

adding the path that you like:

export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add

checking: you can run 'export' cmd and check the output or you can check it using this cmd:

python -c "import sys; print(sys.path)"
Lusty answered 1/5, 2020 at 11:32 Comment(1)
Add where? I know the answer, but it seems that many readers might not. If they did know, they probably wouldn't need to ask or seek the answer to this question in the first place, right?Derna
M
1

Another way to approach this is by installing the file as a single module.

Create an installer file as below (named pysetup.py):

import setuptools

module_name = input("Enter module name: ")
setuptools.setup(
    name=module_name,
    py_modules=[module_name],
)

You can then install this installer using itself with python pysetup.py install Then when prompted enter pysetup.

Now to install any file you can type python -m pysetup install then enter the name of the file. You can also replace install with develop to install in development mode and continue editing the file.

Magna answered 19/11, 2021 at 8:56 Comment(0)
C
0

In one windows distribution in the following file: <python_root_installation_directory>/python38._pth

there are the following lines:

python38.zip
.
./lib
./lib/site-packages

# Uncomment to run site.main() automatically
#import site

Thus, with this content there the following yields:

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
>>> import sys
>>> sys.path
['C:\\Program Files\\Applications\\python_3_8_2\\python38.zip', 'C:\\Program Files\\Applications\\python_3_8_2', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib/site-packages']

So After adding this line into the file: ./lib/site-packages/win32ctypes it is present in the path:

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
>>> import sys
>>> sys.path
['C:\\Program Files\\Applications\\python_3_8_2\\python38.zip', 'C:\\Program Files\\Applications\\python_3_8_2', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib/site-packages', 'C:\\Program Files\\Applications\\python_3_8_2\\./lib/site-packages/win32ctypes']

This way, you don't need to have PYTHONPATH variable present on the system and you can still have the functionality. Disadvantage would be that this is installation specific, so if you have 3 different distributions on your system, this will affect only the chosen installation, while PYTHONPATH will affect all of them simultaneously.

Corella answered 2/5, 2021 at 23:37 Comment(0)
I
0

FILE python-3.12.1-embed-win32/python38._pth

python312.zip
.

# Uncomment to run site.main() automatically
import site

uncomment the import site

Insurable answered 25/12, 2023 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.