Linux 上的 Python 路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18247333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PYTHONPATH on Linux
提问by user2580401
I'm novice in this, and I have started learning Python, but I have some questions that I'm not be able to understand,
我是这方面的新手,我已经开始学习 Python,但我有一些我无法理解的问题,
- What exactly is the PYTHONPATH (on Ubuntu)? Is it a folder?
- Is Python provided by default on Ubuntu, or does it have to be installed explicitly?
- Where is the folder in which all modules are (I have a lot folders called
python_
)? - If I wish a new module to work when I'm programming (such as pyopengl) where should I go to introduce all the folders I've got in the folder downloaded?
- Coming back from the PYTHONPATH issue, how do I configure the PYTHONPATH in order to start working on my new module?
- PYTHONPATH(在Ubuntu上)到底是什么?是文件夹吗?
- Python 是默认提供在 Ubuntu 上,还是必须显式安装?
- 所有模块所在的文件夹在哪里(我有很多名为 的文件夹
python_
)? - 如果我希望在编程时使用一个新模块(例如 pyopengl),我应该去哪里介绍我在下载的文件夹中获得的所有文件夹?
- 从 PYTHONPATH 问题回来,如何配置 PYTHONPATH 以开始处理我的新模块?
采纳答案by mpenkov
PYTHONPATH
is an environment variable- Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
/usr/lib/python2.7
on Ubuntu- you shouldn't install packages manually. Instead, use pip. When a package isn't in pip, it usually has a setuptoolssetup script which will install the package into the proper location (see point 3).
- if you use pip or setuptools, then you don't need to set
PYTHONPATH
explicitly
PYTHONPATH
是环境变量- 是(见https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
/usr/lib/python2.7
在 Ubuntu 上- 您不应该手动安装软件包。相反,使用pip。当一个包不在 pip 中时,它通常有一个setuptools安装脚本,它将把包安装到正确的位置(见第 3 点)。
- 如果您使用 pip 或 setuptools,则不需要
PYTHONPATH
显式设置
If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.
如果您查看pyopengl的说明,您会发现它们与第 4 点和第 5 点一致。
回答by mgilson
1) PYTHONPATH
is an environment variable which you can set to add additional directories where python will look for modules and packages. e.g.:
1)PYTHONPATH
是一个环境变量,您可以设置它以添加额外的目录,python 将在其中查找模块和包。例如:
# make python look in the foo subdirectory of your home directory for
# modules and packages
export PYTHONPATH=${PYTHONPATH}:${HOME}/foo
Here I use the sh
syntax. For other shells (e.g. csh
,tcsh
), the syntax would be slightly different. To make it permanent, set the variable in your shell's init file (usually ~/.bashrc).
这里我使用sh
语法。对于其他 shell(例如csh
, tcsh
),语法会略有不同。要使其永久化,请在 shell 的 init 文件(通常是 ~/.bashrc)中设置该变量。
2) Ubuntu comes with python already installed. There may be reasons for installing other (independent) python versions, but I've found that to be rarely necessary.
2) Ubuntu 已经安装了 python。可能有安装其他(独立)python 版本的原因,但我发现很少需要这样做。
3) The folder where your modules live is dependent on PYTHONPATH
and where the directories were set up when python was installed. For the most part, the installed stuff you shouldn't care about where it lives -- Python knows where it is and it can find the modules. Sort of like issuing the command ls
-- where does ls
live? /usr/bin
? /bin
? 99% of the time, you don't need to care -- Just use ls
and be happy that it lives somewhere on your PATH
so the shell can find it.
3) 模块所在的文件夹依赖于PYTHONPATH
安装 python 时设置的目录。在大多数情况下,你不应该关心安装的东西在哪里——Python 知道它在哪里并且它可以找到模块。有点像发出命令ls
——ls
住在哪里? /usr/bin
? /bin
? 在 99% 的情况下,您不需要关心——只要使用它ls
并感到高兴,它就在您的某个地方,PATH
以便 shell 可以找到它。
4) I'm not sure I understand the question. 3rd party modules usually come with install instructions. If you follow the instructions, python should be able to find the module and you shouldn't have to care about where it got installed.
4)我不确定我是否理解这个问题。第 3 方模块通常带有安装说明。如果您按照说明进行操作,python 应该能够找到该模块,并且您不必关心它的安装位置。
5) Configure PYTHONPATH
to include the directory where your module resides and python will be able to find your module.
5) 配置PYTHONPATH
包含你的模块所在的目录,python 将能够找到你的模块。
回答by Sardathrion - against SE abuse
PYTHONPATH
is an environment variable those content is added to the sys.path
where Python looks for modules. You can set it to whatever you like.
PYTHONPATH
是一个环境变量,这些内容被添加到sys.path
Python 查找模块的位置。您可以将其设置为您喜欢的任何内容。
However, do notmess with PYTHONPATH
. More often than not, you are doing it wrong and it will only bring you trouble in the long run. For example, virtual environments could do strange things…
然而,这样做不乱用PYTHONPATH
。很多时候,你做错了,从长远来看只会给你带来麻烦。例如,虚拟环境可以做奇怪的事情……
I would suggest you learned how to package a Python module properly, maybe using this easy setup. If you are especially lazy, you could use cookiecutterto do all the hard work for you.
我建议您学习如何正确打包 Python 模块,也许使用这个简单的设置。如果您特别懒惰,可以使用cookiecutter为您完成所有繁重的工作。