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 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/
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.
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'
\ },
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
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
To install by referring to requirements.txt in a new environment, hit the following command.
$ pip3 install -r ~/dotfiles/requirements.txt
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
$ 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)
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
.
Starting with Python 3.6, it becomes python3 -m venv
, and pyvenv
is deprecated.
#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
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.
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)
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