2

Here is an extract of my console in a Mac, as you can see the Python on the console is 2.7.15 but on the environment it becomes 2.7.10 which is an older version... any help on why and how to fix it would be most welcomed!

dhcp-18-57:Wakeup6 **jbs$ python**  
**Python 2.7.15** (default, May  1 2018, 16:44:14)  
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin  
Type "help", "copyright", "credits" or "license" for more information.  
>>> exit()  

dhcp-18-57:Wakeup6 jbs$ virtualenv env  
New python executable in /Users/jbs/PycharmProjects/Wakeup6/env/bin/python  
Installing setuptools, pip, wheel...done.  

dhcp-18-57:Wakeup6 jbs$ source env/bin/activate  

(env) dhcp-18-57:Wakeup6 **jbs$ python**  
**Python 2.7.10** (default, Oct 23 2015, 19:19:21)  
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin  
Type "help", "copyright", "credits" or "license" for more information.  
>>> exit()
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • im not sure how your computer is set up, but it sounds like the alias you have set up for your account is different than that of the Virtual Env. – Fallenreaper May 04 '18 at 19:05
  • Can you please tell me how I can check this? Thank you in advance – Brian Barcelona May 04 '18 at 19:07
  • @BrianBarcelona - are you struggling with that? – tdelaney May 04 '18 at 19:11
  • yes... I'm struggling... does my account have a different alias than the Virtual Env? Does each Virtual Env have its own alias? How can one check if so and fix the above issue? – Brian Barcelona May 04 '18 at 19:12
  • By default, `virtualenv` uses the python it was installed with, and apparently that's python 2.7.10, not 2.7.15. You could use the `--python=PYTHON_EXE` option. See the docs at https://virtualenv.pypa.io/en/stable/reference/#cmdoption-p – tdelaney May 04 '18 at 19:13
  • Thanks for the hint. Will do... can you please tell us if it wouldn't be better to simply install virtualenv with 2.7.15? – Brian Barcelona May 04 '18 at 19:14
  • You may be able to reinstall virtualenv to its latest version (and that should also make python 2.7.15 its default). But if virtualenv hasn't changed how its setting up the environment, then just usnig the `--python` flag works just as well. You may be able to do `python \`which virtualenv\` env` – tdelaney May 04 '18 at 19:24
  • I did sudo -H virtualenv --python=/usr/local/Cellar/python@2/2.7.15 env and got Running virtualenv with interpreter and then an error... OSError: [Errno 13] Permission denied... – Brian Barcelona May 04 '18 at 19:37
  • any additional help would be most welcomed... – Brian Barcelona May 04 '18 at 19:46
  • @BrianBarcelona Yes, it would be better to install virtualenv with Python 2.7.15. More generally, if you don't want to use Apple's pre-installed Python 2.7.10, you should never install any packages into its site-packages. (That's _especially_ true when you want to use another 2.7, because things get very confusing when you can't even differentiate with `pip3.4` or `virtualenv3` or the like because your two Pythons have the same version.) – abarnert May 04 '18 at 20:01
  • Thank you. I don't know how to install virtualenv with Python 2.7.15... I tried a few things unsuccessfully.... any hints on this one? What happens I believe is that it somehow still uses 2.7.10... – Brian Barcelona May 04 '18 at 20:06
  • If you want to clean things up: (1) figure out how to explicitly invoke the `pip` that goes with Apple's python 2.7.10. (2) use it to `pip list` and then `pip uninstall` everything you installed with it. (3) `/usr/bin/python -c 'import sys; print "\n".join(sys.path)'` and verify that the only thing not in `/System` is `/Library/Python/2.7/site-packages`. (4) Delete anything in that directory besides `Extras.pth` and `README`. (5) Optionally reinstall the Python 2.7 you actually want, and install all the packages you need for that. – abarnert May 04 '18 at 20:07
  • If you just want to install another one in parallel: (1) figure out how to explicitly invoke the `pip` that goes with your Python 2.7.15. (2) use it to `pip install virtualenv`. (3) `which -a virtualenv` to see if you have two of them on your $PATH now; if so, you may need to rearrange your $PATH or create a bash alias or something, unless you want to manually run /path/to/new/virtualenv every time. – abarnert May 04 '18 at 20:08
  • Thank you. I'll work at earnest at it. However, I can't figure out how to explicitly invoke the pip that goes with my Python 2.7.15.... any suggestion would be most welcomed... – Brian Barcelona May 04 '18 at 20:26
  • sudo -H virtualenv --python=/usr/local/Cellar/python@2/2.7.15 env this gives an error as stated above... – Brian Barcelona May 04 '18 at 20:32

