How to check python path

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.

Читайте также:  Java float to formatted string

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

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

Источник

How to find out the Python library installation path?

For installing third-party Python packages I have used a setup.py script that uses setuptools.setup() to install a bunch of packages. After the installation I can find these packages on one machine under /usr/local/lib/python2.7/dist-packages and on another machine under /usr/lib/python2.7/site-packages. Now I want to write a Python script that finds out where the third-party packages have been installed. How can I do that? 1) sys.prefix=sys.exec_prefix is on both machines «/usr». 2) The python executable is on both machines /usr/bin/python. 3) distutils.sysconfig.get_python_lib() is /usr/lib/python2.7/dist-packages («local» is missing) on one machine and /usr/lib/python2.7/site-packages on the other machine.

5 Answers 5

If the packages have already been installed, you could just import them, and look into their __file__ property:

>>> import mymodule >>> print mymodule.__file__ '/path/to/mymodule.py' 

Interesting trick. But it cannot be used, if the installed packages are different on the different hosts and you don’t know whether a certain module is installed on a certain host.

Nearly found a solution to this problem. I refereed to this question and this question so thanks to answers given there.

Firstly you’ll need to get a list of all installed modules into a list. Use this question to capture the output of the solution to this question.

Now you have a list of all installed python modules. You will need to see the format of list and then format it properly to get individual elements as the names of the modules.

Then you can import the modules from their names as strings as explained here. Then as alejandro already said mymodule.__file__ contains the paths.

This is one solution that should work. Not very elegant but I am just a Python beginner who is better at google search than Python

I found a much easier way to find where modules are. This might be the «elegent» solution that OP was looking for.

From the Python docs about sys module sys.path contains

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

Источник

How to Find Path Information in Python

Book image

In the Python programming language, to use code in a package, Python must be able to locate the package and load it into memory. The location information is stored as paths within Python. Whenever you request that Python import a package, Python looks at all the files in its list of paths to find it. The path information comes from three sources.

Path information sources

  • Environment variables: Python environment variables, such as PYTHONPATH , tell Python where to find modules on disk.

How to find path information

  1. Open the Python Shell. You see the Python Shell window appear.
  2. Type import sys and press Enter.
  3. Type for p in sys.path: print(p) in a new cell and click Run Cell You see a listing of the path information, as shown in the figure below. Your listing may be different from the one shown in the figure, depending on your platform, the version of Python you have installed, and the Python features you have installed.

Finding packages on disk in Python

Another way to find paths

  1. In a new cell, type import os and press Enter.
  2. Type os.environ[‘PYTHONPATH’].split(os.pathsep) and press Enter. When you have a PYTHONPATH environment variable defined, you see a list of paths, as shown in the figure below. However, if you don’t have the environment variable defined, you see an error message instead.

Requesting info about environment variables in Python

When you list the sys.path contents again, you see that the new entry is added to the end of the list. Likewise, when you want to remove an entry you type sys.path.remove(os.getcwd()) and press Enter. The addition is present only during the current session.

About This Article

This article is from the book:

Источник

Оцените статью