What is PYTHONPATH?

Asked 2 years ago, Updated 2 years ago, 49 views

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.

python

2022-09-30 20:18

2 Answers

What is PYTHONPATH and how to configure it

From 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 what

sys.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.*.pthreads the 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.

When to Configure

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/).

importIf you are interested in how it works, go to documentation.This blog article may be concise.


2022-09-30 20:18

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.


2022-09-30 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.