Introducing vebootstrap to handle virtualenv more easily in Python development environment

As of early 2018, Pipenv is recommended. I've noticed that the number of libraries that provide binaries for Windows has increased considerably, so (Mini | Ana) Conda shouldn't be a problem.


Speaking of Python development environment these days, I think the following patterns are the mainstream.

Once you're ready, you'll have a flexible environment, but there are many steps and you'll need to deploy a lot of things other than python. Also, some are difficult to deploy on Windows. So, I made a pure python package that makes it easier to handle the virtualenv environment for each project.

Motivation

Originally, the ideal was that it was easy to set up, just set python hoge.py like normal script execution, the dedicated virtualenv environment was activated, and the necessary packages were included. The script was to launch. Simply put, the package that achieves this is the vebootstrap created this time.

vebootstrap https://pypi.python.org/pypi/vebootstrap https://github.com/tomoemon/vebootstrap

I don't really care about that because I don't switch versions of python that much when writing a little personal script (I should be able to use it in combination with pythonz etc. if necessary).

Operating environment

What you can do

Things impossible

setup

Install vebootstrap using pip that corresponds to the python you plan to use. If you are using the system standard python in a Linux environment, you will need sudo.

$ pip install vebootstrap

How to use

Change the following directory names and script file names as appropriate. However, do not modify requirements.txt as vebootstrap will look for a file with that name. Also, in the sample, it is executed after cd in the project directory, but the same behavior can be obtained even if it is executed from outside the directory.

** Simplest pattern **

Directory structure


project_test1/
    hoge.py

hoge.py


import vebootstrap

print("Hello vebootstrap")

Command execution


$ cd project_test1
$ python hoge.py

If you put ʻimport vebootstrap at the beginning of the main script hoge.pyand start it, it will be called.py ## (##is the python version) in theproject_test1directory at the first startup. Create a directory for your virtualenv environment. After that, executehoge.py with the virtualenv environment active, and activate and execute the existing .py ## `on the second and subsequent startups.

** If there are dependent packages **

Directory structure


project_test2/
    hoge.py
    requirements.txt

hoge.py


import vebootstrap
import requests

print(requests.get('http://yahoo.co.jp').text[:100])

requirements.txt


requests

Command execution


$ cd project_test2
$ python hoge.py

The same goes for creating a directory for the virtualenv environment the first time you start hoge.py, and use requirements.txt to pip install the dependent packages. Then activate the virtualenv environment and run hoge.py. If you rewrite requirements.txt, recreate the virtualenv environment at the next startup. (Since it may be difficult to uninstall individual packages, we use such specifications.)

** (Windows only) If you want to install a binary package for Windows **

For Windows users, installing packages that require their own build, such as numpy and lxml, can be a daunting task. You'll have to find and manually install the pre-built packages using something other than pip (PyPI), but vebootstrap makes this easy.

Specifically, I try to find the target package from the following site and install it. http://www.lfd.uci.edu/~gohlke/pythonlibs/

Directory structure


project_test3/
    hoge.py
    requirements.txt

hoge.py


import vebootstrap
import numpy

print(dir(numpy))

requirements.txt


#windows:numpy

Command execution


$ cd project_test3
$ python hoge.py

The usage is almost the same as before, but the description of requirements.txt is a little special. If there is a line of the form # windows: hogehoge, vebootstrap will look for the hogehoge package from the previous site. For pip, lines starting with # are treated as comments, so this is only interpreted by vebootstrap.

You can also install the packages that are installed by pip standard and the binaries for Windows as shown below.

requirements.txt


requests
#windows:numpy
beautifulsoup4

I don't know which package you have to install pre-built with this special writing! If you are, you should write hogehoge and requirements.txt as usual first, and if you get an unclear error, write # windows: hogehoge.

** If you don't want to include irrelevant import statements **

