Python is not compatible with 2 series and 3 series respectively. There are two versions on the market because of this relationship.
Because there was a slide that introduced the method of virtual environment management in an easy-to-understand manner I borrowed the contents of that slide and added the contents of the virtualenv that I mainly use. If you want to know more, please refer to the references.
pyenv-virtualenv can be set for each directory, and unlike vitalenv, it does not need to be activated at the time of use, so it is the most recommended.
virtualenv -A tool that allows you to switch between libraries used in the same version of Python. ・ Can be installed with pip ・ Can be used on Windows <Citation: [Machine Learning Nagoya_7th Study Group_Python Environment Construction Method.key](https://speakerd.s3.amazonaws.com/presentations/0d969d6526dd4b5fa0549f4e3481745f/%E6%A9%9F%E6%A2%B0 % E5% AD% A6% E7% BF% 92% E5% 90% 8D% E5% 8F% A4% E5% B1% 8B_% E7% AC% AC7% E5% 9B% 9E% E5% 8B% 89% E5 % BC% B7% E4% BC% 9A_Python% E7% 92% B0% E5% A2% 83% E6% A7% 8B% E7% AF% 89% E6% 96% B9% E6% B3% 95_20161015.pdf)> pyenv -Tools for managing various Python versions ・ You can switch between Python 2 and 3 -Python to be used in shell and directory can be specified -You cannot switch libraries with the same version of Python. ・ Cannot be used on Windows <Citation: [Machine Learning Nagoya_7th Study Group_Python Environment Construction Method.key](https://speakerd.s3.amazonaws.com/presentations/0d969d6526dd4b5fa0549f4e3481745f/%E6%A9%9F%E6%A2%B0 % E5% AD% A6% E7% BF% 92% E5% 90% 8D% E5% 8F% A4% E5% B1% 8B_% E7% AC% AC7% E5% 9B% 9E% E5% 8B% 89% E5 % BC% B7% E4% BC% 9A_Python% E7% 92% B0% E5% A2% 83% E6% A7% 8B% E7% AF% 89% E6% 96% B9% E6% B3% 95_20161015.pdf)> pyenv-virtualenv -A tool that allows you to switch libraries with the same version of Python with pyenv. ・ The author is the same as pyenv ・ Cannot be used on Windows <Citation: [Machine Learning Nagoya_7th Study Group_Python Environment Construction Method.key](https://speakerd.s3.amazonaws.com/presentations/0d969d6526dd4b5fa0549f4e3481745f/%E6%A9%9F%E6%A2%B0 % E5% AD% A6% E7% BF% 92% E5% 90% 8D% E5% 8F% A4% E5% B1% 8B_% E7% AC% AC7% E5% 9B% 9E% E5% 8B% 89% E5 % BC% B7% E4% BC% 9A_Python% E7% 92% B0% E5% A2% 83% E6% A7% 8B% E7% AF% 89% E6% 96% B9% E6% B3% 95_20161015.pdf)>
I've also introduced how to install Linux and Windows. Since the amount of sentences will be large, I will omit it only for Mac.
virtualenv
Installation
$ pip install virtualenv
Create virtual environment
$ virtualenv -p [Python version] [Virtual environment name]
#Example
$ virtualenv -p 3.5 python3.5
use
$ source [Virtual environment name]/bin/activate
#Example
source python3.5/bin/activate
If successful, the terminal display will be displayed.
(python3.5)$
Should have changed.
#### **`End`**
```shell
$ deactivate
If you want to delete the environment, you can delete the created directory.
pyenv-virtualenv
Installation
$ brew install pyenv-virtualenv
Environment variable settings(For bash, the default is bash)
$ echo 'export PYENV_ROOT="/usr/local/var/pyenv"' >> ~/.bash_profile
$ echo 'if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi' >> ~/.bash_profile
$ echo 'if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi' >> ~/.bash_profile
$ source ~/.bash_profile
If you use a shell other than bash, please change the **. Bash_profile ** part.
Environment variable settings(For zsh)
$ echo 'export PYENV_ROOT="/usr/local/var/pyenv"' >> ~/.zshrc
$ echo 'if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi' >> ~/.zshrc
$ echo 'if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi' >> ~/.zshrc
$ source ~/.zshrc
Python installation
pyenv install [Python version]
Create virtual environment
$ pyenv virtualenv [Python version] [Virtual environment name]
#Example
$ pyenv virtualenv 2.7.11 Py2Base
$ pyenv virtualenv Py2Base py2-test
If you create a new virtual environment from the virtual environment, it seems that the library etc. will be inherited.
Change Python used as standard
$ pyenv global [Virtual environment name]
Change Python in the current shell
$ pyenv shell [Virtual environment name]
Change Python in the current directory
$ pyenv local [Virtual environment name]
Delete virtual environment
$ pyenv unisntall [Virtual environment name]
[Machine learning Nagoya_7th study session_Python environment construction method.key](https://speakerd.s3.amazonaws.com/presentations/0d969d6526dd4b5fa0549f4e3481745f/%E6%A9%9F%E6%A2%B0%E5% AD% A6% E7% BF% 92% E5% 90% 8D% E5% 8F% A4% E5% B1% 8B_% E7% AC% AC7% E5% 9B% 9E% E5% 8B% 89% E5% BC% B7% E4% BC% 9A_Python% E7% 92% B0% E5% A2% 83% E6% A7% 8B% E7% AF% 89% E6% 96% B9% E6% B3% 95_20161015.pdf)
Recommended Posts