python3 -m pip install VS pip3 install - python

I always use pip install (which I think is equivalent to pip3 install since I only have python3 in my env) to install packages. But I recently heard python3 -m pip install is better. Why?

I would advise against ever calling any pip somecommand (or pip3) script directly. Instead it's much safer to call pip's executable module for a specific Python interpreter explicitly, something of the form path/to/pythonX.Y -m pip somecommand.
There are many advantages to this, for example:
It is explicit for which Python interpreter the projects will be pip-installed (Python 2 or 3, inside the virtual environment or not, etc.)
For a virtual environment, one can pip-install (or do other things) without activating it: path/to/venv/bin/python -m pip install SomeProject
Under Windows this is the only way to safely upgrade pip itself path\to\venv\Scripts\python.exe -m pip install --upgrade pip
But yes, if all is perfectly setup, then python3 -m pip install SomeProject and pip3 install SomeProject should do the exact same thing, but there are way too many cases where there is an issue with the setup and things don't work as expected and users get confused (as shown by the many questions about this topic on this platform).
References
Brett Cannon's article "Why you should use python -m pip"
pip's documentation section on "Upgrading pip"
venv's documentation section on "Creating virtual environments": "You don’t specifically need to activate an environment [...]"

It's the same thing.
python3 -m pip install calls pip as a module in python, while pip install calls pip directly.
The only reason to prefer the first is that in order to use the second you need to have set pip in your environmental variables (for Windows). In older versions of python this was not done automatically during installation, rather you had to do this manually. That's why in a lot of guides you might see them using the first syntax for their instructions (because it works always, as long as you have python3 in your environmental variables. For Linux/Mac operating systems there isn't any difference.

Related

Old pip packages still exist after uninstalling python - how do I fix this?

I have just uninstalled some older python versions (3.9x, 3.10x) on windows 10 and reinstalled the latest python 3.10.7. My environment variables all reference the correct location for python, but when I run pip list I get a bunch of old packages when they should no longer be there. Since I uninstalled python with the intention to have a fresh installation, I don't want those old packages, but they are even still importable. If I run pip -v list, I see most of those (except the base packages that come with python) refer to ../appdata/roaming/python/python310/, instead of ..appdata/local/..., where Python is actually installed.
What can I do to clear this up? Sure I could pip uninstall them, but I don't know if this indicates a deeper issue and I am quite confused by it.
You get a ton of old packages because you didn't work with venvs sometimes! You can safely run this command to uninstall them all pip uninstall -y -r <(pip freeze) or either you can use your requirements.txt file to uninstall only specified libraries, as suggested in this thread.
Working with venvs fixes this exact problem. Every project will have its own environment with its own libraries to be installed. It may sound heavier (for example, if you have three projects that need requests, you will have to install requests three times) but you won't have any sort of version incompatibility since you can safely install different versions of the same package!
Always remember to set up the venv either using PyCharm or running these commands once you are in your folder:
python -m venv .
source venv/bin/activate
pip install -r requirements.txt
Either, if you will use Linux/Ubuntu, you can do apt install virtualenv, which also automatically creates a venv using this command:
virtualenv venv
Using venv also helps using a different Python version for each project meanwhile having them all installed! Here is how to change the version.
To exit a venv, just run the command deactivate!
To understand if you're in a venv, you have to check whether (venv) is written before your host in the cmd or in the PyCharm terminal, like so:
NOT IN VENV:
marco#marco-Aspire-A515-51G:~
IN VENV:
(venv) marco#marco-Aspire-A515-51G:~

apt install virtualenv vs apt install python3-virtualenv

This question has many related questions, but none answers what I want to know:
I usually work like this:
install python 3.5+
sudo apt install virtualenv
virtualenv -p /usr/bin/python3.* bla
. bla/bin/activate
pip install & run python
This seems to work perfectly, and I've done so also with python2.7. Is there a difference between using apt install virtualenv and apt install python3-virtualenv?
Package virtualenv is described here: https://packages.debian.org/buster/virtualenv or https://packages.ubuntu.com/bionic/virtualenv. When you look at the files it contains you see that it consists only of a bit of documentation.
But virtualenv has dependencies packages python3 and python3-virtualenv. So if you install virtualenv those packages will also be installed.
Python 2 is not supported by upstream anymore, not even security fixes. So you simply should not use it.
apt install virtualenv shall install python3-virtualenv as well I am pretty sure about that but I think that depends on what appears when you enter python on your terminal it is supposed to be version dependent.
After some searching, I can say that package virtulenv is an overlay over python-virtualenv(Python 2 Virtual Env creator) and python3-virtualenv(Python 3 virtual Env creator). It is essential for command virtualenv to work (the one you type in terminal). Now, this command can internally use either of 2 versions provided by python.
Therefore, if you do sudo apt install virtualenv it automatically installs python3-virtualenv along with it. However, you can install virtualenv with python2-virtualenv as well.
So, difference is that virtualenv package is the one which provides command line options(invokable via terminal) while python3-virtualenv(for python 3) does actual job of creating environment and is replaceable by python-virtualenv(for python 2).

