Is there any difference running package installation for a python project using these two commands?
python -m pip install <package>
pip install <package>
NOTE I'm using venv in my project.
They are nearly equivalent but they might point to different python installations (and versions). python -m pip gives you more explicit control since you can specify python3.8 -m pip and know that you are installing for the Python 3.8 interpreter. This is not explicitly obvious with pip without more investigation. Brett Cannon wrote a nice blog post: https://snarky.ca/why-you-should-use-python-m-pip/
Short answer
They are probably equivalent.
Longer answer
When you run python -m pip, you are referencing a module of Python called pip. The python command will use your installed version of Python (based on your PATH variable) - So the corresponding pip version will be used.
When you run pip install, a pip module is located by searching the PATH variable, and not by using python. This could be a different pip module than in python -m pip, but it usually isn't.
Edit: In the case of running under a virtual environment, the PATH variable should contain the virtual environment path, so both will be the same - the python that is used will use the pip from the same virtual environment.
Edit:
I'm going to close this question as the reason its happening is different from my original assumption, and it's clearer to ask the question anew:
Pip installs packages in the wrong directory with virtualenv
The accepted answer doesn't directly answer the original question, but is a very useful overview.
Based on discussion below the issue is that even after
$ source ~/PycharmProjects/Practice/venv/bin/activate
$ pip install numpy
numpy is installed in /usr/local/lib/python2.7/site-packages
What could the reason for this be?
Original:
Using Python on OS X via Homebrew:
I've been trying much of the day to sort this out but either I get a must supply either home or prefix/exec-prefix -- not both error, or the package I try to install goes into totally the wrong place:
$ pip3 --version
pip 18.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
$ cd venv
$ pip3 install numpy
..... [snip with following error:]
"must supply either home or prefix/exec-prefix -- not both")
Using this hint
$ pip3 install numpy -t .
Then I get a new error,
`Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/.../pip-install-0fvveq3v/package/'
Searching around SO gives various possibilities involving pip install setuptools. but pip install throws the above error or installs in the wrong place. i.e. the solution involves something that's causing the error in the first place.
I tried to use the Python.org installer but it didn't install pip at all. (The custom installer showed the option checked but with size zero).
An introductory overview is available in this nice tutorial. Here is a good summary with more detail. But, if you renamed or moved the virtual env dir after its creation, it could break it. Create a new one from scratch: $ cd ~/PycharmProjects; python3 -mvenv newenv ; Activate: $ source newenv/bin/activate ; Install something: $ pip install colorama (same as pip3 install only if venv activated); Check: ls ~/PycharmProjects/newenv/lib/python3*/site-packages ; Deactivate: $ deactivate
Then you could try this solution for Pycharm: how to associate a virtual environment with a python project in pycharm. PyCharm indeed comes bundled with virtualenv which could have been customized, please look into Pycharm-specific resources: creating virtual environments and installing packages in Pycharm.
If you have installed PyPI's mainstream virtualenv, by default it will create new environments with the python interpreter that virtualenv was installed with. But it's possible to specify an alternate Python Interpreter upon a new env creation: $ virtualenv -p python3.7 newenvname
Regarding the error DistutilsOptionError: must supply either home or prefix - please check this and this for solutions. Homebrew'ed mappings between python and pip are described here. The normal pip install --user is disabled in homebrewed Python, but there are workarounds. MacOS system Python doesn't provide pip, but it can installed, reinstalled or upgraded for any specific python version manually. Original non-brewed installers are also available for all Python versions: https://www.python.org/downloads/mac-osx/
By default there's no pip.conf, but it can be created by hand to customize things. All possible pip.conf locations (per-user, per-venv, and global/system-wide, and how they override each other) are listed here. If someone faces an issue, they could use pip config list command to see their active configuration, or locate pip.conf and find it.
Finally, you may want to ensure you aren't using pip against macOS' system python. Shell commands such as $ brew info python, which pip, which pip3, pip3 -V, which python3 can help you see what you actually use. Since the macOS default $PATH used to be /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin, stock macOS binaries (including python) may take precedence over some homebrew'ed installations (including python). If so, a custom PATH could be exported via the ~/.bashrc if required.
I have installed python 3.7 but still using and showing version of python 2.7 . I want to change it to 3.. I searched it but ı can't did it.
You should not change your syslink python to use python 3 because it is most likely that your system is using that syslink to python 2 for its own tasks and processes, if you change that, you may broke your system.
As Sammy said in a comment, you should use python3 to use that version.
On the pip side, it is probably that your python 3 does not have pip include (it should have it, but I have seen a lot of Python 3 without it). You can check if you have pip doing: python3 -m pip. The -m param is used to execute modules of python installed.
If you do not have pip installed, you can install it following this (which I recommend because always work): https://pip.pypa.io/en/stable/installing/
That is:
Download a script to install pip: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Execute the script in order to install pip (with sudo because you're going to modify your system): sudo python3 get-pip.py
Now you should have pip installed and doing python3 -m pip again should show you the help of pip.
If you have pip already installed and no pip3 command in your system, you can always create an alias to python3 -m pip with the name pip3 and problem solved. Also if you don't know or do not want to create alias or executables in the /usr/bin folder, you can always keep using python3 -m pip.
PS: It is highly recommended to use virtualenvs when developing with python. If you do not know what it is, here it is a link to the docs: https://virtualenv.pypa.io/en/latest/
I'm using Ubuntu, how do I instruct pip to use the Python3 installation and not Python2.6? 2.6 is the default installation on Ubuntu. I can't upgrade that as it will break Ubuntu.
Any single pip installation is (roughly) specific to one Python installation. You can, however, have multiple parallel pip installations. Your package manager probably has a package called pip-3.3 or similar. If not, you can manually install it (run the get-pip.py script using Python 3.3), though you'll have to be careful that it ends up in the right place in PATH. You can also use a virtualenv.
Install pip by downloading the source and doing:
python3 setup.py install
Then, you should have a pip3 or a pip-3.3 command that you can use, to instruct pip to install whatever package using python3
Conversely, if you want to migrate to python3 entirely, do which python and follow the symlinks to /usr/local/bin (I think). There, you should be able to sudo ln -s to change what python points to
I installed Python 3.x (besides Python 2.x on Ubuntu) and slowly started to pair modules I use in Python 2.x.
So I wonder, what approach should I take to make my life easy by using pip for both Python 2.x and Python 3.x?
The approach you should take is to install pip for Python 3.2.
You do this in the following way:
$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python3.2 get-pip.py
Then, you can install things for Python 3.2 with pip-3.2, and install things for Python 2-7 with pip-2.7. The pip command will end up pointing to one of these, but I'm not sure which, so you will have to check.
What you can also do is to use apt-get:
apt-get install python3-pip
In my experience this works pretty fluent too, plus you get all the benefits from apt-get.
First, install Python 3 pip using:
sudo apt-get install python3-pip
Then, to use Python 3 pip use:
pip3 install <module-name>
For Python 2 pip use:
pip install <module-name>
The shortest way:
python3 -m pip install package
python -m pip install package
If you don't want to have to specify the version every time you use pip:
Install pip:
$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python3
and export the path:
$ export PATH=/Library/Frameworks/Python.framework/Versions/<version number>/bin:$PATH
In Windows, first installed Python 3.7 and then Python 2.7. Then, use command prompt:
pip install python2-module-name
pip3 install python3-module-name
That's all
This worked for me on OS X: (I say this because sometimes is a pain that mac has "its own" version of every open source tool, and you cannot remove it because "its improvements" make it unique for other apple stuff to work, and if you remove it things start falling appart)
I followed the steps provided by #Lennart Regebro to get pip for python 3, nevertheless pip for python 2 was still first on the path, so... what I did is to create a symbolic link to python 3 inside /usr/bin (in deed I did the same to have my 2 pythons running in peace):
ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/pip /usr/bin/pip3
Notice that I added a 3 at the end, so basically what you have to do is to use pip3 instead of just pip.
The post is old but I hope this helps someone someday. this should theoretically work for any LINUX system.
On Suse Linux 13.2, pip calls python3, but pip2 is available to use the older python version.
To use pip for a Python 2.x environment, use this command:
py -2 -m pip install -r requirements.txt
To use pip for Python 3.x environment, use this command:
py -3 -m pip install -r requirements.txt
Please note that on msys2 I've found these commands to be helpful:
$ pacman -S python3-pip
$ pip3 install --upgrade pip
$ pip3 install --user package_name
In the general case, you will need to find out the precise location of your Python 2 and Python 3 interpreters. On Windows, the whereis command will reveal where a program is installed; on Unix and related platforms, try type or command -v (don't use the popular but nonstandard tools whereis or which unless you know you can trust them).
Windows example:
C:\loser> whereis python
C:\Python37\Python.exe
Linux example:
bash$ type python
python is /usr/local/bin/python
For instance, you might have Python 3 in /usr/local/bin/python3 and Python 2 in /usr/bin/python.
(This is by no means universal; where and how you installed the different Python versions can vary enormously. You could have Python in /opt/anaconda/forest/trees/shirley for all I know. Probably also look in /opt and in your home directory, or just use the search facilities of your OS to see what's where.)
Generally, if you find a Python binary, /path/to/this/python --version will print its version number.
Once you know this, using the full path to the version you want to use will always get you precisely that version, with its specific configuration for where to install packages.
So, in this example,
/usr/local/bin/python3 -m pip install requests
will install requests for Python 3, and
/usr/bin/python -m pip install requests
for Python 2. But again, these are just examples; you need to know the correct paths for your specific system.
On Windows (and now, going forward, other platforms) there is a wrapper called py which lets you conveniently specify which version of Python you want to run with an option. py -2 runs Python 2 (provided you have installed it, of course), and you can probably guess how to get Python 3.
By the way; on Debian and derived Linux distros, including Ubuntu, Mint, etc, pip is not necessarily installed when you apt install python3 (or python etc); you will need to separately install python3-pip (or python-pip etc).
For convenience, you might want to explore using virtual environments to control your Python environment. With Python 3, you want /path/to/your/python -m venv envname to create envname which, when activated, will always run that particular Python version when you simply run python, and the correct corresponding pip when you type that command. (Again, on Debian-based distros, you will separately need to install python3-venv.) With Python 2, you need to separately install the third-party module virtualenv. (There are other tools with similar facilities but slightly different features; but these are de facto standard, pretty much.)
If there are only a couple of versions which you want to keep on using in parallel, you can create a wrapper or alias so you don't have to type the full path every time. For example, if $HOME/bin is on your PATH, you could create a file $HOME/bin/py3 with the contents
#!/bin/sh
exec /path/to/python3 "$#"
and chmod +x this file to be able to type just py3 going forward. (The syntax for a similar wrapper on Windows will be different; in cmd you want %* instead of "$#" and no exec or #!/bin/sh shebang, and probably a # in front. There is no chmod on Windows, but the file needs to have the .cmd extension.)
Of course, if the versions you want to use already have different names, you just need to make sure their directories are on your PATH, and make sure no other programs with the same name are in a directory which is earlier on your PATH.
If you want to be able to use multiple Python versions in parallel (for example, a specific version for each project you are working on) there are third-party tools like pyenv which let you maintain a number of parallel Python installations and choose between them easily. But this is just a convenience; if you know what you are doing, you can always run a specific Python version with the instructions in this answer.