How to Find Python Path Information [Tutorial]

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.
How to Find Python Path Information [Tutorial]
  • 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:
  • 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.
How to Find Python Path Information

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.

Although the 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.

  1. Type import os and press Enter.
  2. Type os.environ[‘PYTHONPATH’].split(os.pathsep) in Python Shell and press Enter.
  3. 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.
  4. Since split function cannot be used with the sys.path which is the reason we are using a for loop to get the values of sys.path in the above exampl.
  5. Whereas the os.environ[‘PYTHONPATH’] attribute does include the split() function, with which you can get the list of path present in PYTHONPATH.
  6. 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
    user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
    user_paths = []

That is all if you are running the above script for the first time and then you should know how to run the python script.

Let me know in the comment section if you are able to get the PYTHONPATH information correctly. If not then I can help you get the PYTHONPATH information.

That is all for the post in which you learned how to get Python Path Information in two ways. If you liked our post let us know in the comment section. Please follow us on Facebook and Twitter.

Leave a Comment