430

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

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

错误:没有名为 coltrane 的模块

4

22 回答 22

459

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

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

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

或者

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

在此处输入图像描述

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

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

C:\Python27;

随之而来。否则它将永远无法工作

于 2011-01-31T20:23:05.130 回答
132

Windows 7 Professional 我修改了@mongoose_za 的答案,以便更轻松地更改 python 版本:

  1. [右键]计算机>属性>高级系统设置>环境变量
  2. 单击“系统变量”下的[新建]
  3. 变量名:PY_HOME,变量值:C:\path\to\python\version 在此处输入图像描述
  4. 点击【确定】
  5. 找到“路径”系统变量并单击[编辑]
  6. 将以下内容添加到现有变量中:

    %PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk; 在此处输入图像描述

  7. 单击 [确定] 关闭所有窗口。

作为最后的健全性检查,打开命令提示符并输入 python。你应该看到

>python [whatever version you are using]

如果需要在版本之间切换,只需要修改 PY_HOME 变量指向正确的目录即可。如果您需要安装多个 python 版本,这更容易管理。

于 2014-01-29T13:55:24.273 回答
104

从 Windows 命令行:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

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

于 2010-09-13T15:14:09.990 回答
63

只需将您的安装路径(例如C:\Python27\)附加到System variables中的PATH变量即可。然后关闭并打开命令行并输入 'python'

于 2013-02-07T14:26:21.983 回答
53

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

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

那应该可以解决您的问题

于 2010-09-13T16:11:38.457 回答
41

在 Windows 环境中添加PythonPythonPath :

  1. 打开资源管理器。
  2. 右键单击左侧导航树面板中的“计算机” 。
  3. 选择上下文菜单底部的“属性” 。
  4. 选择“高级系统设置”
  5. 单击高级选项卡中的“环境变量...”
  6. “系统变量”下:

    1. 添加

      • PY_HOME

        C:\Python27
        
      • PYTHONPATH

        %PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;C:\another-library
        
    2. 附加

      • path

        %PY_HOME%;%PY_HOME%\Scripts\
        
于 2015-09-16T12:51:09.883 回答
19

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

在此处输入图像描述

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

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

于 2013-06-18T18:41:03.830 回答
18

成功的最简单方法是再次运行 python 安装程序(在第一次安装之后),然后:

  1. 选择修改。
  2. 检查您想要的可选功能,然后单击下一步。
  3. 我们开始吧,在“高级选项”步骤中,您必须看到一个选项说“将 Python 添加到环境变量”。只需选中该选项,然后单击安装。 第三步 安装完成后添加python环境变量,随处可以轻松使用python。
于 2019-02-28T20:59:34.753 回答
15

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

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

于 2010-09-13T15:13:00.323 回答
13

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

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

于 2013-02-01T23:31:55.570 回答
9
import sys
sys.path.append("path/to/Modules")
print sys.path

这不会在重新启动后持续存在或被翻译成其他文件。但是,如果您不想对系统进行永久性修改,那就太好了。

于 2017-03-28T14:28:27.583 回答
9
于 2019-07-25T19:56:29.863 回答
9

This question needs a proper answer:

Just use the standard package site, which was made for this job!

and here is how (plagiating my own answer to my own question on the very same topic):


  1. Open a Python prompt and type
>>> import site
>>> site.USER_SITE
'C:\\Users\\ojdo\\AppData\\Roaming\\Python\\Python37\\site-packages'
...

(Alternatively, call python -m site --user-site for the same effect.)

  1. Create this folder if it does not exist yet:
...
>>> import os
>>> os.makedirs(site.USER_SITE)
...

