Difference between pip3 and python3 -m pip

I am trying to install some packages using pip and python3. I use MacOS, so by default, when I run pip, it uses my version of Python 2.

I was able to install the package in python 3 using:

$ pip3 install package_name 

However, I can do the same (at least it seems):

 $ python3 -m pip install package_name 

I wonder if pip3 and python3 -m pip have the same effect.

+7
source share
2 answers

They are the same. If you look at the pip3 file in the bin folder, it calls the main function from the pip module.

pip3 install package_name runs the pip3 install package_name file in the bin folder:

 # bin/pip3 # or bin/pip if using pip install package_name import re import sys from pip import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(main()) 

python3 -m pip install package_name runs the __init__.py file of the pip module.

 # pip/__init__.py if __name__ == '__main__': sys.exit(main()) 

Both of them run the same main() function.

+9
source

As @tihorn says, pip3 and python3 -m pip should be the same. There is at least one exception: if they are not on the same path. I had the following setup:

 $ which pip3 /usr/bin/pip3 $ which python3 /home/username/anaconda3/bin/python3 

After installing the modules with pip3 and checking with pip3 freeze I was not able to access them when running python3 my_script.py or python3 -c 'import my_module' . I got a ModuleNotFound error.

+1
source

Source: https://habr.com/ru/post/1261796/


All Articles