Unification of Python environment

I have summarized the procedure for building a Python environment from 0.

Prior knowledge for those who have used Javascript

To help understanding I will write down the terms and tools that appear below, such as the correspondence with JS. In Javascript

nvm => pyenv
node_modules => pyvenv
package.json => requirements.in, requirements.txt
npm install command=> pip-sync
lint tools=> pytest

It is a correspondence like that.

1. Install pyenv

What is pyenv

Command line tool for versioning Python itself

Installation method

(Install Python 3.5.2 as an example) (Install the git command in advance)

$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv
$ export PATH="$HOME/.pyenv/shims:$HOME/.pyenv/bin:${PATH}"

#Display a list of installable versions
$ pyenv install --list

# 「3.5.2 "part specifies any version
$ pyenv install 3.5.2
$ pyenv global 3.5.2

2. Setting pyvenv

What is pyvenv?

A command line tool for building Python virtual environments. Included by default in Python

What is a virtual environment?

Example: If you don't have Pandas on your server and you don't want to install it on the server itself (you don't want to change the settings on the server itself), but you want to use Pandas By combining "Python environment with Pandas installed" into one directory, Even within the same server

Default=>Pandas can't be used
Under the created virtual environment=>Can use Pandas

And can be used properly. (If you want to delete the virtual environment, just delete the entire directory)

How to create a virtual environment

#A directory called venv is created
$ pyvenv venv

#Enter the virtual environment
$ source venv/bin/activate

#What was installed here is venv/lib/python3.5/site-packages/Managed within
# (Not installed in the default Python environment)
(venv) $ pip install pandas

#Get out of the virtual environment
(venv) $ deactivate

3. Package management

Even if you share the entire virtual environment itself (under the venv directory), the same environment can be shared between different servers, but since the capacity of the virtual environment itself is too large, you usually use the package management tool to "install which package". Write only the information of "Is it?" And share it.

Write the package you want in requirements.in.

#Write the package you want to install
pandas
#If you want to specify the version, write as below
django>=1.8.0,<1.9.0
tweepy==3.3.0
ansible>=1.8.0

Actually install the contents written in requirements.in.

#Installation of required commands
$ pip install pip-tools

# requirements.Create txt
# requirements.In txt, the version of the package to be actually installed is the contents of the dependent package.
$ pip-compile requirements.in

# requirements.Actually install the package with the contents written in txt.
#If it is not installed in the environment, it will be installed, and if the version is different, it will be required to the specified version..If a package not listed in txt is installed, it will be removed.
# --dry-You can also check the installation contents with the run option
$ pip-sync requirements.txt

4. Unification of coding style

#Installation of required packages(Actually requirement.Write in in and pip-Install with sync)
$ pip install pep8 pytest pytest-pep8

# sample.Check py's coding style
$ export PYTHONPATH=. #Required for PATH check such as import
$ py.test --pep8 sample.py

#Output all files that violate the rules under the current directory
$ pep8 . | cut -d: -f 1 | sort | uniq

Use py.test --pep8 sample.py to check if sample.py complies with the PEP8 coding conventions and tell you if there are any violations.

How to automatically fix to comply with PEP8

#Installation of required packages(Actually requirement.Write in in and pip-Install with sync)
$ pip install pep8 autopep8

#Mod the auto-corrected one.Output to py
$ autopep8 sample.py > mod.py

#Automatic correction&Overwrite
$ autopep8 -i sample.py

#Modify the source code under the current directory at once
$ pep8 . | cut -d: -f 1 | sort | uniq | xargs autopep8 -i

Recommended Posts

Unification of Python environment
Environment construction of python2 & 3 (OSX)
Environment construction of python and opencv
Environment construction of python3.8 on mac
Introduction of Python
Python environment construction
python environment settings
python windows environment
Environment construction (python)
Basics of Python ①
Basics of python ①
python environment construction
Copy of python
Python --Environment construction
Python environment construction
python environment construction
Introduction of Python
About the virtual environment of python version 3.7
[Python] Operation of enumerate
List of python modules
[Python] Get environment variables
My python environment memo
Prepare the execution environment of Python3 with Docker
python windows environment construction
Copy of python preferences
Basics of Python scraping basics
homebrew python environment construction
[python] behavior of argmax
Create an environment of 64bit Windows + python 2.7 + MeCab 0.996
Python development environment construction
Usage of Python locals ()
the zen of Python
Use multiple versions of python environment with pyenv
python virtual environment Pipenv
Installation of Python3 and Flask [Environment construction summary]
Installation of Python 3.3 rc1
About Python development environment
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
virtual environment in python
python development environment -use of pyenv and virtualenv-
Python environment with docker-compose
# 4 [python] Basics of functions
python2.7 development environment construction
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Create a Python environment
Development environment in Python
Mac environment construction Python
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Virtual environment with Python 3.6
Python environment construction @ Win7
Various processing of Python
Comparison of Python and Ruby (Environment / Grammar / Literal)
Python environment for projects
Operate mongoDB from python in ubuntu environment ① Introduction of mongoDB
Recommendation of building a portable Python environment with conda
Summary of python environment settings for myself [mac] [ubuntu]
Runtime version of Google App Engine / Python Standard Environment