Python development environment for macOS using venv 2016

Also, since it became an environment to write Python in the development, I prepared a Python environment because it is a good idea. I mainly use it for Web development + a little script, and now I don't handle 2 system code anymore, so I stopped using pyenv and pyenv-virtualenv and changed to using venv. (Addition: 2017/12/29 It is still under development in this environment as of 2017.)

Environment: OS X EL Capitan, High Sierra

Uninstall pyenv, pyenv-virtualenv

Uninstall pyenv and pyenv-virtualenv according to Flowchart whether pyenv is required --Qiita. See the link for details, but it seems that pyenv is unnecessary for those who arrived at this article looking for a way to build a development environment.

I used to install .python-version files using pyenv in the past, but I can't specify something like 3.5. *, So every time I update a minor version from 3.5.1 to 3.5.2. I was rewriting python-version. So, .python-version is a file that shows the executable version, so of course I managed it with git, but every time I upgrade, an extra history of git is added, and if I try to execute it after a long time, the old version I had to install Python or rewrite .python-version before I could run it with a version error.

However, in the first place, only 3 systems are currently handled by individuals, and there is no need for Python version control. In addition, I decided that it is not necessary to use pyenv because the version and package management of the team products can be managed with Dockerfile and others. Thank you for your help during the transition period. Thank you pyenv.

$ brew uninstall --force pyenv
$ brew uninstall --force pyenv-virtualenv
$ rm -rf .pyenv/

Imadoki's Python 3 series installation & upgrade

Install with homebrew

$ brew install python3

To call Python3, python3, pip becomes pip3. If you are prompted to upgrade pip3,

$ pip3 install --upgrade pip

After that, you can do a brew upgrade even when you feel like it.

$ brew upgrade

Vim

These are the third plugins related to Python development. Detailed usage is left to the official document and omitted.

Specifying execution commands in quickrun.vim

Development uses only Python 3 series, but the python command executes system python (2.7 series in osx), so the execution command in quickrun.vim specifies to execute 3 series.

let g:quickrun_config = {
  \ 'python': {
  \   'command': 'python3'
  \ },

Install only the packages required globally in the global environment

In a global environment, install only the minimum number of packages. Here, flake8, which is necessary for using with an editor, is installed as an example. Other necessary packages are installed on the Docker or venv side, so do not install them globally.

$ pip3 install flake8
Collecting flake8
  Downloading flake8-3.0.4-py2.py3-none-any.whl (64kB)
    100% |████████████████████████████████| 71kB 320kB/s
Collecting pycodestyle<2.1.0,>=2.0.0 (from flake8)
  Using cached pycodestyle-2.0.0-py2.py3-none-any.whl
Collecting mccabe<0.6.0,>=0.5.0 (from flake8)
  Downloading mccabe-0.5.2-py2.py3-none-any.whl
Collecting pyflakes!=1.2.0,!=1.2.1,!=1.2.2,<1.3.0,>=0.8.1 (from flake8)
  Using cached pyflakes-1.2.3-py2.py3-none-any.whl
Installing collected packages: pycodestyle, mccabe, pyflakes, flake8
Successfully installed flake8-3.0.4 mccabe-0.5.2 pycodestyle-2.0.0 pyflakes-1.2.3

Keep globally installed package management files in dotfiles

Package management file output

The pip3 freeze command can output a list of installed packages, so use that.

$ pip3 freeze
flake8==3.0.4
mccabe==0.5.2
pycodestyle==2.0.0
pyflakes==1.2.3

Python usually outputs it as requirements.txt, so follow the convention.

#Requirements for package list.Output to txt
$ pip3 freeze > ~/dotfiles/requirements.txt

Install from package management file

To install by referring to requirements.txt in a new environment, hit the following command.

$ pip3 install -r ~/dotfiles/requirements.txt

Note: Bulk package update

If you want to update all at once, there is a command like the following, so I registered it as an alias.

alias pip3_update_all='pip3 freeze --local | grep -v "^\-e" | cut -d = -f 1 | xargs pip3 install -U'

Reference: Batch update with pip --Qiita

Note: About python / python3, pip / pip3 commands

$ python is system-dependent Python 2.7.10 and $ python3 is Python 3 installed by Homebrew. Both $ pip and $ pip3 are associated with Python 3 installed with Homebrew.

# python,python3 path,Check version
$ where python
/usr/bin/python

$ where python3
/usr/local/bin/python3

$ python -V
Python 2.7.10

$ python3 -V
Python 3.5.2

# pip,pip3 path,Check version
$ where pip
/usr/local/bin/pip

$ where pip3
/usr/local/bin/pip3

$ pip -V
pip 9.0.0 from /usr/local/lib/python3.5/site-packages (python 3.5)

$ pip3 -V
pip 9.0.0 from /usr/local/lib/python3.5/site-packages (python 3.5)

use venv

Docker is basically used for Web development, but when using the local environment for script execution etc., venv (virtualenv is incorporated into Python) added in Python 3.3 is used as needed. Build a virtual environment using it. The command is pyvenv.

Postscript: 2017-05-31

Starting with Python 3.6, it becomes python3 -m venv, and pyvenv is deprecated.

Create a virtual environment

#Build a virtual environment with the name myenv
# Python 3.5 or earlier
$ pyvenv myenv
# Python 3.6 or later
$ python3 -m venv myenv



# activate
$ source myenv/bin/activate

# deactivate
$ deactivate

Try enabling the virtual environment and installing the package in the virtual environment.

In a virtual environment, install only the packages required for each environment.

#Enable virtual environment
$ source myenv/bin/activate 

#Try installing flask
(myenv) $ pip install flask
Collecting flask
  Downloading Flask-0.11.1-py2.py3-none-any.whl (80kB)
    100% |████████████████████████████████| 81kB 2.3MB/s
Collecting Werkzeug>=0.7 (from flask)
  Downloading Werkzeug-0.11.11-py2.py3-none-any.whl (306kB)
    100% |████████████████████████████████| 307kB 2.5MB/s
Collecting click>=2.0 (from flask)
  Using cached click-6.6.tar.gz
Collecting itsdangerous>=0.21 (from flask)
  Downloading itsdangerous-0.24.tar.gz (46kB)
    100% |████████████████████████████████| 51kB 3.8MB/s
Collecting Jinja2>=2.4 (from flask)
  Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB)
    100% |████████████████████████████████| 266kB 2.4MB/s
