Find full path of the Python interpreter?
sys.executable
contains full path of the currently running Python interpreter.
import sys
print(sys.executable)
which is now documented here
How to get the current Python interpreter path from inside a Python script?
The name of the interpreter is stored in the variable sys.executable
How can I find where Python is installed on Windows?
In your Python interpreter, type the following commands:
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
How to get the python.exe location programmatically?
This works in Linux & Windows:
Python 3.x
>>> import sys
>>> print(sys.executable)
C:\path\to\python.exe
Python 2.x
>>> import sys
>>> print sys.executable
/usr/bin/python
Getting the full path the script is currently running in [Python]
Let me explain to you by explaining the outputs of all the commands you mentioned by running them in Windows.
Nothing special here, just importing some modules. Importing numpy
to test the commands.
In [1]: import os
...: import sys
...: import numpy
This gives the current working directory in which your process/program is executing. (detailed tutorial here)
In [2]: os.getcwd()
Out[2]: 'C:\\Users\\<username>'
This tells which file the module is loaded from which file.
In [3]: numpy.__file__
Out[3]: 'C:\\Users\\<username>\\anaconda3\\lib\\site-packages\\numpy\\__init__.py'
The path of the loaded file for the module, it can be a relative path.
In [4]: os.path.dirname(numpy.__file__)
Out[4]: 'C:\\Users\\Punit Singh\\anaconda3\\lib\\site-packages\\numpy'
The actual path of the loaded file for the module. See the difference from previous in lib
and Lib
. The folder name actually starts from the capital letter.
In [5]: os.path.dirname(os.path.realpath(numpy.__file__))
Out[5]: 'C:\\Users\\Punit Singh\\anaconda3\\Lib\\site-packages\\numpy'
This is the absolute path, you'll see the difference when you'll run this in Linux. It starts with the root folder denoted by \
.
In [6]: os.path.dirname(os.path.abspath(numpy.__file__))
Out[6]: 'C:\\Users\\Punit Singh\\anaconda3\\lib\\site-packages\\numpy'
[7]
and [8]
respectively give the path and absolute path of the file which is currently executing, here sys.argv[0]
returns the file which is currently executing, since currently, I am running ipython, it is ipython.exe shown in [9]
. (detailed tutorial here)
In [7]: os.path.dirname(sys.argv[0])
Out[7]: 'C:\\Users\\<username>\\anaconda3\\Scripts'
In [8]: os.path.abspath(os.path.dirname(sys.argv[0]))
Out[8]: 'C:\\Users\\<username>\\anaconda3\\Scripts'
In [9]: sys.argv[0]
Out[9]: 'C:\\Users\\<username>\\anaconda3\\Scripts\\ipython'
This gives the absolute path of the executable binary for the Python interpreter (documentation here)
In [10]: sys.executable
Out[10]: 'C:\\Users\\Punit Singh\\anaconda3\\python.exe'
Now coming to your requirement, I understand that you need to access files where this ipython.exe
as shown in [9]
is located, so you can use commands mentioned in [7]
or [8]
.
How to issue a command to find out the path of python3?
That command in your terminal should work just fine. I just confirmed this in my linux terminal. Check to make sure you have python3 install and not python2. Also when in doubt try tagging sudo onto the command
Related Topics
How to Filter Only Printable Characters in a File on Bash (Linux) or Python
Multi Platform Portable Python
Running a Bash Script from Python
Executable Python Program with All Dependencies for Linux
Checking Running Python Script Within the Python Script
Detect Face Then Autocrop Pictures
How to Get the Pythonpath in Shell
Serving a Request from Gunicorn
Error Installing Uwsgi in Virtualenv
Creating a Symbolic in Shared Volume of Docker and Accessing It in Host MAChine
How to Install Writable Shared and User Specific Data Files with Setuptools
How to Send Http Requests to Flask Server