用Python导入 [英] Importing in Python
问题描述
在Python 2.5中,我通过更改环境变量来导入模块。它有效,但使用site-packages却没有。是否有另一种方法可以在C:\ Python25以外的目录中导入模块?
In Python 2.5, I import modules by changing environment variables. It works, but using site-packages does not. Is there another way to import modules in directories other than C:\Python25 ?
推荐答案
途中是 PYTHONPATH
环境变量。另一个是添加 sys.path的路径
可以直接通过 sys.path.append(路径)
或通过定义 .pth
文件并将其添加到 site.addsitedir(dirWithPths)
。路径文件( .pth
)是简单的文本文件,每行都有一个路径。将读取 dirWithPths
中的每个 .pth
文件。
On way is with PYTHONPATH
environment variable. Other one is to add path to sys.path
either directly by sys.path.append(path)
or by defining .pth
files and add them to with site.addsitedir(dirWithPths)
. Path files (.pth
) are simple text files with a path in each line. Every .pth
file in dirWithPths
will be read.
这篇关于用Python导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!