How to get the python.exe location programmatically? [duplicate] - python

This question already has answers here:
Find full path of the Python interpreter?
(3 answers)
Closed 6 years ago.
Basically I want to get a handle of the python interpreter so I can pass a script file to execute (from an external application).

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

sys.executable is not reliable if working in an embedded python environment. My suggestions is to deduce it from
import os
os.__file__

I think it depends on how you installed python. Note that you can have multiple installs of python, I do on my machine. However, if you install via an msi of a version of python 2.2 or above, I believe it creates a registry key like so:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Python.exe
which gives this value on my machine:
C:\Python25\Python.exe
You just read the registry key to get the location.
However, you can install python via an xcopy like model that you can have in an arbitrary place, and you just have to know where it is installed.

Related

python module not found even though it is installed [duplicate]

This question already has answers here:
PyCharm error: 'No Module' when trying to import own module (python script)
(17 answers)
Closed 1 year ago.
I am using PyCharm and am having trouble importing modules
I set up a venv and imported the dependencies via pip install -r
When I try to run the application and get 'module not found' for all imports.
If I open a terminal in PyCharm and do pip list all the required modules are listed
Further PyCharm can file the modules - I can perform completion when typing the name of the module.
What else can I try?
Sounds like you have more than one python environment on your machine.
So, in my opinion, you need to manage the environments in python IDE.
The same happened to me so this helped me in that.
Do check: https://docs.python.org/3/tutorial/venv.html
You can also seek help from the below article:
https://www.freecodecamp.org/news/manage-multiple-python-versions-and-virtual-environments-venv-pyenv-pyvenv-a29fb00c296f/
I solved this by checking the boxes 'Add content toots to PYTONPATH' and #Add source roots to PYTHONPATH'

How to remove python 2.7 from Mac OS X 10.14.5?

I realise that there is already an answer for the question but its for Mac OS X 10.6.4 and the python he installed was from "python.org". My python 2.7 has come built-in in my Mac so how do I remove it? Is there any way of removing it? I have installed a python3.7 from the website and it is working perfectly but 2.7 is also working. I want to remove 2.7 and make it such that when i type "python" in terminal it runs python3.7 and not 2.7.
You don't have to remove python 2.7. You can simply add the command as an alias (you can also add this in your ~/.bash_profile file):
alias python='python3.7'
Do not remove python 2.7 (default python package), it may damage your operating system.
If you want you can simply use this command (removes the python installed with homebrew):
brew uninstall python
Refer this question if you really thinking of removing python 2.7. Here is another question which will give you more information.
the 2.7 version of python is a bundle that comes along with the MAC Unix operating system. which means maybe you not using it but there are some pre-loaded programs and dependencies which uses python hence you cannot remove it completely. If you want to use python 3 directly from the terminal's command line just use "python3" in place of "python". this will launch python 3.* what ever you have installed.
A Mac has more Pythons than a Malaysian jungle.
A new Mac M1 has version 2.7 in /System/Library/Frameworks/Python.framework/Versions/2.7
If you use Homebrew to install Vim, it uses /opt/homebrew/Cellar/python#3.9, and it warns you if you delete it, Vim won't work.
Meantime, I want to do the introductory tensorflow machine learning course, and it insists on Python 3.8
So in my .bash_profile
alias python='/opt/homebrew/Cellar/python#3.8/3.8.8_1/bin/python3'
alias python3='python'
alias python3.8='python3'
and I deleted all references to Python2.7.
But when I open a new terminal, and run a Python program which does
print(sys.path)
it includes /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
and tensorflow won't work. The Mac is inserting its own Python into what I want.

Where can I find pre-installed packages that comes with Python IDLE?

I'm more specifically wanting to know whether sqlite3 and json comes with python IDLE or do I have to install them separately to use them inside IDLE, If so can anyone link me to those installing procedures of sqlite3 and json on Python IDLE?
I also want to know where I can find the list of other pre-installed packages that comes with basic Python IDLE (i.e. Python 2.7.14) . I am a Beginner and it would be really helpful.
Thank you.
To get a list of your packages, from your terminal, launch python :
python
Then
help("modules")
Another solution, if you want to check if json or sqlite3 are installed, start Python from your terminal :
python
Then import sqlite3 and json:
import json
import sqlite3
You can check theirs version with :
>>> json.__version__
'2.0.9'
>>> sqlite3.version
'2.6.0'
IDLE is part of the CPython standard library and is usually installed when tkinter (and turtle) are. It provides an optional alternate interface for running your code with a particular python binary. When you run code through IDLE, it is run by the python binary that is running IDLE. So the same modules are available whether you run code through the terminal interface or through IDLE. There is no separate installation.

Calling python scripts from inside python

It took me forever to find this solution, so I want others to be able to see it.
I wanted to write a python script to create a virtual env and install modules inside it. Unfortunately, pip does not play nice with subprocess, as detailed here:
https://github.com/pypa/pip/issues/610
My answer is already on that thread, but I wanted to detail it below
Basically, the issue is that pip is still using the python executable that the original python called. To fix this, you need to delete it from the passed in environment variables. Here is the solution:
#!/usr/bin/python3
import os
import subprocess
python_env_var = {"_", "__PYVENV_LAUNCHER__"}
CMD_ENVIRONMENT = {name: value for (name, value) in os.environ.items()
if name not in python_env_var}
subprocess.call('./pip install -r requirements.txt', shell=True,
env=CMD_ENVIRONMENT)
Tested on Mac, ubuntu 14.04 and Windows with python 3
This same problem could easily exist for lots of situations -- I will be deleting this variable from now on, to prevent this kind of behavior when dealing with virtualenv's

What all packages do I have? [duplicate]

This question already has answers here:
Python Interpreter Mode - What are some ways to explore Python's modules and its usage
(3 answers)
Closed 8 years ago.
OS Windows XP SP3
situation
I installed three python exes on my machine.
Python 2.6
Python 2.7
Python EPD ENABLED (for pylab)
problem
I installed wxPython and in the selection I decided to install it to Python in system registry
I don't know to which python this package was installed.
what I tried
I tried writing import wx on all the shells and found that it was installed to EPD python.
bigger issue
I don't want to keep doin this each time I install a package. So is there a command that can be used in the shell or any other way, so that I can know about all the packages installed?
please help me with this issue.
Type help() in the shell. And then in the help prompt type modules to see a complete list of all modules.
You can get a complete list with sys.builtin_module_names and pkgutil.walk_packages():
import pkgutil
import sys
print sys.builtin_module_names + [name for module_loader, name, ispkg in pkgutil.walk_packages()]
The modules subcommand of help() puts a friendlier interface on top of these results.

Categories

Resources