2016-08-14 102 views
0

我看过这个帖子之前问过在stackoverflow,但它是4年前(How to fix the python path),所以我不能确定这是否是正确的解决方案,因为我使用的是较新版本的Python(3.5.2 )。这是我在Python Shell中看到的:KeyError:'PYTHONPATH',我该如何修复PYTHONPATH? (Python 3.5.2)

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> import os 
>>> os.environ['PYTHONPATH'].split(os.pathsep) 
Traceback (most recent call last): 
File "<pyshell#1>", line 1, in <module> 
os.environ['PYTHONPATH'].split(os.pathsep) 
File "C:\Users\John\AppData\Local\Programs\Python\Python35\lib\os.py", line 725, in __getitem__ 
raise KeyError(key) from None 
KeyError: 'PYTHONPATH' 

我想找到pythonpath.So,我该如何解决这个错误?

+1

那么,你的系统环境中的变量? –

+0

http://stackoverflow.com/a/4855685/1005215 –

回答

0

OS环境变量中没有PYTHONPATH变量。因此错误。

它不是由Python安装创建的(atleast在windows中)。你必须创建一个变量。

要检查是否有这样的一个环境变量,如下命令类型:

SET PYTHONPATH 

您还可以创建并使用以下命令将其设置

SETX PYTHONPATH <your desired path> 
+0

好吧,我明白了,谢谢。我可以使用这个而不是创建一个变量吗? import sys,for x in sys.path:print(x) –

+0

是的,你可以做到这一点 –

+0

没有区别,对吧? –

0

您可能要检查sys.path

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.