简体   繁体   中英

KeyError: 'PYTHONPATH', how can I fix PYTHONPATH? (Python 3.5.2)

I have seen this post asked before in stackoverflow but it was 4 years ago ( How to fix the python path ) so I am not sure wether this is the right solution because I am using a newer version of python (3.5.2). This is what I see in a Python Shell:

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'

I want to find the pythonpath.So, how can I fix this error?

There is no PYTHONPATH variable in your OS Environemental variables. Hence the error.

It is not created by Python installation (atleast in windows). You have to create one variable.

To check if there is one such environmental variable, type below command:

SET PYTHONPATH

You can also create and set it using below command

SETX PYTHONPATH <your desired path>

You may want to check sys.path

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

First of all make sure if you have any environment variable with this name. I also got an error like this. I used PATH instead of PYHONPATH and this worked for me. I am working on Ubuntu btw.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM