43

I was trying to use virtualenv to switch between python versions before learning that I could use both python and python3 on my Mac.

I was able to fix my python 2.7 version so that still works fine however, now when I run python3, I get this error:

Failed to import the site module
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 544, in <module>
main()
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 282, in addusersitepackages
user_site = getusersitepackages()
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 258, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 248, in getuserbase
USER_BASE = get_config_var('userbase')
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 601, in get_config_var
return get_config_vars().get(name)
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 580, in get_config_vars
import _osx_support
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_osx_support.py", line 4, in <module>
import re
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 125, in <module>
import functools
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/functools.py", line 21, in <module>
from collections import namedtuple
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/collections/__init__.py", line 32, in <module>
from reprlib import recursive_repr as _recursive_repr
  File "/usr/local/lib/python2.7/site-packages/reprlib/__init__.py", line 7, in <module>
raise ImportError('This package should not be accessible on Python 3. '
ImportError: This package should not be accessible on Python 3. Either you are trying to run from the python-future src folder or your installation of python-future is corrupted.

I've looked online for this error but most of the conversation was on fixing up the patch and then upgrading virtualenv. However, this still doesn't fix my issue.

python3 -V: Python 3.6.0
virtualenv --version: 15.1.0
env | egrep -i 'python|virtualenv': PYTHONPATH=/usr/local/lib/python2.7/site-packages:
user1883614
  • 905
  • 3
  • 16
  • 30

2 Answers2

115

Your environment contains PYTHONPATH=/usr/local/lib/python2.7/site-packages

This doesn't work with Python 3 for obvious reasons. To remove it:

unset PYTHONPATH
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 2
    But I would like to keep both python and python3. Does unsetting affect my python (2.7)? – user1883614 Feb 13 '17 at 22:17
  • If its configuration is correct/sane, it'll have its own `site-packages` in its `sys.path` by default. So if unsetting `PYTHONPATH` breaks your Python 2.7, then something about your Python 2.7 install was already broken. – Charles Duffy Feb 13 '17 at 22:18
  • 3
    For some reason, I have to unset it every time I need to use python3. Is there any way I can make it permanent? – user1883614 Feb 16 '17 at 17:28
  • Find the place where the setting was made permanent in the first place, and undo it. It's not something that's there by default. – Charles Duffy Feb 16 '17 at 17:38
  • ...if you're having trouble finding that location, try running `PS4=':$BASH_SOURCE:$LINENO+' bash -x -l -i`, and look for references to `PYTHONPATH` in the output; each line will be prefixed by the source filename and line number where it came from. It's not quite a sure thing that that'll tell you where it's being set (could be something like PAM modules that are invoked before your shell is started), but it's a good place to start. – Charles Duffy Feb 16 '17 at 17:40
  • I lost this when cleaning up my .zshrc file so have come back here to be rescued again. I can't live without `grcat`. – Sridhar Sarnobat Oct 07 '20 at 23:57
17

I solved my problem with below command

sudo pip install virtualenv --upgrade
virtualenv -p python3 env
anjaneyulubatta505
  • 10,713
  • 1
  • 52
  • 62
  • 3
    FYI for the copy-pasters out there, this will create a virtual environment named `env` running Python 3, which you can then use by activating w/ `source env/bin/activate`. You'll also want to be aware of the `deactivate` and `which python` commands to jump around Python envs. Given the OP wishes to switch between Python 2x and Python 3x, this is the best approach. – BoltzmannBrain Jun 24 '18 at 18:14