Are you not able to run the Python script because of the Python Path issue? In this article, I will teach you how to find Python Path Information and set it on your system so that you can easily access the Python from any location on your computer.
Unless are set your Python Path in your computer, Python will not be able to load all the import libraries you have mentioned in your script unless you have saved that script in the Python Installation folder.
The Path information can be located by Python from three different sources. Those are as follows below.
- Python environment variables: such as PYTHONPATH, inform Python where to look for modules on the hard drive.
- Current directory: You can alter the current Python directory so that your program can find any modules it needs.
- Default directories: Python may still discover its own libraries in the set of default directories that are included as part of its own path information, even if you don’t provide any environment variables and the current directory doesn’t yield any usable modules.
PYTHONPATH is an environment variable whose value is a list of directories.
How to Find Python Path Information
Because the lack of a path can cause your application to fail, knowing the current path information is useful. The steps below will show you how to find Python path information:
- Open your start menu and in search bar search for Python. And Select IDLE (Python 3.9 64-bit) to open Python Shell.
- Once Python Shell Window is open then type import sys and press Enter.
- Now Copy the below code in the shell window and press Enter. Or alternatively if paste is not working you can type the below code and make sure you maintain the indent of the code.
import sys for path in sys.path: print(path)
- Once you run the above code, on the Python Shell window you will get the information related to Python Path.
- Note that the list and path can be different for you depending upon your Python version and Path where you installed the Python.
Alternate Way to Get Python Path Information
If the above tutorial and code don’t work for you then you can try this alternate approach to get Python Path information.
sys.path attribute is reliable, it may not necessarily contain all of the paths that Python can view. If you don’t see the path you require, you may always look for it in another area where Python looks for data.
- Type import os and press Enter.
os.environ[‘PYTHONPATH’].split(os.pathsep)in Python Shell and press Enter.
- If PYTHONPATH is already defined in your system then you will see the list of paths. However, if you don’t have the environment variable defined, you may get a error message.
- Since split function cannot be used with the
sys.pathwhich is the reason we are using a for loop to get the values of sys.path in the above exampl.
- Whereas the os.environ[‘PYTHONPATH’] attribute does include the split() function, with which you can get the list of path present in PYTHONPATH.
- Split() requires a value to search for when splitting a list of elements. The os.pathsep constant (a variable with a single, unchangeable, defined value) specifies the path separator for the current platform, allowing you to use the same code on any Python-enabled platform.
import os try: user_paths = os.environ['PYTHONPATH'].split(os.pathsep) except KeyError: user_paths = 
That all if you are running above script for first time and then you should know how to run the python script.
Let me know in comment section if you are able to get the PYTHONPATH information correctly. If not then I can help you get the PYTHONPATH information.