问题:如何在Windows中添加到PYTHONPATH,以便找到我的模块/软件包?

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

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

错误:没有名为coltrane的模块

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

I tried adding C:\My_Projects\; to my Windows Path variable 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:

Error: No module named coltrane


回答 0

您知道在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;

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

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

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

Just add the path as C:\Python27 (or wherever you installed 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

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;

along with it. Else it will never work.


回答 1

Windows 7 Professional I修改了@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版本,这将更易于管理。

Windows 7 Professional I Modified @mongoose_za’s answer to make it easier to change the python version:

  1. [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
  2. Click [New] under “System Variable”
  3. Variable Name: PY_HOME, Variable Value:C:\path\to\python\version enter image description here
  4. Click [OK]
  5. Locate the “Path” System variable and click [Edit]
  6. Add the following to the existing variable:

    %PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk; enter image description here

  7. Click [OK] to close all of the windows.

As a final sanity check open a command prompt and enter python. You should see

>python [whatever version you are using]

If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.


回答 2

从Windows命令行:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

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

From Windows command line:

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.


回答 3

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

Just append your installation path (ex. C:\Python27\) to the PATH variable in System variables. Then close and open your command line and type ‘python’.


回答 4

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

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

那应该照顾你的问题

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


回答 5

PythonPythonPath添加到Windows环境:

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

      • PY_HOME

        C:\Python27
      • PYTHONPATH

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

      • path

        %PY_HOME%;%PY_HOME%\Scripts\

Adding Python and PythonPath to the Windows environment:

  1. Open Explorer.
  2. Right-click ‘Computer’ in the Navigation Tree Panel on the left.
  3. Select ‘Properties’ at the bottom of the Context Menu.
  4. Select ‘Advanced system settings’
  5. Click ‘Environment Variables…’ in the Advanced Tab
  6. Under ‘System Variables’:

    1. Add

      • PY_HOME

        C:\Python27
        
      • PYTHONPATH

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

      • path

        %PY_HOME%;%PY_HOME%\Scripts\
        

回答 6

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

在此处输入图片说明

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

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

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

enter image description here

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

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


回答 7

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

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

You need to add to your PYTHONPATH variable instead of Windows PATH variable.

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


回答 8

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

有关更多信息,请参见此处

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

See here for more information.


回答 9

import sys
sys.path.append("path/to/Modules")
print sys.path

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

import sys
sys.path.append("path/to/Modules")
print sys.path

This won’t persist over reboots or get translated to other files. It is however great if you don’t want to make a permanent modification to your system.


回答 10

成功执行此操作的最简单方法是再次运行python安装程序(首次安装后),然后:

  1. 选择修改。
  2. 检查所需的可选功能,然后单击下一步。
  3. 到这里,在“高级选项”步骤中,您必须看到一个选项“将Python添加到环境变量”。只需检查该选项,然后单击“安装”即可。 第三步 安装完成后,将添加python环境变量,您可以在任何地方轻松使用python。

The easiest way to do that successfully, is to run the python installer again (after the first installation) and then:

  1. choose Modify.
  2. check the optional features which you want and click Next.
  3. here we go, in “Advanced Options” step you must see an option saying “Add Python to environment variables”. Just check that option and click Install. 3rd step When the installation is completed, python environment variables are added and you can easily use python everywhere.

回答 11

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

;D:\Programming\Python34

关闭并重新打开命令提示符,然后执行“ python”。它将打开python shell。这也解决了我的Sublime 3问题:“ python无法识别为内部或外部命令”

In Python 3.4 on windows it worked when I added it to PATH enviroment variable instead of PYTHONPATH. Like if you have installed Python 3.4 in D:\Programming\Python34 then add this at the end of your PATH environment variable

;D:\Programming\Python34

Close and reopen command prompt and execute ‘python’. It will open the python shell. This also fixed my Sublime 3 issue of ‘python is not recognized as an internal or external command’.


回答 12

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

The python 2.X paths can be set from few of the above instructions. Python 3 by default will be installed in C:\Users\\AppData\Local\Programs\Python\Python35-32\ So this path has to be added to Path variable in windows environment.


回答 13

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

谢谢

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.

thanks


回答 14

Python使用PYTHONPATH环境变量来指定可在Windows上从中导入模块的目录列表。运行时,您可以检查sys.path变量以查看导入内容时将搜索哪些目录。

要从命令提示符设置此变量,请使用:set PYTHONPATH=list;of;paths

要从PowerShell设置此变量,请使用: $env:PYTHONPATH=’list;of;paths’在启动Python之前

建议通过“环境变量”设置全局设置此变量,因为任何版本的Python都可以使用它,而不是您打算使用的版本。在Windows FAQ文档的Python中阅读更多内容。

The PYTHONPATH environment variable is used by Python to specify a list of directories that modules can be imported from on Windows. When running, you can inspect the sys.path variable to see which directories will be searched when you import something.

To set this variable from the Command Prompt, use: set PYTHONPATH=list;of;paths.

To set this variable from PowerShell, use: $env:PYTHONPATH=’list;of;paths’ just before you launch Python.

Setting this variable globally through the Environment Variables settings is not recommended, as it may be used by any version of Python instead of the one that you intend to use. Read more in the Python on Windows FAQ docs.


回答 15

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

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.


回答 16

这个问题需要一个正确的答案:

只需使用site为此工作量身定制的标准包装即可!

这是怎么做的(在同一主题上回答我自己的问题的答案):


  1. 打开Python提示符并输入
>>> import site
>>> site.USER_SITE
'C:\\Users\\ojdo\\AppData\\Roaming\\Python\\Python37\\site-packages'
...
  1. 如果此文件夹尚不存在,请创建它:
...
>>> import os
>>> os.makedirs(site.USER_SITE)
...
  1. 手动或使用类似以下代码sitecustomize.py的内容在此文件夹中创建一个包含的内容的文件FIND_MY_PACKAGES。当然,您必须更改C:\My_Projects到自定义导入位置的正确路径。
...
>>> 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)

下次启动Python时,C:\My_Projects它会出现在您的中sys.path,而无需触摸系统范围的设置。奖励:以上步骤也适用于Linux!

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'
...
  1. Create this folder if it does not exist yet:
...
>>> import os
>>> os.makedirs(site.USER_SITE)
...
  1. Create a file sitecustomize.py 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!


回答 17

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

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

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

This PYTHONPATH variable needs to be set for ArcPY when ArcGIS Desktop is installed.

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

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


回答 18

我按照以下步骤在Windows 10中工作了。

在环境变量下,应仅将其添加到“ 系统变量 ”的PATH下,而不应将其添加到“ 用户变量 ”下。这是一个很大的混乱,如果我们错过了,那就会浪费时间。

另外,只需尝试导航到在计算机中安装Python的路径并将其添加到PATH。这只是工作而无需添加其他任何东西。

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

最重要的是,关闭命令提示符,重新打开,然后再次尝试键入“ python”以查看版本详细信息。在环境变量中设置路径后,需要重新启动命令提示符以查看版本。

重新启动后,在命令提示符下键入python时,您应该能够看到python提示符和以下信息:

在命令提示符下键入python时

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


回答 19

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

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

  2. 转到高级系统设置。

  3. 单击环境变量。

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

  5. 选择它

  6. 点击编辑

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

  8. 单击确定,接受器等。

完成后,输入cmd并从驱动器的任何位置编写python,它应该进入Python程序。

我的PC的示例(我有Python34

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

希望能帮助到你。

波哥大的问候

Maybe a little late, but this is how you add the path to the Windows Environment Variables.

  1. Go to the Environment Variables tab, you do this by pressing Windows key + Pausa inter.

  2. Go to Advanced System Settings.

  3. Click on Environment Variables.

  4. On the lower window search for the ‘Path’ value.

  5. Select it

  6. Click on Edit

  7. In the end of the line add your instalation folder and the route to ‘Scripts’ folder.

  8. Click ok, aceptar etc.

You’re done, enter cmd and write python from any location of your drive, it should enter the Python program.

Example with my pc (I have Python34)

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

Hope it helps.

Greetings from Bogotá


回答 20

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

  1. 打开运行并编写cmd

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

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

在此处输入图片说明

You can set the path variable for easily by command prompt.

  1. Open run and write cmd

  2. In the command window write the following: set path=%path%;C:\python36

  3. press enter.
  4. to check write python and enter. You will see the python version as shown in the picture.

enter image description here


回答 21

虽然这个问题是关于“真正的” Python的,但确实是在网络搜索“ Iron Python PYTHONPATH”中出现的。对于像我一样困惑的Iron Python用户:事实证明Iron Python寻找一个名为的环境变量IRONPYTHONPATH

Linux / Mac / POSIX用户:不要忘记Windows不仅\用作路径分隔符,而且还;用作路径定界符,而不是:

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 :.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。