A story about trying to run multiple python versions (Mac edition)

Previously I set up various machine learning libraries by the method of building a virtual environment introduced in the article, but as I investigated variously There is also code created in the python3 system operating environment, and it is troublesome to rewrite it for Python2 every time, so this time I would like to talk about introducing Python3 without breaking the default Python environment on the Mac.

--Reference: Procedure memo for creating a virtualenv environment with Homebrew Python2 and Python3

Development environment

Advance preparation

--Homebrew update

$ brew update

--Install virtualenv

$ pip install virtualenv

--Install Python3

brew install python3

Environment

$ mkdir -p $(Working directory name)
$ cd $(Working directory)
$ python3 -m venv venv
$ source venv/bin/activate

When I tried it, when I entered the command on the third line from the top, I got the following error.

Failed to import the site module
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 544, in <module>
    main()
  File 

(Omission)

"/usr/local/Cellar/python3/3.6.1/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.

When I looked it up, it seems that the PYTHON PATH I set earlier was in the way.

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

It seems that Python 3 didn't work because of this. (Reference) So, cancel the PYTHON_PATH setting with the following command.

$ unset PYTHONPATH

If you try again with this, you can build the environment without problems,

(venv)$

have become. Now you can run Python3. After that, you can use it by installing the libraries required in this environment from scratch. By the way, if you want to return to the original environment, just like last time

$ deactive

You can do it by typing.

Recommended Posts

A story about trying to run multiple python versions (Mac edition)
A story about trying to implement a private variable in Python.
A story about trying to run JavaScripthon on Windows and giving up.
A story about trying a (Golang +) Python monorepo with Bazel
A story about a Python beginner trying to get Google search results using the API
A story about trying to introduce Linter in the middle of a Python (Flask) project
A story about how to specify a relative path in python.
Introduction to Python Django (2) Mac Edition
[Note] A story about trying to override a class method with two underscores in Python 3 series.
A story about trying to automate a chot when cooking for yourself
A story about adding a REST API to a daemon made with Python
Create a shell script to run the python file multiple times
A story about Python pop and append
Run Python code on A2019 Community Edition
How to run a Maya Python script
When running a Python shell from Electron, pass multiple arguments to run Python.
A story about trying to connect to MySQL using Heroku and giving up
A story about a beginner trying hard to set up CentOS 8 (procedure memo)
A story about running Python on PHP on Heroku
Think about building a Python 3 environment in a Mac environment
A story about modifying Python and adding functions
How to build an environment for using multiple versions of Python on Mac
A * algorithm (Python edition)
A story about making 3D space recognition with Python
A story that got stuck when trying to upgrade the Python version on GCE
Create a plugin to run Python Doctest in Vim (2)
A story about how Windows 10 users created an environment to use OpenCV3 with Python 3.5
Create a plugin to run Python Doctest in Vim (1)
A story about making Hanon-like sheet music with Python
A story about struggling to loop 3 million ID data
I want to run a quantum computer with Python
A story about a Python beginner who was about to be crushed by ModuleNotFoundError: No module named'tweepy'
A story about trying to use cron on a Raspberry Pi and getting stuck in space
[Django] A story about getting stuck in a swamp trying to validate a zip with form [TDD]
How to run a Python file at a Windows 10 command prompt
How to run a Python program from within a shell script
A simple way to avoid multiple for loops in Python
A story about an amateur making a breakout with python (kivy) ②
A story about how to deal with the CORS problem
A story about an amateur making a breakout with python (kivy) ①
A story about a python beginner stuck with No module named'http.server'
[Python] A story that seemed to fall into a rounding trap
A road to intermediate Python
Install multiple versions of Python
Update python on Mac to 3.7-> 3.8
A memorandum about correlation [Python]
How to run Notepad ++ Python
A memorandum about Python mock
A note about [python] __debug__
A story about everything from data collection to AI development and Web application release in Python (3. AI development)
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
Build and test a CI environment for multiple versions of Python
A story that struggled to handle the Python package of PocketSphinx
A story about developing a soft type with Firestore + Python + OpenAPI + Typescript
Call dlm from python to run a time-varying coefficient regression model
How to delete multiple specified positions (indexes) in a Python list
A story about wanting to think about garbled characters on GAE / P
A story about trying to reproduce Katsuwo Isono, who does not react to inconvenience, by natural language processing.
A story about someone who wanted to import django from a python interactive shell and save things to a DB
A story about trying to improve the testing process of a system written in C language for 20 years