Linux find python path

How to get the PYTHONPATH in shell?

sys.path is not PYTHONPATH , sys.path actually consists of multiple things : current dir,PYTHONPATH,standard library, and paths contained in .pth files if any. docs.python.org/2/tutorial/modules.html#the-module-search-path

6 Answers 6

The environment variable PYTHONPATH is actually only added to the list of locations Python searches for modules. You can print out the full list in the terminal like this:

python -c "import sys; print(sys.path)" 

Or if want the output in the UNIX directory list style (separated by : ) you can do this:

python -c "import sys; print(':'.join(x for x in sys.path if x))" 

Which will output something like this:

/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg:/usr/local/lib/ python2.7/dist-packages/stripogram-1.5-py2.7.egg:/home/qiime/lib:/home/debian:/us r/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib /python2.7/lib-old:/usr/lib/python2.7/lib- dynload:/usr/local/lib/python2.7/dist- packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/u sr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0: /usr/lib/pymodules/python2.7

@variable No, the paths in PYTHONPATH is added to the paths in sys.path when the Python interpreter starts. In other words, sys.path will include all the paths in PYTHONPATH , but also additional paths, like the path to the Python standard library and the path to installed packages.

This gives me syntax error (pointing to end of import word — EOL while scanning string literal): python -c ‘import os; print(os.environ[«PYTHONPATH»])’. If I use double quote then it says «name ‘PYTHONPATH’ is not defined»

Just write:

just write which python in your terminal and you will see the python path you are using.

That’s the path to the python executable NOT the PYTHONPATH. PYTHONPATH is where python itself looks for modules to import.

Those of us using Python 3.x should do this:

python -c "import sys; print(sys.path)" 

Python, at startup, loads a bunch of values into sys.path (which is «implemented» via a list of strings), including:

  • various hardcoded places
  • the value of $PYTHONPATH
  • probably some stuff from startup files (I’m not sure if Python has rcfiles )

$PYTHONPATH is only one part of the eventual value of sys.path .

If you’re after the value of sys.path , the best way would be to ask Python (thanks @Codemonkey):

python -c "import sys; print sys.path" 

Python 2.x:
python -c «import sys; print ‘\n’.join(sys.path)»

Python 3.x:
python3 -c «import sys; print(‘\n’.join(sys.path))»

The output will be more readable and clean, like so:

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /Library/Python/2.7/site-packages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC

Источник

How to Find the Python Installation Path on Ubuntu, Debian, or Linux Mint

There comes a time now and again when you might want to know where your Python installation path on your Ubuntu, Debian, or Linux Mint distros is located.

Читайте также:  What exactly is linux

Generally, by default, your Python binary is located at /usr/bin/python but it may not always be a guarantee depending on the version you are using. As you can see from this post you can actually install a different version from the default that comes with your distro.

As with the case with many things on Linux systems, there is more than one way to reliably get the Python installation path on that system.

Getting the Python Installation Path Using PYTHONPATH

You can get the value of PYTHONPATH only if it has been set. This is an environment variable that is available on the system. If it has not been set then the result of running any one of the commands below will not return anything.

If the above commands do not work you can also get the path using the which command as shown below.

$ which python /usr/bin/python

Conclusion

Once you know the path of the default Python installation path for your system you can permanently add it as an environment variable by opening the startup file you use for your default shell. This is usually ~/.profile in Ubuntu.

Open the file in your preferred editor and add the following line at the end of that file.

export PYTHONPATH=/usr/bin/python

You then need to restart your terminal to effect the change. You can also run the above on the command-line if you just need it to last the current session.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Источник

How do I find out my PYTHONPATH using Python?

How do I find out which directories are listed in my system’s PYTHONPATH variable, from within a Python script (or the interactive shell)?

I’m not sure what are you trying to do, but if you want to know which folders are used to search for modules being imported you should not rely on PYTHONPATH . Use sys.path for that.

By simple experiment, I found Vanuan’s answer below (printing sys.path) just prints PYTHONPATH. This works after we alter the value using add_path(new_path) which adds to PYTHONPATH.

The title of this post and the body ask two different questions. sys.path is «A list of strings that specifies the search path for modules» — docs.python.org/2/library/sys.html#sys.path. PYTHONPATH is an environment variable that effects this list. By any reasonable definition sys.path is your «python path».

Читайте также:  Sed linux вывести строку

10 Answers 10

You would probably also want this:

Or as a one liner from the terminal:

python -c "import sys; print('\n'.join(sys.path))" 

Caveat: If you have multiple versions of Python installed you should use a corresponding command python2 or python3 .

Same. Linux aaa.com 2.6.18-4-686-bigmem #1 SMP Wed Jun 6 09:41:07 UTC 2007 i686 GNU/Linux . Debian Sarge

This answer was much more helpful for my situation than the accepted answer. I know what my environment variables is. I needed to know where python decided to point besides my environment variable.

sys.path might include items that aren’t specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os try: user_paths = os.environ['PYTHONPATH'].split(os.pathsep) except KeyError: user_paths = [] 

