1: posts
Reading ModeRO
$ cat 20200524_pythoninst.md

#Setting up a Python development environment

Date:2020-05-26 (Tue)
>Managing Python versions and virtual environments with pyenv and pyenv-virtualenv, then managing project dependencies with Poetry.

This time I’m setting up a Python development environment.
Python is my main language. After trying several setups, this is the one I find easiest to work with.

ℹ️ Info: In this case, we will be dealing with environment setup for Ubuntu 18.04 LTS.

##In this article

  • The installation of pyenv/pyenv-virtualenv
  • The installation of package management tool Poetry

##Choosing a Python environment

What I found most confusing at first was how many different ways there are to set up Python.

In fact, there is a way to use apt for building the most basic Python environment.
In the case of Ubuntu 18.04LTS, I think this is installed from the beginning.
Is it the case that for MacOS, the method involves using homebrew (I haven't used Macs much, so I'm not very familiar with it).

Bash
# For Python 2
sudo apt install python

# For Python 3
sudo apt install python3

There are no particular problems with running Python code related to the OS system, but it is not appropriate to use this Python environment when creating code yourself.
The reason is as follows.

  • Because you cannot create a virtual environment, you are likely to encounter inconvenient problems.
  • I am not able to change the Python version freely.

If you've ever touched Python even a little, you'll understand, but if you keep reinstalling and uninstalling Python packages, the version dependencies can get messed up and things can go completely wrong.
Therefore, it is not recommended to install the package yourself in the above environment.

