Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current Python interpreter path from inside a Python script? [duplicate]

I want to run a Python script from a Python script with subprocess, and I wish to do it using the same interpreter for each of them.

I'm using virtualenv, so I'd like to do something like:

subprocess.Popen('%s script.py' % python_bin)

How do I get python_bin?

It should be /usr/bin/python outside a virtualenv, and /path/to/env/bin/python in a virtualenv.

like image 862
e-satis Avatar asked May 08 '11 13:05

e-satis


People also ask

How do I find the current Python interpreter path?

1 Answer. For finding the full path of the Python interpreter you can use sys. executable which contains the full path of the currently running Python interpreter.

How do I select a Python interpreter?

To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Note: If the Python extension doesn't find an interpreter, it issues a warning.

Where is Python path file?

On Windows, this depends on where we installed Python; the default directory is c:\Python32. If we run the Python Shell from the command line, the current working directory starts as the directory we were in when we ran python3.


2 Answers

The name of the interpreter is stored in the variable sys.executable

like image 147
Ignacio Vazquez-Abrams Avatar answered Sep 29 '22 16:09

Ignacio Vazquez-Abrams


I found it by:

>>> import sys           
>>> help(sys)
...

DATA
    __stderr__ = <open file '<stderr>', mode 'w' at 0x110029140>
    __stdin__ = <open file '<stdin>', mode 'r' at 0x110029030>
    __stdout__ = <open file '<stdout>', mode 'w' at 0x1100290b8>
    api_version = 1013
    argv = ['']
    builtin_module_names = ('__builtin__', '__main__', '_ast', '_codecs', ...
    byteorder = 'big'
    copyright = 'Copyright (c) 2001-2009 Python Software Foundati...ematis...
    dont_write_bytecode = False
    exc_value = TypeError('arg is a built-in module',)
    exec_prefix = '/usr/bin/../../opt/freeware'
    executable = '/usr/bin/python_64'
like image 36
nemo Avatar answered Sep 29 '22 16:09

nemo