Building multiple Python environments on the same system

Python is installed on Mac OS X from the beginning, but if you want to use different versions and packages in various situations on the same system, a procedure memo.

――I want to use multiple versions properly on the same system. ――I want to use different versions and package sets for each project. ――I want to use it properly, but I want to reduce the trouble of switching. ――I don't want to pollute the existing environment when trying new packages and software.

The goal is to clear such requests. Version control systems in Python include pyenv, virtualenv, and direnv. I don't have enough experience to evaluate it, but pyenv also has a plugin for the virtualenv function, so I will build it with pyenv. Also, managing individual packages is complicated, so I will use Anaconda.

Anaconda is a free distribution of various packages for building a math environment for Python. By installing, you can install not only NumPy, SciPy, matplotlib, but also packages such as the machine learning library scikit-learn. http://morimori2008.web.fc2.com/contents/PCprograming/python/pythonAnaconda.html

The following configuration is assumed.

Homebrew installation

Launch Terminal.app and copy and paste the following command to execute it.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Below, we will install using the Homebrew function. The command at the beginning that says brew uses this feature.

Install pyenv

Install pyenv using the Homebrew mechanism.

$ brew install pyenv

Install pyenv-virtualenv

Install pyenv-virtualenv using the Homebrew mechanism.

$ brew install pyenv-virtualenv

Pass the path to the system shell startup script

(For bash) Pass the path related to pyenv to bash_profile. The operation of vi is difficult to understand, but from Terminal.app

$  open ~/.bash_profile 

When you type, bash_profile will open in a text editor, so you can edit the file with a familiar operation feeling. Describe as follows.

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

To reflect the settings, restart Terminal.app or execute the following command.

$ source ~/.bash_profile

Check the version that can be installed with pyenv

Display the list with the following command in Terminal.app.

$ pyenv install -l

It will be listed like this.

Available versions:
  2.1.3
  2.2.3
  ...
  ...

Environment construction procedure

  1. Download the required version of Python
  2. Use the pyenv virtualenv command to give the installation version a versioning identifier.
  3. Associate the version specification identifier with a specific directory
  4. Install the additional required packages by specifying global or local (using the version specification identifier).

Download the required version of Python

Example of installing Anaconda (Python version 2 system)

$ pyenv install anaconda-2.1.0

Example of installing Anaconda (Python version 3 system)

$ pyenv install anaconda3-2.1.0

Example of installing Python version 2 system

$ pyenv install 2.7.9

Example of installing Python version 3 system

$ pyenv install 3.4.2

Add a versioning identifier to the installation version with the pyenv virtualenv command

Specify it like this. You need to specify the version number accurately, but please make your own version specification identifier.

$pyenv virtualenv version number version specification identifier

If you use anaconda3-2.1.0 and add a versioning identifier called condaanalyse1, it will be like this.

$ pyenv virtualenv anaconda3-2.1.0 condaanalyse1

Associate a specific directory with a versioning identifier

  1. Move to a specific directory
$ cd specific_prj
  1. Associate the directory with the versioning identifier
$ pyenv local condaanalyse1
  1. If it is under this directory, the specified version of Python will be selected automatically.

Main commands of pyenv

$ pyenv install -l //Display a list of installable versions
$ pyenv install <Version number> //Installation
$ pyenv uninstall <Version number> //Uninstall

$ pyenv version //Show current version
$ pyenv versions //Display the list of installed versions

$ pyenv local <Version number> //Version switching in the current directory
$ pyenv global <Version number> //System-wide version switching

How to use with Anaconda's version control system

Anaconda also has a version control system (conda). Penv is for version control on the same system including Anaconda, and conda is for management in the package provided by Anaconda, and the management layer is different, so use it properly.

Recommended Posts

Building multiple Python environments on the same system
Install and manage multiple environments of the same distribution on WSL
About the --enable-shared option when building Python on Linux
Building a Python environment on Ubuntu
Memorize the Python commentary on YouTube.
Sakura Use Python on the Internet
Manage multiple execution environments using Python venv
Set client-specific ssh host keys on multiple diskless clients that utilize the exact same root file system
Download files on the web with Python
[python] Permutation generation considering the same elements
[Python] A progress bar on the terminal
Python open and io.open are the same
Script to use multiple github accounts properly in the same repository on the same machine
Find a building on Google Earth that is the same height as Shingodzilla
Turn multiple lists with a for statement at the same time in Python
Python: Try using the UI on Pythonista 3 on iPad
Try CIing the pushed python code on GitHub.
Introduction to Python with Atom (on the way)
Sound the buzzer using python on Raspberry Pi 3!
At the time of python update on ubuntu
Building a Python environment on a Sakura VPS server
Python on Windows
twitter on python3
58 The same castle
python on mac
Python on Windbg
Think about how to program Python on the iPad
I tried Python on Mac for the first time.
Probably the most unhelpful Python implementation method on Qiita
I tried python on heroku for the first time
[Python] Evaluate the facial expressions that appear on the face
Install the 3rd party python library on Cinema 4D
Information for controlling the motor with Python on RaspberryPi
Python program that looks for the same file name
Execute python3 system with PHP exec () on AWS EC2
Procedure for building a CDK environment on Windows (Python)
Write a log-scale histogram on the x-axis in python
How to enjoy Python on Android !! Programming on the go !!
Install django on python + anaconda and start the server
How to register the same data multiple times with one input on the Django management screen