Reputation: 823
Why is there a difference between 'py' and 'python', when I'm using to pip to install modules through the command:
python -m pip install [Mod]
or
py -m pip install [Mod]
The modules aren't available when I'm using the Python IDLE. Furthermore, when I'm checking the
sys.path
it's different for both 'python' and 'py'. How do I make it so they are both the same, and when installing modules, installing into the same folder where they can both access.
Edit:
I forgot to mention that this on windows. So Anyways, I executed
python -V
and it says the version is "Python 3.6.4:: Anaconda, Inc"
While:
py -V
gives "Python 3.6.5". How much difference is there? and why do they have different paths if they are the same version(3.6)?
Upvotes: 79
Views: 82708
Reputation: 569
In addition to @poke's answer, if you use a virtual environment in a particular project, and activate it via
> venv\Scripts\activate.bat
(or the activate script that best suits your platform), the activation places the local venv\Scripts\
directory at the head of the PATH list, so you can execute
> python ....
and it will run the correct version of python without having to have one version of python always in the PATH list. (Though virtual environments will work nicely both ways.)
Upvotes: 0
Reputation: 21
poke's answer is great, but if you want to use "python" you will either need to type the entire path, or add the installation directory to your system’s PATH variable.
This is the solution if you're on Windows:
This "solved" it in my case.
Upvotes: 2
Reputation: 2571
@poke gave a great answer. I'd just like to add that there's a #!/usr/bin/env python2
comment you can add at the top of Python files to tell it what version of Python to use.
python
command line command ignores the comment. py
parses the comment and uses the correct version.
Personally, I'll be using py
for executing files.
Upvotes: 4
Reputation: 387795
python
is the Python executable of the Python installation which you have selected as a default during the installation. This basically put the path to that version inside the PATH, so that the executable is directly available.
py
is the Python launcher which is a utility that comes with Python installations on Windows. It gets installed into C:\Windows\
so it’s available without requiring PATH modifications. The Python launcher detects what Python versions are installed on your machine and is able to automatically delegate to the right version. By default, it will use the latest Python version that is on your machine. So if you have installed 2.7, 3.5 and 3.6, running py
will launch 3.6. You can also specify a different version by doing e.g. py -3.5
to lauch 3.5, or py -2
to launch the latest Python 2 version on your machine.
You can read more about the launcher in the documentation.
These days, I personally never put Python directly in my PATH. I only use the launcher for everything as that gives me more control over what Python will be launched. If you see that py -m pip install
will not install modules for the Python version you run with IDLE, you should check what versions there are. Every Python installation comes with its own directory where pip modules are installed in. So if you e.g. launch IDLE for Python 3.5, you need to make sure that you also run pip with Python 3.5 (e.g. py -3.5 -m pip install
).
python
is a symlink to the default Python installation on your machine. For many Linux machines, this will only be Python 2. Even distributions that no longer come with Python 2 but only ship Python 3 will not use python
for Python 3 since the general expectations of tools would be that python
is a Python 2. So they may only have a python3
symlink.
py
usually does not exist on Linux, unless you set an alias or symlink yourself. You can check with which python
and which py
to see what these commands actually are.
The Python version you are using is from Anaconda, which is a different Python distribution targeted at data scientists that comes bundled with quite a few things. It uses a different Python version that is separate from the official CPython releases that are available from python.org. I assume that those versions simply won’t be available through the Python launcher by default.
Upvotes: 121