I was investigating python version control when preparing the python environment, but I was quite confused by virtualenv and pyenv-virtualenv.
I found this article at that time. It was very easy to understand and refreshed.
I will try to summarize it myself.
pyenv-virtualenv and virtualenv are different, and the following two combinations are possible.
For example, when you want to use different python environments for projectA, projectB, and projectC, if you use pyenv-virtualenv, all three environments will be created in the HOME directory, and you will refer to them from each project. If you use virtualenv, a python environment will be created within each project. I think that's what it means.
virtualenv can be completed in the project directory, but it is troublesome to have to activate the environment every time. pyenv-virtualenv is easy because the environment is automatically applied when you enter the project directory, but it is a bit unpleasant that the environment is created under the HOME directory. Because you don't really want to use the same environment from another project.
So I decided to use pyenv + virtualenv.
pyenv
Install pyenv with homebrew.
$ brew install pyenv
Add the following settings to .bash_profile
etc.
.bash_profile}
export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
Try installing the latest version of python.
You can see a list of installable versions with pyenv install -l
. Try installing the latest versions of 2 and 3 series.
$ pyenv versions
* system (set by /Users/clutter/.pyenv/version)
$ pyenv install -l
$ pyenv install 2.7.12
$ pyenv install 3.5.2
$ pyenv versions
* system (set by /Users/clutter/.pyenv/version)
2.7.12
3.5.2
You have successfully installed 2.7.12
and 3.5.2
.
Try switching versions with pyenv.
~$ pyenv version
system (set by /Users/clutter/.pyenv/version)
~$ python -V
Python 2.7.10
##global version change
~$ pyenv global 3.5.2
~$ pyenv version
3.5.2 (set by /Users/clutter/.pyenv/version)
~$ python -V
Python 3.5.2
##Changes in a specific directory
~$ mkdir pytest
~$ cd !$
pytest$ pyenv local 2.7.12
pytest$ pyenv version
2.7.12 (set by /Users/clutter/pytest/.python-version)
pytest$ python -V
Python 2.7.12
pytest$ cd ..
~$ pyenv version
3.5.2 (set by /Users/clutter/.pyenv/version)
~$ python -V
Python 3.5.2
You can see that the versions have been switched between the home directory and the pytest directory.
The local version seems to be recorded in the .python-version
created in that directory.
pytest$ ls -a
. .. .python-version
pytest$ cat .python-version
2.7.12
virtualenv
If you switch to the version installed with pyenv, you can use pip
, so install it.
$ pip install virtualenv
Create a virtual environment and try installing Flask.
$ pip list
pip (8.1.1)
setuptools (20.10.1)
virtualenv (15.1.0)
#Create a new virtual environment venvtest
$ virtualenv venvtest
New python executable in /Users/clutter/venvtest/bin/python2.7
Also creating executable in /Users/clutter/venvtest/bin/python
Installing setuptools, pip, wheel...done.
$ ls
venvtest
$ cd venvtest
#Enable virtual environment
$ source bin/activate
#Install packages in virtual environment
(venvtest) $ pip install Flask
(venvtest) $ pip list
click (6.6)
Flask (0.11.1)
itsdangerous (0.24)
Jinja2 (2.8)
MarkupSafe (0.23)
pip (9.0.1)
setuptools (30.4.0)
Werkzeug (0.11.11)
wheel (0.29.0)
#Exit the virtual environment
(venvtest) $ deactivate
$ pip list
pip (8.1.1)
setuptools (20.10.1)
virtualenv (15.1.0)
When you exit the virtual environment, you can see that Flask is not installed.
If you add --no-site-packages
when creating a virtual environment, it seems that it will ignore the packages installed globally and create the environment in a beautiful state, so it seems better to remember this option. ..
Create with the --python
option
virtualenv venv --python=~/.pyenv/versions/3.5.2/bin/python3
You can set --python = python3.5.2
, but it is said that you do not know which python3 is which anaconda is also installed.
This may be the cause.
.bash_profile
are not loadedRecommended Posts