(Or, in Bash, your preferred variant of makedirs -p $(python -m site --user-site).)

  1. Create a file sitecustomize.py (with exactly this filename, or it won't work) in this folder containing the content of FIND_MY_PACKAGES, either manually or using something like the following code. Of course, you have to change C:\My_Projects to the correct path to your custom import location.
...
>>> FIND_MY_PACKAGES = """
import site
site.addsitedir(r'C:\My_Projects')
"""
>>> filename = os.path.join(site.USER_SITE, 'sitecustomize.py')
>>> with open(filename, 'w') as outfile:
...     print(FIND_MY_PACKAGES, file=outfile)

And the next time you start Python, C:\My_Projects is present in your sys.path, without having to touch system-wide settings. Bonus: the above steps work on Linux, too!


Why does this work?

From the documentation of standard library package site:

[Then] an attempt is made to import a module named sitecustomize, which can perform arbitrary site-specific customizations. [...].

So if you create a module named sitecustomize anywhere in PYTHONPATH, package site will execute it at Python startup. And by calling site.addsitedir, the sys.path can be safely extended to your liking.

于 2019-12-19T14:22:26.720 回答
6

在Windows 上的Python 3.4中,当我将它添加到PATH 环境变量而不是 PYTHONPATH 时,它起作用了。就像您在 D:\Programming\Python34 中安装了 Python 3.4 一样,然后将其添加到 PATH 环境变量的末尾

;D:\Programming\Python34

关闭并重新打开命令提示符并执行“python”。它将打开python shell。这也解决了我的 Sublime 3 问题'python is not Recognized as an internal or external command'

于 2015-08-29T20:58:06.463 回答
6

python 2.X 路径可以从上面的几个指令中设置。默认情况下,Python 3 将安装在 C:\Users\\AppData\Local\Programs\Python\Python35-32\ 所以这个路径必须添加到 Windows 环境中的 Path 变量中。

于 2016-06-29T00:43:41.337 回答
5

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

谢谢

于 2013-03-13T07:41:54.673 回答
4

I got it worked in Windows 10 by following below steps.

Under environment variables, you should only add it under PATH of "System Variables" and not under "User Variables". This is a great confusion and eats time if we miss it.

Also, just try to navigate to the path where you got Python installed in your machine and add it to PATH. This just works and no need to add any other thing in my case.I added just below path and it worked.

C:\Users\YourUserName\AppData\Local\Programs\Python\Python37-32

Most important, close command prompt, re-open and then re-try typing "python" to see the version details. You need to restart command prompt to see the version after setting up the path in environment variables.

After restarting, you should be able to see the python prompt and below info when typing python in command prompt:

On typing python in command prompt

于 2019-08-05T08:10:19.360 回答
3

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

于 2013-11-04T09:01:17.663 回答
2

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

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

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

于 2012-11-30T18:14:54.053 回答
1

可能有点晚了,但这是您将路径添加到 Windows 环境变量的方式。

  1. 转到环境变量选项卡,您可以通过按 Windows 键 + Pausa inter 来执行此操作。

  2. 转到高级系统设置。

  3. 单击环境变量。

  4. 在下部窗口中搜索“路径”值。

  5. 选择它

  6. 点击编辑

  7. 在该行的末尾添加您的安装文件夹和“脚本”文件夹的路径。

  8. 单击确定,接受等。

大功告成,从驱动器的任意位置输入 cmd 并编写 python,它应该进入 Python 程序。

以我的电脑为例(我有Python34

EXISTING_LINES;C:\Python34;C:\Python34\Scripts\

希望能帮助到你。

来自波哥大的问候

于 2016-03-28T19:13:07.223 回答
1

您可以通过命令提示符轻松设置路径变量。

  1. 打开运行并写入cmd

  2. 在命令窗口中写入以下内容: set path=%path%;C:\python36

  3. 按回车。
  4. 检查写python并输入。您将看到如图所示的 python 版本。

在此处输入图像描述

于 2019-05-09T16:30:49.523 回答
1

While this question is about the 'real' Python, it did come up in a websearch for 'Iron Python PYTHONPATH'. For Iron Python users as confused as I was: It turns out that Iron Python looks for an environment variable called IRONPYTHONPATH.

Linux/Mac/POSIX users: Don't forget that not only does Windows use \ as path separators, but it also uses ; as path delimiters, not :.

于 2019-10-16T16:45:50.043 回答