2 Answers2

1

If you installed python using Anaconda, then set up a new virtual environment for python 2.7.15 Then following directions in the Anaconda documentation, activate this virtual environment and you should be good to go.

If you didn't use Anaconda, just follow the directions for setting up a virtual environment on a Unix box. Then activate that environment and start working.

You can have multiple versions on your computer. Virtual environments allow you to run the version you want. I have virtual environments for python 2.7.13 and python 3.6.

If this doesn't work please let me know. I'll try to help.

Natsfan
  • 4,093
  • 3
  • 22
  • 29
  • Thanks for your answer.... I don't use Anaconda... I installed it in the console directly and then program with Pycharm... What you say I've tried and it does not work for the very good reasons mentioned above by @tdelaney in the comments.... – Brian Barcelona May 04 '18 at 19:39
  • did you create a virtual environment for 2.7.15? if so did you actvate it before running any python commands? – Natsfan May 04 '18 at 19:48
  • I'm not certain but I thought PyCharm came with its own version of python. – Natsfan May 04 '18 at 19:50
  • Pycharm lets you select the version of python you want to use. This is set in preferences ... project interpreter ... – Brian Barcelona May 04 '18 at 19:59
  • I did activate the environment – Brian Barcelona May 04 '18 at 20:00
  • then I don't see your problem. Pycharm should use version you selected. – Natsfan May 04 '18 at 20:01
  • before you need to create a virtual environment with the selected Python interpreter and I don't succeed in doing it... – Brian Barcelona May 04 '18 at 20:04
  • How do you know you didn't succeed in creating virtual environment? Did you get an error message? you said you activated environment. – Natsfan May 04 '18 at 20:08
  • yes, I added the environment but then, as my original question indicates, the python inside the environment is 2.7.10... – Brian Barcelona May 04 '18 at 20:33
  • yes I don't see how that could be. I'm sorry but I'm out of ideas! – Natsfan May 04 '18 at 20:39
  • each line on the terminal should begin with name of your environment. my python 3.6 virtual environment is named py36, Lines written on my terminal all begin with '(py36)' . Do you see this on your terminal? – Natsfan May 04 '18 at 20:55
1

Thank you all for your input. I finally figured out how to solve it. The difficult part was finding out how to locate the place where python 2.7.15 was installed which I'll leave intact in the response since I found it by trial and error looking in other places. I have not found a command that gives you the full path (perhaps an expert can tell us how to go from the laconic which python to the full path).

What I find interesting is that it does not really matter what the console uses when calling virtualenv. What matters is the version of Python used to install virtualenv which is the default version that will be used.

Essentially one has to use the -p option in virtualenv and make sure to find the right location where the desired version of python is.

dhcp-18--41:WakeUp8 jbs$ virtualenv -p /usr/local/Cellar/python@2/2.7.15/bin/python env
Running virtualenv with interpreter /usr/local/Cellar/python@2/2.7.15/bin/python
New python executable in /Users/jbs/PycharmProjects/WakeUp8/env/bin/python2.7
Also creating executable in /Users/jbs/PycharmProjects/WakeUp8/env/bin/python
Installing setuptools, pip, wheel...done.
dhcp-18-189-34-41:WakeUp8 jbs$ python
Python 2.7.15 (default, May  1 2018, 16:44:14) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

dhcp-18--41:WakeUp8 jbs$ pwd
/Users/jbs/PycharmProjects/WakeUp8

dhcp-18-189-34-41:WakeUp8 jbs$ source env/bin/activate

(env) dhcp-18-189-34-41:WakeUp8 jbs$ python
Python 2.7.15 (default, May  1 2018, 16:44:14) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
  • See also this related question about the strange down-versioning of pip inside the environment (https://stackoverflow.com/questions/50194070/a-python-virtual-environment-ends-up-with-an-older-version-of-pip-than-the-one-t/50201418#50201418) – Brian Barcelona May 06 '18 at 22:38