ʻImport vebootstrap` is the minimum magic to activate the virtualenv environment just by running python, but it may not be necessary if you are writing a script to expose to the outside world. In that case, please use the function to execute the module as a script as shown below. It behaves in the same way as when an import statement is inserted.

Directory structure


project_test4/
    hoge.py
    requirements.txt

hoge.py


import requests

print(requests.get('http://yahoo.co.jp').text[:100])

requirements.txt


requests

Command execution


$ cd project_test4
$ python -m vebootstrap hoge.py

** If it's a hassle to write dependent packages for most projects every time **

If you are a frequent project creator and write the same requirements.txt every time, try using the following functions.

Using vebootstrap even once creates a configuration file called .vebootstrap under your home directory (cd% USERPROFILE% for Windows users).

$HOME/.vebootstrap


{
    "default_packages": [
        "ipython"
    ], 
    "default_windows_packages": []
}

This file specifies the packages that should be installed in all projects regardless of the contents of requirements.txt. By default, the ʻipythonpackage is installed so that the REPL of the virtualenv environment can be used immediately. Add other packages as needed. Binaries for Windows can be installed automatically by adding them todefault_windows_packages in the form "numpy" `.

Example when adding some

$HOME/.vebootstrap


{
    "default_packages": [
        "ipython",
        "requests"
    ], 
    "default_windows_packages": [
        "numpy"
    ]
}

Notes

Note that the virtualenv environment is created for each version of python you run. When you type the following command, for example, if the version of python is 2.7.x, create a virtualenv environment with the name .py27 (the python executable file copied internally is 2.7.x. Things).

$ python hoge.py

If you execute the following command in the same directory, a virtualenv environment named .py35 will be created. (If the version of python3 is 3.5.x)

$ python3 hoge.py

at the end

Since my main environment is Windows, it may be difficult for Linux main people to use it. Please point out if you say "it cannot be used in such cases".

And although the package name vebootstrap is a bit long (and a little hard to type) personally, both ve and veb have already been taken ...

Recommended Posts

Introducing vebootstrap to handle virtualenv more easily in Python development environment
Development environment in Python
Handle environment variables in Python
How to handle Japanese in Python
How to access environment variables in Python
Method to build Python environment in Xcode 6
How to prepare Python development environment [Mac]
Build Python development environment (pythonz, virtualenv, direnv)
python in virtualenv
Introduction to Python Let's prepare the development environment
Use cryptography module to handle OpenSSL in Python
python development environment -use of pyenv and virtualenv-
Work in a virtual environment with Python virtualenv.
How to handle datetime type in python sqlite3
Use os.getenv to get environment variables in Python
App development to tweet in Python from Visual Studio 2017
Python development environment construction
I want to easily build a model-based development environment
About Python development environment
virtual environment in python
Handle markdown in python
Python development environment construction 2020 [From Python installation to poetry introduction]
Introducing Python 2.7 to CentOS 6.6
python2.7 development environment construction
Handle Parquet in Python
Framework development in Python
python memo (for myself): About the development environment virtualenv
Slackbot development in Python
How to handle JSON in Ruby, Python, JavaScript, PHP
Commands often used in the development environment during Python implementation
Build a Python development environment in Eclipse (add HTML editor)
Video cannot be loaded with Spyder in Python development environment
How to build a python2.7 series development environment with Vagrant
How to get into the python development environment with Vagrant
Introducing a good way to manage DB connections in Python
UpNext2 Development Record # 1 Build Python CI environment in VS Code
Easily format JSON in Python
To flush stdout in Python
Manage python environment with virtualenv
Login to website in Python
Organize your Python development environment
[ev3dev × Python] Build ev3dev development environment
Python development in Visual Studio 2017
Speech to speech in python [text to speech]
[MEMO] [Development environment construction] Python
[For organizing] Python development environment
Introducing GUI: PyQt5 in Python
How to develop in Python
Python development in Visual Studio
[Python] How to use virtualenv
Handle complex numbers in Python
Post to Slack in Python
[Windows 10] [Development environment construction] How to resolve when unresolved import'django.xxx' python (unresolved-import) occurs in VS Code
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
How to handle multiple versions of CUDA in the same environment
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
LINEbot development, I want to check the operation in the local environment
From installing Ansible to building a Python environment in Vagrant's virtual environment
[Python] How to do PCA in Python
Build python virtual environment with virtualenv
How to collect images in Python