Permanently add a directory to PYTHONPATH?
You need to add your new directory to the environment variable PYTHONPATH
, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profile
or whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose.
superuser.com
may be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.
Permanently adding a file path to sys.path in Python
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.
adding directory to sys.path /PYTHONPATH
This is working as documented. Any paths specified in PYTHONPATH
are documented as normally coming after the working directory but before the standard interpreter-supplied paths. sys.path.append()
appends to the existing path. See here and here. If you want a particular directory to come first, simply insert it at the head of sys.path:
import sys
sys.path.insert(0,'/path/to/mod_directory')
That said, there are usually better ways to manage imports than either using PYTHONPATH
or manipulating sys.path
directly. See, for example, the answers to this question.
Add folder to PYTHONPATH programmatically
If you only want to change it for the execution of the current script, you can do it simply by assigning (or changing an existing) value in the os.environ
mapping. The code below is complicated a bit by the fact that I made it work even if os.environ[PYTHONPATH]
isn't initially set to anything (as is the case on my own system).
import os
from pathlib import Path
PYTHONPATH = 'PYTHONPATH'
try:
pythonpath = os.environ[PYTHONPATH]
except KeyError:
pythonpath = ''
print('BEFORE:', pythonpath)
folder = Path(__file__).resolve().parent.joinpath('code')
print(f'{folder=}')
pathlist = [str(folder)]
if pythonpath:
pathlist.extend(pythonpath.split(os.pathsep))
print(f'{pathlist=}')
os.environ[PYTHONPATH] = os.pathsep.join(pathlist)
print('AFTER:', os.environ[PYTHONPATH])
Adding folder to Python's path permanently
The PYTHONPATH environment variable will do it.
How to add to the PYTHONPATH in Windows, so it finds my modules/packages?
You know what has worked for me really well on windows.
My Computer > Properties > Advanced System Settings > Environment Variables >
Just add the path as C:\Python27 (or wherever you installed python)
OR
Then under system variables I create a new Variable called PythonPath
. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path
This is the best way that has worked for me which I hadn't found in any of the docs offered.
EDIT: For those who are not able to get it,
Please add
C:\Python27;
along with it. Else it will never work.
Sibling package imports
Seven years after
Since I wrote the answer below, modifying sys.path
is still a quick-and-dirty trick that works well for private scripts, but there has been several improvements
- Installing the package (in a virtualenv or not) will give you what you want, though I would suggest using pip to do it rather than using setuptools directly (and using
setup.cfg
to store the metadata) - Using the
-m
flag and running as a package works too (but will turn out a bit awkward if you want to convert your working directory into an installable package). - For the tests, specifically, pytest is able to find the api package in this situation and takes care of the
sys.path
hacks for you
So it really depends on what you want to do. In your case, though, since it seems that your goal is to make a proper package at some point, installing through pip -e
is probably your best bet, even if it is not perfect yet.
Old answer
As already stated elsewhere, the awful truth is that you have to do ugly hacks to allow imports from siblings modules or parents package from a __main__
module. The issue is detailed in PEP 366. PEP 3122 attempted to handle imports in a more rational way but Guido has rejected it one the account of
The only use case seems to be running scripts that happen
to be living inside a module's directory, which I've always seen as an
antipattern.
(here)
Though, I use this pattern on a regular basis with
# Ugly hack to allow absolute import from the root folder
# whatever its name is. Please forgive the heresy.
if __name__ == "__main__" and __package__ is None:
from sys import path
from os.path import dirname as dir
path.append(dir(path[0]))
__package__ = "examples"
import api
Here path[0]
is your running script's parent folder and dir(path[0])
your top level folder.
I have still not been able to use relative imports with this, though, but it does allow absolute imports from the top level (in your example api
's parent folder).
Add a directory to Python sys.path so that it's included each time I use Python
Simply add this path to your PYTHONPATH environment variable. To do this, go to Control Panel / System / Advanced / Environment variable, and in the "User variables" sections, check if you already have PYTHONPATH. If yes, select it and click "Edit", if not, click "New" to add it.
Paths in PYTHONPATH should be separated with ";".
Related Topics
How to Pad a String With Zeroes
Why Is "Except: Pass" a Bad Programming Practice
Count the Number of Occurrences of a Character in a String
How to Send a "Multipart/Form-Data" With Requests in Python
How to Wait Some Time in Pygame
Difference Between Map, Applymap and Apply Methods in Pandas
Find the Row Indexes of Several Values in a Numpy Array
Apply Multiple Functions to Multiple Groupby Columns
How to Save and Load Cookies Using Python + Selenium Webdriver
How to Have One Colorbar For All Subplots
Make a Dictionary With Duplicate Keys in Python
How to Create a New Column from the Output of Pandas Groupby().Sum()
How to Check If a String Represents an Int, Without Using Try/Except
Pandas Groupby With Delimiter Join