Linux 如何永久添加 Python 导入路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10531359/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:15:43  来源:igfitidea点击:

How do I add a Python import path permanently?

pythonlinuximport

提问by corazza

I know that I can add an import path to Python like this:

我知道我可以像这样向 Python 添加导入路径:

import sys

sys.path.append("/path/to/directory/")

But, when I restart Python, this is gone. I'd find it quite annoying if I had to do this all the time, I'd like to do this once and for all and be done with it.

但是,当我重新启动 Python 时,它就消失了。如果我必须一直这样做,我会觉得很烦人,我想一劳永逸地这样做并完成它。

So, how? Where can I find that file? Or do I need to edit something else? I'm using the latest version of Ubuntu.

又怎样?我在哪里可以找到那个文件?或者我需要编辑其他东西吗?我正在使用最新版本的 Ubuntu。

采纳答案by tuxuday

From man python

人蟒蛇

   ~/.pythonrc.py
          User-specific initialization file loaded by the user module; not used by default or by most applications.

ENVIRONMENT VARIABLES

   PYTHONPATH
          Augments the default search path for module files.  The format is the same as the shell's $PATH: one or more directory  pathnames
          separated by colons.  Non-existent directories are silently ignored.  The default search path is installation dependent, but gen-
          erally begins with ${prefix}/lib/python<version> (see PYTHONHOME above).  The default search path is always appended to  $PYTHON-
          PATH.   If  a script argument is given, the directory containing the script is inserted in the path in front of $PYTHONPATH.  The
          search path can be manipulated from within a Python program as the variable sys.path .

回答by dmytro

execute following from the shell:

从 shell 执行以下操作:

echo -e "\nexport PYTHONPATH=$PYTHONPATH:/path/to/directory" >> ~/.bashrc

and restart it

并重新启动它

回答by Fredrik Pihl

You can set a environmental variable called PYTHONPATHto include you directory.

您可以设置一个名为的环境变量PYTHONPATH以包含您的目录。

Read more about it in the docs

文档中阅读更多相关信息

回答by user747508

You can also use a path file.

您也可以使用路径文件。

If you want to add a module called mymodule to your import path, add the file mymodule.pth to the standard directory for 3rd party modules, usually called dist-packages, or site-packages. On Ubuntu you will probably find it somewhere like

如果要将名为 mymodule 的模块添加到导入路径,请将文件 mymodule.pth 添加到 3rd 方模块的标准目录中,通常称为 dist-packages 或 site-packages。在 Ubuntu 上,您可能会在类似的地方找到它

/usr/local/lib/python2.7/dist-packages

The file mymodule.pth should contain a single line, the directory you want to add to the python import path

文件 mymodule.pth 应包含一行,即您要添加到 python 导入路径的目录

<mymodule.pth>
/path/to/directory/containing/mymodule

Any python modules or packages in the directory will now be importable from the interpreter.

目录中的任何 python 模块或包现在都可以从解释器导入。