Environment construction procedure for those who are not familiar with the python version control system

environment

Premise

pyenv, virtualenv and pyenv-virtualenv

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

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 -)"

Install another version

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.

Version switching

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

Install virtualenv

If you switch to the version installed with pyenv, you can use pip, so install it.

$ pip install virtualenv

Creating a virtual environment

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. ..

When creating an environment by specifying the python version

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.

When it doesn't work

This may be the cause.

reference

Recommended Posts

Environment construction procedure for those who are not familiar with the python version control system
[Solved] I have a question for those who are familiar with Python mechanize.
Environment construction for those who want to study python easily with VS Code (for Mac)
Python environment construction 2016 for those who aim to be data scientists
A modern environment building procedure for those who want to get started with Python right away
For those who want to write Python with vim
The first step of machine learning ~ For those who want to implement with python ~
For those who are new to programming but have decided to analyze data with Python
ABC's A problem analysis for the past 15 times to send to those who are new to Python
For those who are in trouble because NFC is read infinitely when reading NFC with Python
For those who are having trouble drawing graphs in python
Installation procedure for Python and Ansible with a specific version
Python environment construction For Mac
Anaconda3 python environment construction procedure
Python3 environment construction (for beginners)
Machine learning A story about people who are not familiar with GBDT using GBDT in Python
Explanation for those who are having trouble with "command not found" in rbenv or pyenv
What kind of environment should people who are learning Python for the first time build?
[Environment construction] Procedure for implementing the Python environment of Rabbit Challenge, which is a JDLA certified E qualification measure account, with Databricks
[September 2020 version] Explains the procedure to use Gmail API with Python
NumPy example collection for those who are not good at math
Virtual Environment Version Control Summary Python
Get started with Python! ~ ① Environment construction ~
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Python3 TensorFlow for Mac environment construction
pytorch @ python3.8 environment construction with pipenv
[Comparison of PHP, Ruby, and Python description] For those who are wondering how the description method is different.
Java SE8 Gold measures (for those who are not good at it)
Build a version control environment for Python, Ruby, Perl, Node.js on UNIX
[YOLO v5] Object detection for people who are masked and those who are not
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
About the virtual environment of python version 3.7
Environment construction procedure: Ubuntu + Apache2 + Python + Pyramid
Let's get along with Python # 0 (Environment construction)
Tips for those who are wondering how to use is and == in Python
For those who want to learn Excel VBA and get started with Python
5 Reasons Processing is Useful for Those Who Want to Get Started with Python
The first step for those who are amateurs of statistics but want to implement machine learning models in Python
Collecting information from Twitter with Python (Environment construction)
VS Code + Azure Functions + Python environment construction procedure
Prepare the execution environment of Python3 with Docker
Dart grammar for those who only know Python
Python environment construction (Anaconda + VSCode) @ Windows10 [January 2020 version]
Building an Anaconda environment for Python with pyenv
Use logger with Python for the time being
Manage Python multiple version environment with Pythonz, virtualenv
Image Processing with Python Environment Setup for Windows
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
Commands for creating a python3 environment with virtualenv
Procedure for creating a Python quarantine environment (venv environment)
First python ① Environment construction with pythonbrew & Hello World !!
From Python environment construction to virtual environment construction with anaconda
❤️ Bloggers ❤️ "Beloved BI" ❤️ Let's get started ❤️ (For those who can make charts with Python)
For Debian users who are having trouble with a bug in the Linux kernel 5.10
For those who are in trouble with an error when pip install xg boost
How to set the development environment for each project with VSCode + Python extension + Miniconda
What to do if Python does not switch from the System version in pyenv
For those who should have installed janome properly with Python but get an error
[Memo for Flask] An amateur who does not understand HTML or Web construction tries to create a Web service with Python + Flask