I have summarized the procedure for building a Python environment from 0.
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.
pyenv
pyenv
Command line tool for versioning Python itself
(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
pyvenv
pyvenv
?A command line tool for building Python virtual environments. Included by default in Python
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)
#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
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.
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
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
#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.
#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