Collecting MarkupSafe (from Jinja2>=2.4->flask)
  Downloading MarkupSafe-0.23.tar.gz
Installing collected packages: Werkzeug, click, itsdangerous, MarkupSafe, Jinja2, flask
  Running setup.py install for click ... done
  Running setup.py install for itsdangerous ... done
  Running setup.py install for MarkupSafe ... done
Successfully installed Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.11 click-6.6 flask-0.11.1 itsdangerous-0.24

#Check the package list(The package list of myenv virtual environment is output)
(myenv) $ pip freeze
click==6.6
Flask==0.11.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
Werkzeug==0.11.11

#Disable virtual environment
$ deactivate

#Check the package list(The package list of the global environment is output)
$ pip3 freeze
flake8==3.0.4
mccabe==0.5.2
pycodestyle==2.0.0
pyflakes==1.2.3

Reference: 28.3. venv — Creating a virtual environment — Python 3.5.2 documentation

It was confirmed that the virtual environment can be switched without any problem.

Note: About python / python3, pip / pip3 commands on venv

In the virtual environment, the command without 3 also points to the same version, so there is no problem even if you hit the command without 3.

# python,python3 path,Check version
(myenv) $ where python
/Users/user_name/myenv/bin/python
/usr/bin/python

(myenv) $ where python3
/Users/user_name/myenv/bin/python3
/usr/local/bin/python3

(myenv) $ python -V
Python 3.5.2

(myenv) $ python3 -V
Python 3.5.2


# pip,pip3 path,Check version
(myenv) $ where pip
/Users/user_name/myenv/bin/pip

(myenv) $ where pip3
/Users/user_name/myenv/bin/pip3

(myenv) $ pip -V
pip 9.0.0 from /Users/user_name/pyvenv/lib/python3.5/site-packages (python 3.5)

(myenv) $ pip3 -V
pip 9.0.0 from /Users/user_name/pyvenv/lib/python3.5/site-packages (python 3.5)

Summary

pyenv, pyenv-Prepared a clean Python environment without virtualenv. From now on, Python will be updated with brew upgrade, and if you move to a new environment, you can just do pip3 install -r ~ / dotfiles / requirements.txt.

Recommended Posts

Python development environment for macOS using venv 2016
Python3 + venv + VSCode + macOS development environment construction
[For organizing] Python development environment
Build a Python development environment using pyenv on MacOS
Python development environment options for May 2020
Python development environment construction on macOS
Emacs settings for Python development environment
Using venv in Windows + Docker environment [Python]
Building a Python development environment for AI development
Build a local development environment for Lambda + Python using Serverless Framework
Python development environment construction
About Python development environment
python2.7 development environment construction
Development environment in Python
Python environment for projects
Procedure for creating a Python quarantine environment (venv environment)
Directory structure for test-driven development using pytest in python
Development environment suitable for ArcPy
Notes on creating a python development environment on macOS Catalina
OpenCV3 installation for Python3 @macOS
Python environment construction For Mac
Python3 environment construction (for beginners)
Organize your Python development environment
Build a Python virtual environment using venv (Django + MySQL ①)
[ev3dev × Python] Build ev3dev development environment
QGIS3 Python plugin development environment construction with VSCode (macOS)
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
venv: Python virtual environment management
[MEMO] [Development environment construction] Python
python standard virtual environment venv
python memo (for myself): About the development environment virtualenv
Memo for building a machine learning environment using Python
python3.8 venv environment jupyter notebook
Build an environment for machine learning using Python on MacOSX
Building a development environment for Android apps-creating Android apps in Python
Explosive speed! Using Python Simple HTTP Server for kintone development
Smooth Python development environment for teams [Poetry + pyenv + black + isort]
Construction of Cortex-M development environment for TOPPERS using Raspberry Pi
Until building a Python development environment using pyenv on Ubuntu 20.04
Prepare Python development environment on Ubuntu
[For beginners] Django -Development environment construction-
Prepare your first Python development environment
[Python3] Development environment construction << Windows edition >>
[TouchDesigner] Tips for for statements using python
Vim + Python development environment setting memo
Install Python development environment on Windows 10
Python3 TensorFlow for Mac environment construction
[Python] Reasons for overriding using super ()
Emacs Python development environment construction memo
Checking the NAOqi Python development environment
[Python] Multiplication table using for statement
Prepare Python development environment with Atom
Python3.6 environment construction (using Win environment Anaconda)
Try using virtualenv, which can build a virtual environment for Python
venv (Python)
Notes for using OpenCV on Windows10 Python 3.8.3.
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
Build an environment for Blender built-in Python
Addition of local development environment on MacOS
[Development environment] Python with Xcode [With screen transition]
Manage multiple execution environments using Python venv