Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pip vs python -m pip why pip command throws a module object is not callable error

Tags:

python-3.x

pip

I've seen similar posts like

pip install vs python3 -m pip install

that does not really clarify this problem. I am new to Python, just following a basic python 3 tutorial where they mention pip as the dependency manager, and they mention that it should be used as pip install dependency_here

I am using Python 3.8.0, brand new install on Windows (for all users, so that it's installed under C:\Program Files (x86)\Python38-32)

and I cannot run pip command. I get a module error which I have no idea what's about.

$ pip --version
Traceback (most recent call last):
  File "c:\program files (x86)\python38-32\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\program files (x86)\python38-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Program Files (x86)\Python38-32\Scripts\pip.exe\__main__.py", line 9, in <module>
TypeError: 'module' object is not callable

I can, however, use python -m pip command

$ python -m pip --version
pip 19.3.1 from C:\Users\iberodev\AppData\Roaming\Python\Python38\site-packages\pip (python 3.8)

and install modules with it.

The question is why I can't use a command that most of the tutorials mention, and even the official pip repository uses for the examples: https://pypi.org/project/Flask/#Installing

I'd like to know why am I having that error. since it seems not so common

Thanks!

UPDATE 1 (2 Nov 2019): Thanks to the answer I found out the PATH for user and system was a bit of a mess. I decided to clear it and reinstall Python 3.8.0 64bits version and it seems all good now:

$ python --version
Python 3.8.0

And pip

$ pip --version
pip 19.2.3 from c:\users\iberodev\appdata\local\programs\python\python38\lib\site-packages\pip (python 3.8)

and PATH

$ echo $PATH | tr \: \\n | grep "Python"
/c/Users/iberodev/AppData/Local/Programs/Python/Python38/Scripts
/c/Users/iberodev/AppData/Local/Programs/Python/Python38
like image 468
diegosasw Avatar asked Oct 16 '22 09:10

diegosasw


1 Answers

Check your path environment variable. There's a path for the current user (no elevated rights, user environment variables) and a path for the system (elevated rights, system environment variables). Now, to call pip without using it as a python method (python -m pip),

  • your path must include an entry pointing to the Scripts folder from your Python installation (and of course the entry pointing to the folder that contains the python.exe). This is true for whatever installation you want to use (user or system).
  • if you want to call pip from a system Python installation to add packages to that installation, you'll need to call it from a command prompt with elevated rights (similar to using sudo on Linux).

I suspect the second point is what's causing the error you got.

Not entirely sure who creates \AppData\Roaming\Python\Python38\site-packages, but that's not where you'd find a Python installation. A Python user installation would be located in \AppData\Local\Programs\Python. Likely the AppData\Roaming\... folder is used by pip (out of a system installation) when called without elevated rights to install packages.

In principle, I'd suggest to have only a user installation of Python on Windows 10. For most applications, you won't need a system installation in my experience. And I'd rather use the 64bit version. But that depends on what you intend to do.

like image 140
FObersteiner Avatar answered Oct 21 '22 05:10

FObersteiner