I recently started using python, but I found out that there is something called pythonpATH.
Could you tell me how to set this PATH and why?
I look forward to your kind cooperation.
What is PYTHONPATH
and how to configure itFrom Python's Documentation PPython Setup and Usage:
Augment the default search path for module files.The format is the same as the shell'sPATH: one or more directory paths separated by os.pathsep(e.g.colons on Unix or semicons on Windows).Non-existent directories are silently ignored. (omitted) The default search path is installation dependent, but generously begins with prefixExtension (IoveYEON)
In other words, this environment variable sets the default (typically somehow/lib/python (and its version)
) as Python reads the module. The notation is the same as PATH, for example ~/.bashrc
for Linux.
exportPYTHONPATH="/path/to/the/directory/to/add:$PYTHONPATH"
You can configure it by writing it down as shown in . You can see what is configured now in echo$PYTHONPATH
.
sys.path
contains whatsys.path
may contain directories configured with PYTHONPATH
, some of the directories in addition to the default path.'/usr/lib/python 2.7/dist-packages/gtk-2.0'
are in this category.
This is in English, but stackoverflow's This answer is helpful.
reads the *.pth
file automatically loaded when the interpreter starts.Something like version>/dist-packages
seems to be added at this time, and some modules add directories to sys.path
themselves.
PYTHONPATH
specifies the "place you want me to find additional" as above, so I don't think there's much special setting to install the module using apt
or pip
. I want to find my own module (e.g., under ~/Documents/my/Amazing/project/
).
import
If you are interested in how it works, go to documentation.This blog article may be concise.
http://docs.python.jp/2.7/using/cmdline.html#envvar-PYTHONPATH
Path to find the module Python uses in the import
statement.
The code below confirms the default path at startup.
$python
>>import sys
>>print(sys.path)
Setting the environment variable PYTHONPATH adds it to this value.
© 2024 OneMinuteCode. All rights reserved.