如何添加到 Windows 中的 PYTHONPATH,以便它找到我的模块/包?

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

How to add to the PYTHONPATH in Windows, so it finds my modules/packages?

pythonwindowsenvironment-variablespythonpath

提问by darren

I have a directory which hosts all of my Django apps (C:\My_Projects). I want to add this directory to my PYTHONPATHso I can call the apps directly.

我有一个目录,用于托管我所有的 Django 应用程序 ( C:\My_Projects)。我想将此目录添加到我的目录中,PYTHONPATH以便我可以直接调用应用程序。

I tried adding C:\My_Projects\;to my Windows Pathvariable from the Windows GUI (My Computer > Properties > Advanced System Settings > Environment Variables). But it still doesn't read the coltrane module and generates this error:

我尝试从 Windows GUI ( )添加C:\My_Projects\;到我的 WindowsPath变量My Computer > Properties > Advanced System Settings > Environment Variables。但它仍然没有读取 coltrane 模块并生成此错误:

Error: No module named coltrane

错误:没有名为 coltrane 的模块

采纳答案by darren

You know what has worked for me really well on windows.

你知道在 Windows 上什么对我有用。

My Computer > Properties > Advanced System Settings > Environment Variables >

My Computer > Properties > Advanced System Settings > Environment Variables >

Just add the path as C:\Python27 (or wherever you installed python)

只需将路径添加为 C:\Python27 (或安装 python 的任何位置)

OR

或者

Then under system variables I create a new Variable called PythonPath. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

然后在系统变量下我创建了一个名为PythonPath. 在这个变量中我有C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

enter image description here

在此处输入图片说明

This is the best way that has worked for me which I hadn't found in any of the docs offered.

这是对我有用的最好方法,我在提供的任何文档中都没有找到。

EDIT:For those who are not able to get it, Please add

编辑:对于那些无法获得它的人,请添加

C:\Python27;

C:\Python27;

along with it. Else it will never work.

伴随着它。否则它永远不会工作

回答by Tao

You need to add to your PYTHONPATHvariable instead of Windows PATHvariable.

您需要添加到PYTHONPATH变量而不是 Windows PATH变量。

http://docs.python.org/using/windows.html

http://docs.python.org/using/windows.html

回答by Yuval Adam

From Windows command line:

从 Windows 命令行:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

To set the PYTHONPATH permanently, add the line to your autoexec.bat. Alternatively, if you edit the system variable through the System Properties, it will also be changed permanently.

要永久设置 PYTHONPATH,请将该行添加到您的autoexec.bat. 或者,如果您通过系统属性编辑系统变量,它也将被永久更改。

回答by inspectorG4dget

These solutions work, but they work for your code ONLY on your machine. I would add a couple of lines to your code that look like this:

这些解决方案有效,但它们仅适用于您机器上的代码。我会在您的代码中添加几行,如下所示:

import sys
if "C:\My_Python_Lib" not in sys.path:
    sys.path.append("C:\My_Python_Lib")

That should take care of your problems

那应该可以解决您的问题

回答by Dan Randolph

This PYTHONPATHvariable needs to be set for ArcPYwhen ArcGIS Desktop is installed.

安装 ArcGIS Desktop 时PYTHONPATH需要设置此变量ArcPY

PYTHONPATH=C:\arcgis\bin(your ArcGIS home bin)

PYTHONPATH=C:\arcgis\bin(您的 ArcGIS home bin)

For some reason it never was set when I used the installer on a Windows 7 32-bit system.

出于某种原因,当我在 Windows 7 32 位系统上使用安装程序时,它从未设置过。

回答by brentlance

You can also add a .pthfile containing the desired directory in either your c:\PythonX.Xfolder, or your \site-packages folder, which tends to be my preferred method when I'm developing a Python package.

您还可以.pth在您的c:\PythonX.X文件夹或您的文件夹中添加一个包含所需目录的文件\site-packages folder,这在我开发 Python 包时往往是我的首选方法。

See herefor more information.

请参阅此处了解更多信息。

回答by Paulino III

Just appendyour installation path (ex. C:\Python27\) to the PATHvariable in System variables. Then close and open your command line and type 'python'.

您只需将您的安装路径(前C:\ Python27 \)到PATH变量系统变量。然后关闭并打开命令行并输入 'python'

回答by Brajendu Kumar Das

To augment PYTHONPATH, run regedit and navigate to KEY_LOCAL_MACHINE \SOFTWARE\Python\PythonCore and then select the folder for the python version you wish to use. Inside this is a folder labelled PythonPath, with one entry that specifies the paths where the default install stores modules. Right-click on PythonPath and choose to create a new key. You may want to name the key after the project whose module locations it will specify; this way, you can easily compartmentalize and track your path modifications.

要扩充 PYTHONPATH,请运行 regedit 并导航到 KEY_LOCAL_MACHINE \SOFTWARE\Python\PythonCore,然后选择要使用的 Python 版本的文件夹。在这个文件夹中是一个标记为 PythonPath 的文件夹,其中一个条目指定了默认安装存储模块的路径。右键单击 PythonPath 并选择创建一个新密钥。您可能希望以将指定其模块位置的项目命名密钥;这样,您可以轻松划分和跟踪您的路径修改。

thanks

谢谢

回答by Bamara Coulibaly

The easier way to set the path in python is : click start> My Computer >Properties > Advanced System Settings > Environment Variables > second windows >

在python中设置路径的更简单方法是:单击开始>我的电脑>属性>高级系统设置>环境变量>第二个窗口>

enter image description here

在此处输入图片说明

select Path > Edit > and then add ";C:\Python27\;C:\Python27\Scripts\"

选择路径 > 编辑 > 然后添加“;C:\Python27\;C:\Python27\Scripts\”

link :http://docs.python-guide.org/en/latest/starting/install/win/

链接:http: //docs.python-guide.org/en/latest/starting/install/win/

回答by Andreas Bergstr?m

For anyone trying to achieve this with Python 3.3+, the Windows installer now includes an option to add python.exe to the system search path. Read more in the docs.

对于尝试使用 Python 3.3+ 实现此目的的任何人,Windows 安装程序现在包含将 python.exe 添加到系统搜索路径的选项。在文档中阅读更多内容。