2

First I run command pip install virtualenv then after I run python -m virtualenv venv, I get this following error msg

"/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenv"

Cuurently, I'm using python v2.7.16 and when I run pip freeze | grep virtualenv , I get virtualenv==20.4.2 so virtualenv is there. When I run which python I get /usr/bin/python and I don't have .bash_profile when I run ls -a. I am using mac. What could be the reasons python not recognizing virtualenv when it's there?

Judoboy Alex
  • 326
  • 1
  • 14
  • does pip3 install virtualenv work? Also try python3 -m virtualenv venv – coderboi Mar 10 '21 at 00:19
  • Run `pip` with `python -m pip`, same as you're running `virtualenv`. This should guarantee that you're using the same Python for both. The `pip` script might be set up for a different interpreter instance. Try running `pip -V` - it should display which interpreter it uses. – Czaporka Mar 10 '21 at 00:31
  • When I run "python -m pip", I get No module name pip. When I run pip -V, I get pip 18.1 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip (python 2.7) Is there any other way to get pip to use same python as virtualenv? – Judoboy Alex Mar 10 '21 at 01:49

1 Answers1

0
  1. You may create .bash_profile and it is auto-recognised by the macintosh machine.

  2. Please also run which pip and make sure the pip is in the same bin as your python (/usr/bin/python)

The bottom line is pip used to install a package by default will install the packages in the bin directory that also stored your python executable.

hongkail
  • 679
  • 1
  • 10
  • 17
  • When I run "which pip", I get /usr/local/bin/pip but when I run "which python", I get /usr/bin/python. Also when I run "python -m pip", I get No module named pip error msg. So how can I switch pip to use same location as python which is /usr/bin/python? – Judoboy Alex Mar 10 '21 at 01:44
  • I see. Very interesting setup. i will recommend that you ls /usr/bin/pip and see pip is in this /usr/bin/ and "/usr/bin/pip install virtualenv" – hongkail Mar 10 '21 at 02:28
  • 1
    There is no pip in /usr/bin, but there is python2.7 in /usr/local/bin. If I change the path for python to /usr/local/bin/python, which is same as pip path then everything should be ok? – Judoboy Alex Mar 10 '21 at 02:47
  • Yes. that is a good reverse thought. You may actually "$ export PATH="/usr/local/bin:$PATH"" OR "$ export PATH="/usr/local/bin:$PATH"", depending on your system if PATH is read from the front or the back It is also better to execute "$ /usr/local/bin/python" to check what is the version of the python. Sometimes it is just a softlink or a copy to the python2 or python3. The best approach to resolve it and save all the future problem is to use anaconda. https://stackoverflow.com/questions/66112932/replicate-python-environment-on-another-computer/66113003#66113003 – hongkail Mar 10 '21 at 06:05
  • 1
    "/usr/local/bin/python -m virtualenv venv" did the trick for me. I know it's not the ideal solution, but I thought things could get worse if I don't set export PATH correctly. – Judoboy Alex Mar 10 '21 at 06:09
  • Great ! Happy to know you find the best way that works you ;) – hongkail Mar 10 '21 at 06:10