(or, more generically . split(os.sep) ). Can’t figure out why you’re not getting the love, Mark. Your reply is technically more accurate than Paul D Waite’s own reply to his question .

And that problem with the separator is probably why I wasn’t getting the love. Thanks for setting me straight.

And if receive a KeyError, does it means that PYTHONPATH is not defined in my system? Is that a problem? Thanks

@glarrin, correct — KeyError means that PYTHONPATH is not defined. It won’t be a problem because there’s already default paths set up, see sys.path .

Can’t seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.pathsep as below:

sys.path might include items that aren’t specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os os.environ.get('PYTHONPATH', '').split(os.pathsep) 

For future readers: os.sep returns the directory separator for the operating system, e.g. / . The separator used in the Python path is different, and returned by os.pathsep as shown in the accepted answer.

PYTHONPATH is an environment variable whose value is a list of directories. Once set, it is used by Python to search for imported modules, along with other std. and 3rd-party library directories listed in Python’s «sys.path».

As any other environment variables, you can either export it in shell or in ~/.bashrc, see here. You can query os.environ[‘PYTHONPATH’] for its value in Python as shown below:

$ python3 -c "import os, sys; print(os.environ['PYTHONPATH']); print(sys.path) if 'PYTHONPATH' in sorted(os.environ) else print('PYTHONPATH is not defined')" 
$ export PYTHONPATH=$HOME/Documents/DjangoTutorial/mysite 
/home/Documents/DjangoTutorial/mysite ['', '/home/Documents/DjangoTutorial/mysite', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages'] 
PYTHONPATH is not defined 

To set PYTHONPATH to multiple paths, see here.

Note that one can add or delete a search path via sys.path.insert(), del or remove() at run-time, but NOT through os.environ[]. Example:

>>> os.environ['PYTHONPATH']="$HOME/Documents/DjangoTutorial/mysite" >>> 'PYTHONPATH' in sorted(os.environ) True >>> sys.path // but Not there ['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages'] >>> sys.path.insert(0,os.environ['PYTHONPATH']) >>> sys.path // It's there ['$HOME/Documents/DjangoTutorial/mysite', '', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages'] >>> 

In summary, PYTHONPATH is one way of specifying the Python search path(s) for imported modules in sys.path. You can also apply list operations directly to sys.path without the aid of PYTHONPATH.

Читайте также:  Linux mint установка драйвера intel

Источник

Find full path of the Python interpreter?

How do I find the full path of the currently running Python interpreter from within the currently executing Python script?

3 Answers 3

sys.executable contains full path of the currently running Python interpreter.

import sys print(sys.executable) 

This does not seem to work from scripts with a shebang /usr/bin/env python executed as env -i ./script . In that case it returns the current working directory.

@JohnFreeman: I tried this on a GNU/Linux box w/ GNU coreutils 8.4 (env) and Python 3.4.2. #!/usr/bin/env python3 will return the correct full binary path via sys.executable . Perhaps your OS or Python version behaves slightly differently.

Note that this will not return the name of the Python interpreter if Python is embedded in some application.

I tried this with the shebang for python2 and python3 and it printed the correct executable. I also tried with no shebang and called the script with the python and python3 commands and it printed the correct executable.

@mic_e, this may have been true in 2015, but I just tried it today and it behaves as expected (it returns the absolute file of the executable that embeds Python).

Just noting a different way of questionable usefulness, using os.environ :

import os python_executable_path = os.environ['_'] 
$ python -c "import os; print(os.environ['_'])" /usr/bin/python 

There are a few alternate ways to figure out the currently used python in Linux is:

Similarly On Windows with Cygwin will also result the same.

kuvivek@HOSTNAME ~ $ which python /usr/bin/python kuvivek@HOSTNAME ~ $ whereis python python: /usr/bin/python /usr/bin/python3.4 /usr/lib/python2.7 /usr/lib/python3.4 /usr/include/python2.7 /usr/include/python3.4m /usr/share/man/man1/python.1.gz kuvivek@HOSTNAME ~ $ which python3 /usr/bin/python3 kuvivek@HOSTNAME ~ $ command -v python /usr/bin/python kuvivek@HOSTNAME ~ $ type python python is hashed (/usr/bin/python) 

If you are already in the python shell. Try anyone of these. Note: This is an alternate way. Not the best pythonic way.

>>> import os >>> os.popen('which python').read() '/usr/bin/python\n' >>> >>> os.popen('type python').read() 'python is /usr/bin/python\n' >>> >>> os.popen('command -v python').read() '/usr/bin/python\n' >>> >>> 

If you are not sure of the actual path of the python command and is available in your system, Use the following command.

pi@osboxes:~ $ which python /usr/bin/python pi@osboxes:~ $ readlink -f $(which python) /usr/bin/python2.7 pi@osboxes:~ $ pi@osboxes:~ $ which python3 /usr/bin/python3 pi@osboxes:~ $ pi@osboxes:~ $ readlink -f $(which python3) /usr/bin/python3.7 pi@osboxes:~ $ 

Источник

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