Using pip on Windows installed with both python 2.7 and 3.5

I am using Windows 10. Currently, I have Python 2.7 installed. I would like to install Python 3.5 as well. However, if I have both 2.7 and 3.5 installed, when I run pip, how do I get the direct the package to be installed to the desired Python version?
You will have to use the absolute path of pip.
E.g: if I installed python 3 to C:\python35, I would use:
C:\> python35\Scripts\pip.exe install packagename
Or if you're on linux, use pip3 install packagename
If you don't specify a full path, it will use whichever pip is in your path.
Because usually i change my intepreter to run something(i got 2 diff projects with both 2 and 3), i use these solution:
Add path to the environment as usual (of course)
Rename ur python.exe , in my case i want to run python 3 using command python3 on my cmd. So i renamed my python.exe in python3.x directory with python3. Itll works with python 2 ofc.
Then to use pip in both python, i use this command.
python3 -m pip install 'somepackage'
and to run pip on python2
python -m pip install 'somepackage'
This is may not the best solution out there, but i like this one
** WINDOWS **
ref : https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a
In my case, I have Python 2.7 and Python 3.4, with the Python Launcher for Windows.
This is the output when running this commands:
PS C:\> pip -V
pip 9.0.1 from c:\python27\lib\site-packages (python 2.7)
PS C:\> pip3 -V
pip 9.0.1 from C:\Python34\lib\site-packages (python 3.4)
I'll note that in my Python27\Scripts\ directory, I have pip.exe, pip2.exe and pip2.7.exe.
And in my Python34\Scripts\ directory, I have pip.exe, pip3.exe and pip3.4.exe.
So all of these .exe files help you when you have different versions of Python installed at the same time.
Of course, for this to work, you have to have the respective Scriptsdirectries in your Path system enviroment variable.
The answer from Farhan.K will work. However, I think a more convenient way would be to rename python35\Scripts\pip.exe to python35\Scripts\pip3.exe assuming python 3 is installed in C:\python35.
After renaming, you can use pip3 when installing packages to python v3 and pip when installing packages to python v2. Without the renaming, your computer will use whichever pip is in your path.
I would advise against ever calling any pip script directly (nor pip3, pip2.7.exe, anything like that).
Instead, a surefire way is to always prefer the explicit variant of calling pip's executable module for a specific Python interpreter:
path/to/pythonX.Y -m pip somecommand
path/to/venv/bin/python -m pip somecommand
C:\path\to\venv\Scripts\python.exe -m pip somecommand
There are many advantages to this, for example:
It is explicit for which Python interpreter the projects will be pip-installed (Python 2 or 3, inside the virtual environment or not, etc.)
For a virtual environment, one can pip-install (or do other things) without activating it: path/to/venv/bin/python -m pip install SomeProject
Under Windows this is the only way to safely upgrade pip itself path\to\venv\Scripts\python.exe -m pip install --upgrade pip
But yes, if all is perfectly setup, then python3 -m pip install SomeProject and pip3 install SomeProject should do the exact same thing, but there are way too many cases where there is an issue with the setup and things don't work as expected and users get confused (as shown by the many questions about this topic on this platform).
References
Brett Cannon's article "Why you should use python -m pip"
pip's documentation section on "Upgrading pip"
venv's documentation section on "Creating virtual environments": "You don’t specifically need to activate an environment [...]"
I ran across an issue with running pip with absolute path. This might be related to WinPython's installation routine and the order of installing Python 3.6 first, 2.7 second, or Python 3.6 being in the path.
No matter which pip was called, it was activating the 3.6 one:
λ C:\prog\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\Scripts\pip2.exe --version
pip 9.0.1 from C:\prog\WinPython-64bit-3.6.1.0Zero\python-3.6.1.amd64\lib\site-packages (python 3.6)
What finally did the trick was calling pip as a module of the respective python binary:
λ C:\prog\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\python.exe -m pip --version
pip 9.0.1 from C:\prog\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\lib\site-packages (python 2.7)
Hope that might help someone with similar issues.
I tried many things , then finally
pip3 install --upgrade pip worked for me as i was facing this issue since i had both python3 and python2.7 installed on my system.
mind the pip3 in the beginning and pip in the end.
And yes you do have to run in admin mode the command prompt and make sure if the path is set properly.
1-open command prompt and change direction using the command cd C:\Python35\Scripts
2- write the command pip3 install --upgrade pip
3- close the command prompt and reopen it again to return to the default direction and use the command pip3.exe install package_name to install any package you want

