[PYTHON] Let's claim the possibility of pyenv-virtualenv in 2021

what's this?

In 2021, when everyone uses pipenv and poetry, I realized that pyenv-virtualenv is perfect for a certain purpose, so I will introduce it.

What is pyenv-virtualenv?

It is one of the virtual environment construction tools of python. It is an extension of pyenv. In normal pyenv, the environment is controlled by specifying the version name prepared by the pyenv formula like pyenv local 3.8.7, but if you use pyenv-virtualenv, you can use your favorite environment like pyenv local saishinban. You can specify a name, and you can isolate the environment by this environment name.

There are many articles about pyenv-virtualenv in Qiita, so I'll just give you a rough explanation. If you want to know more, please refer to Official README and other articles.

What did you notice?

You're probably writing in python the strongest CLI tools I've come up with for your hobbies and work. And, it seems that package management is done using poetry etc. in a modern way (2021). Let's say you have a tool like that. So how do you get this CLI tool to work?

  1. docker is supreme
  2. Keep the same poetry/pipenv environment as the development
  3. System python
  4. Other

To give you the answer right away, pyenv-virtualenv is perfect for the others in 4.

No good (1,2,3)

First, if 1, 2 and 3 fail, it will be as follows.

Case problem
1. docker CLI tools often handle local directories and files, which require mounting and authority management, which is troublesome and risky (badly)./Mount and erase all files, etc.)
2.Same as development When using poetry or pipenv, it is necessary to go to that directory first and then operate it, which is troublesome (handling of CLI argument paths is also troublesome).
3.system Where did the dependency management go ...

Well, if you do your best and ignore the risks, you can do it, but I don't want to do anything.

Answer (pyenv-virtualenv)

Create the pyenv-virtualenv environment as follows:

$ pyenv install X.X.X  #Install your favorite version (not required if already installed)
$ pyenv virtualenv X.X.X mytoolenv
$ pyenv shell mytoolenv
$ pip install -e PATH/TO/mytool  #Now install your strongest CLI tool (setup if using poetry).Without py,`pip install -e`Note that cannot be used)

Then, create a file with the same name as the tool name in a directory in your PATH (in the example, the tool name is mytool), write the following contents, and give execute permission.

#! /bin/bash
eval "$(pyenv init -)"
pyenv shell mytoolenv
mytool $*

Now, when you hit mytool, you can run it in a dedicated virtual environment. If you're trying to get rid of setup.py and you can't use pip install -e, it's a good idea to have a script to reinstall.

The appeal of this method is that you can use the tools in any directory and still have a decent virtual environment (not as much as docker).

Summary

There are good cases to use pyenv-virtualenv even in 2021. If you are facing similar troubles, please consider it.

Recommended Posts

Let's claim the possibility of pyenv-virtualenv in 2021
Let's use the open data of "Mamebus" in Python
The story of the "hole" in the file
The meaning of ".object" in Django
Let's decide the winner of bingo
[Python] Let's reduce the number of elements in the result in set operations
[Understanding in 3 minutes] The beginning of Linux
Check the behavior of destructor in Python
The story of an error in PyOCR
Django ~ Let's display it in the browser ~
Implement part of the process in C ++
The basics of running NoxPlayer in Python
Let's summarize the construction of NFS server
In search of the fastest FizzBuzz in Python
Let's investigate the mechanism of Kaiji's cee-loline
Output the number of CPU cores in Python
The meaning of {version-number} in the mysql rpm package
[Python] Sort the list of pathlib.Path in natural sort
Let's parse the git commit log in Python!
Change the font size of the legend in df.plot
Get the caller of a function in Python
View the result of geometry processing in Python
Let's automatically display the lyrics of the song being played on iTunes in Python
Make a copy of the list in Python
[AWS] Let's run a unit test of Lambda function in the local environment
Find the number of days in a month
Read the output of subprocess.Popen in real time
Find the divisor of the value entered in python
The story of finding the optimal n in N fist
setup of pyenv-virtualenv
Fix the argument of the function used in map
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
Put the second axis in 2dhistgram of matplotlib
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Visualized the usage status of the sink in the company
Output in the form of a python array
The story of viewing media files in Django
Search by the value of the instance in the list
Make progress of dd visible in the progress bar
Factfulness of the new coronavirus seen in Splunk
Let's erase the unintended (base) that appears in the terminal [Get out of the Conda environment]
[Django] Let's try to clarify the part of Django that was somehow through in the test
[Machine learning] "Abnormality detection and change detection" Let's draw the figure of Chapter 1 in Python.
Let's get notified of the weather in your favorite area from yahoo weather on LINE!
[Statistics] Let's explain the execution of logistic regression in stan in detail (w / Titanic dataset)
Experience the good calculation efficiency of vectorization in Python
Specify the lighting Model of SCN Material in Pythonista
Numerical representation of days of the week in various languages
Sort in Python. Next, let's think about the algorithm.
Used from the introduction of Node.js in WSL environment
How to get the number of digits in Python
In search of the best random dot stereogram (RDS).
The story of building the fastest Linux environment in the world
Count the number of parameters in the deep learning model
Let's use the API of the official statistics counter (e-Stat)
Let's clear the ambiguity of the hyphen (-) of the su command now! !!
○○ Solving problems in the Department of Mathematics by optimization
Omit the decimal point of the graph scale in matplotlib