将文件夹永久添加到 Python 的路径

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

Adding folder to Python's path permanently

python

提问by riwalk

I've written a library in python and I want it to reside in a common location on the file system.

我已经用 python 编写了一个库,我希望它驻留在文件系统上的一个公共位置。

From my script, I just want to do:

从我的脚本中,我只想做:

>>> import mylib

Now I understand that in order to do this, I cando this:

现在我明白为了做到这一点,我可以这样做:

>>> import sys
>>> sys.path.append(r'C:\MyFolder\MySubFolder')
>>> import mylib

But I don't want to have to do that every time.

但我不想每次都这样做。

The question is: How do I add a folder to python's sys.path permanently? I would imagine that it would be an environment variable, but I can't find it.

问题是:如何将文件夹永久添加到 python 的 sys.path 中?我想它会是一个环境变量,但我找不到它。

It seems like it should be easy, but I can't find out how to do it.

看起来应该很容易,但我不知道如何去做。

采纳答案by nmichaels

The PYTHONPATH environment variable will do it.

PYTHONPATH 环境变量会做到这一点。

回答by Uku Loskit

Deducing from the path you provided in your example here's a tutorial for setting the PYTHONPATH variable in Windows: http://docs.python.org/using/windows.html#excursus-setting-environment-variables

从您在示例中提供的路径推断,这里有一个在 Windows 中设置 PYTHONPATH 变量的教程:http: //docs.python.org/using/windows.html#excursus-setting-environment-variables

回答by bobince

Another possibility is to alter the sys.pathin your sitecustomize.py, a script that is loaded as Python startup time. (It can be put anywhere on your existing path, and can do any setup tasks you like; I use it to set up tab completion with readline too.)

另一种可能性是更改sys.path您的sitecustomize.py,一个作为 Python 启动时间加载的脚本。(它可以放在您现有路径上的任何位置,并且可以执行您喜欢的任何设置任务;我也用它来设置带有 readline 的选项卡完成。)

The sitemodule offers a method that takes care of adding to sys.pathwithout duplicates and with .pthfiles:

site模块提供了一种方法来处理sys.path无重复.pth文件和文件的添加:

import site
site.addsitedir(r'C:\MyFolder\MySubFolder')

回答by Julien Gorenflot

I don't know how general it is, but I have a "usercustomize" file lying around which is read when starting my shell. Maybe it's just because I am a newbie for who "environment variable" sounds scary... Anyway, that's how I permanently modify my sys.path

我不知道它有多普遍,但是我有一个“usercustomize”文件,在启动我的 shell 时会读取它。也许只是因为我是一个新手,因为“环境变量”听起来很可怕......无论如何,这就是我永久修改我的 sys.path 的方式

But as said, I don't know how general it is. I have python 2.7.3, installed with python(x,y) on windows 7. And this file is at

但正如所说,我不知道它有多普遍。我有 python 2.7.3,在windows 7上安装了 python(x,y) 。这个文件在

C:>Users>Me>Appdata>Roaming>Python>Python27>sitepackages> (Careful, Appdata is hidden folder)

C:>Users>Me>Appdata>Roaming>Python>Python27>sitepackages>(小心,Appdata是隐藏文件夹)

and the file, as said, is "usercustomize.py" nothing special in that file. In my case, just my 3 imported paths:

如上所述,该文件是“usercustomize.py”,该文件中没有什么特别之处。就我而言,只有我的 3 个导入路径:

import sys
sys.path.append('C:\Users\blablabla\LPlot')
sys.path.append('C:\Users\bliblibli\MTSim')
sys.path.append('C:\Users\blobloblo\XP')

hope it helps too... And if not, don't hit me, I'm 100% newb. Or let's say 99.99%

希望它也有帮助...如果没有,请不要打我,我是 100% 新手。或者让我们说 99.99%