How to override the pip command to Python3.x instead of Python2.7?

I am using OSX and I have pip installed for both Python3.5 and Python2.7. I know I can run the command pip2 to use Python2 and when I use the command pip3 Python3.x will be used.
The problem is that the default of pip is set to Python2.7 and I want it to be Python3.x.
How can I change that?
edit:
No, I am not running a virtual environment yet. If it was a virtual environment I could just run Python3.x and forget all about Python2.7, unfortunately since OSX requires Python2.7 for it's use I can't do that. Hence why I'm asking this.
Thanks for the answer. I however don't want to change what running python does. Instead I would like to change the path that running pip takes. At the moment pip -V shows me pip 8.1.2 from /Library/Python/2.7/site-packages (python 2.7), but I am looking for pip 8.1.2 from /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (python 3.5) I am sure there has to be a way to do this. Any ideas?
Run this:
pip3 install --upgrade --force pip
or even more explicit:
python3 -m pip install --upgrade --force pip
This will install pip for Python 3 and make Python 3 version of pip default.
Validate with:
pip -V
I always just run it via Python itself, this way:
python3 -m pip install some_module
or
python2 -m pip install some_module
The -m calls the __main__.py module of a specified package. Pip supports this.
Can't you alias pip='pip3' in your ~/.bash_profile?
In Terminal, run nano ~/.bash_profile, then add a line to the end that reads alias pip='pip3'. This is safe; it won't affect system processes, only your terminal.
For your projects, you should be using a virtualenv.
You can choose which python will be that of the virtualenv at creation time, by specifying it on the command line:
virtualenv -p python3 env
# then
. env/bin/activate
python # ← will run python3
That python interpreter will be the one used when you run python or pip while the virtualenv is active.
Under the hood, activating the virtualenv will:
modify your PATH environment setting so binaries in env/bin
override those from your system.
modify your PYTHONHOME
environment setting so python modules are loaded from env/lib.
So python, pip and any other package you install with pip will be run from the virtualenv, with the python version you chose and the package versions you installed in the virtualenv.
Other than this, running python without using virtualenv will just run the default python of the system, which you cannot usually change as it would break a lot of system scripts.
It works for me:
As super-user
Uninstall pip
sudo pip uninstall pip
Install pip
sudo python3 -m pip install --upgrade --force pip
Check install path
sudo pip -V
As local-user
Uninstall pip
pip uninstall pip
Install pip
python3 -m pip install --upgrade --force pip
Check install path
pip -V
Although PEP 394 does not specifically mention pip, it does discuss a number of other Python-related commands (including python itself). The short version is that, for reasons of backwards compatibility, the unversioned commands should refer to Python 2.x for the immediate future on most reasonable systems.
Generally, these aliases are implemented as symbolic links, and you can just flip the symlink to point at the version you want (e.g. with ln -f -s $(which pip3) $(which pip) as root). But it may not be a good idea if you have any software that expects to interact with Python 2 (which may be more than you think since a lot of software interacts with Python).
The saner option is to set up a Virtualenv with Python 3. Then, within the Virtualenv, all Python-related commands will refer to 3.x instead of 2.x. This will not break the system, unlike the previous paragraph which could well break things.
Since you have specified in the comments you want syntax like pip install [package] to work, here is a solution:
Install setuptools for Python3: apt-get install python3-setuptools
Now pip for Python3 could be installed by: python3 -m easy_install pip
Now you can use pip with the specific version of Python to
install package for Python 3 by: pip-3.2 install [package]
Why not just repoint the link /bin/python to python3? It seems like the easiest solution. Especially if you want it for all users of your system.

Recommended way to install a Python package with pip and no sudo privileges

I usually just use the command:
pip install --user <package>
but I've seen here that this:
pip install <package> --install-option="--prefix=~"
can also be used to bypass the need for sudo privileges. About this command the site says:
There is also a –user option with pip install, which installs into ~/.local. This is fine for the python module, but it puts the corr2 executable into ~/.local/bin, which is probably not in your path. The above command will instead install corr2 into ~/bin.
So apparently it does not behave the same way as the first command.
Is one way preferred over the other and if so why?
The official Python package installation guide is here:
https://packaging.python.org/en/latest/installing.html
It recommends creating Python virtual environments per project using virtualenv command (or python3.4 -m venv).
This is because if you are working with multiple Python projects they have different dependencies and having per project installation environments is the sane way to deal with this in Python.

Categories

Resources