Installing packages in the above default environment is very risky.
Therefore, it is essential that the development environment in Python can be able to build virtual environments.
(If it's a virtual environment, you can delete the environment that has become weird and start over again.)

The most famous one is Anaconda (I also used this initially when I started).

If there's no particular preference, honestly I think just putting Anaconda in would be enough (quite serious).
It's easy to do various things such as creating a virtual environment in the GUI or installing packages.
However, if it's like this, the article will end, so I'll list the disadvantages anyway.

  • Many packages cannot be installed using the conda command alone.
  • When you install the same package using both pip and conda, the behavior becomes strange.
  • I cannot set up a virtual environment for each directory

For these reasons, I stopped using Anaconda.
And I am currently using pyenv.
I think there are other methods as well, but this environment is pretty convincing.
(Until a while ago, there were some packages that were difficult to install without using conda, such as PyTorch, but I feel that they have decreased quite a bit recently.)

##Setting up pyenv and pyenv-virtualenv

###Installation

Let's quickly get pyenv set up.
We also introduce the plugin that creates virtual environments simultaneously, pyenv-virtualenv.
First, execute the following command to clone the repository.

Bash
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

Next, add the following description to .profile or .zshenv.

Bash
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
    eval "$(pyenv init -)"
fi
eval "$(pyenv virtualenv-init -)"

if [[ -n $VIRTUAL_ENV && -e "${VIRTUAL_ENV}/bin/activate" ]]; then
    source "${VIRTUAL_ENV}/bin/activate"
fi

Then, restart the terminal or reload it using the source command.
You can display the pyenv version with the following command.

Bash
pyenv --version
> pyenv 1.2.16

###Building a specific Python version

Next, build the version of Python you prefer.
By the way, you can check the versions that can be built using the following command.

Bash
pyenv versions

This time, let's build 3.7.6 as an example.
Run the following command.
Installation may take a while.

Bash
CONFIGURE_OPTS="--enable-shared" CFLAGS="-fPIC" pyenv install 3.7.6

If you don't specify the above options when building, you will get stuck in various places.
(I think there was a problem when I used OpenCV or something. It's something from the past, so I don't remember...)

After the build is complete, specify the Python version to use as the default.
There are two ways to specify the Python version to be used in pyenv: global and local.
In global, you can specify the version to be used as the default, and in local, you can specify the version to be used in that directory.

Specify the 3.7.6 version that I just built as the default version.

Bash
pyenv global 3.7.6

Let's check it out.

Bash
python -V
> Python 3.7.6

It should be okay if it is displayed like this.

###Creating a virtual environment

Next, we will create the virtual environment.
It's very easy to create.
Using the version 3.7.6 that we just built, let's try to create a virtual environment named sample.

Bash
pyenv virtualenv 3.7.6 sample

After creating it, run the following command in the directory of the project you want to use this virtual environment for.

Bash
pyenv local sample

With this, you will be able to use the virtual environment sample within this directory.
By the way, if you want to temporarily use a specific virtual environment, you can use the following command.

Bash
# Activate the virtual environment
pyenv activate sample
# Leave the virtual environment
pyenv deactivate

##Setting up Poetry

Here, we will introduce the package management tool Poetry.

Poetry is a package management software that plays the role of managing package dependencies.
In addition to Poetry, it can also be used for package building and publishing.

Similar software such as Pipenv and Pyflow exist.
I have not used these other software, so I will not compare them, but I am using Poetry because Pipenv cannot be used for building or publishing, Pyflow is more feature-rich and can manage the Python version, but it is still in its development stage, so there is less information.
Since managing the Python version is handled by pyenv-virtualenv as mentioned above, there are no particular problems if you combine them.

###How to install it

The installation is just as easy for this one.

Bash
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

The command should display the version if it works.

Bash
poetry --version
> Poetry version 1.0.5

If the poetry command cannot be executed, add the following description to .profile or .zshenv.

Bash
export PATH="$HOME/.poetry/bin:$PATH"

###Enabling shell completion

Poetry can easily set up shell command completion.
Depending on the environment, you just need to run the following commands.

Bash
# Bash
poetry completions bash > /etc/bash_completion.d/poetry.bash-completion

# Bash (Homebrew)
poetry completions bash > $(brew --prefix)/etc/bash_completion.d/poetry.bash-completion

# Fish
poetry completions fish > ~/.config/fish/completions/poetry.fish

# Fish (Homebrew)
poetry completions fish > (brew --prefix)/share/fish/vendor_completions.d/poetry.fish

# Zsh
poetry completions zsh > ~/.zfunc/_poetry

# Zsh (Homebrew)
poetry completions zsh > $(brew --prefix)/share/zsh/site-functions/_poetry

This should enable command completion.

###Creating a project

Poetry manages the project using two files: pyproject.toml and poetry.lock.
In this case, only pyproject.toml is actually manipulated, and poetry.lock will be automatically updated as necessary.

First, run the following command in the directory where you want to create the project.

Bash
poetry init

Then, they will ask for the project settings in a dialogue format, so we will enter them in order.

  • Package name : Name of the Package
  • Version : Version
  • Description: Overview of the package
  • Author: Developer Name
  • License : License
  • Compatible Python versions : Compatible versions of Python

Poetry then asks twice about dependencies. Answer No to both prompts; we’ll add packages later.
Finally, you will be asked to confirm with Do you confirm generation?, so answer with Yes.
The project is now complete.

###Installing packages

We install packages into the project.
At this point, switching to a virtual environment with pyenv local will install packages into that virtual environment.
To install the package, simply execute the following command.

Bash
# Install numpy
poetry add numpy

# Install matplotlib
poetry add matplotlib

When you install the package using the above command, the package will also be added to pyproject.toml.
In that case, the version of the installed packages and the version of dependent packages, etc., will be automatically listed in poetry.lock.

This keeps dependencies I explicitly requested separate from the ones those packages bring in.
Therefore, if you want to uninstall, you can run the following command, which will uninstall not only the dependent packages but also the ones you want to uninstall.

Bash
# Uninstall matplotlib
poetry remove matplotlib

Additionally, if you obtain the projects managed by Poetry (pyproject.toml and poetry.lock are included in the directory) from outside, you can install the required packages together by running the following command.

Bash
poetry install

I have covered the basic usage of Poetry in detail.
Poetry can also build and publish packages to PyPI. I’ll save that part for another post.

##Wrapping up

That’s my Python setup with pyenv, pyenv-virtualenv, and Poetry.
I’d like to cover publishing packages and configuring my editor separately.

## Comments

"20200524_pythoninst.md" [readonly]2L, 10074B
utf-8100%