Build Python development environment (pythonz, virtualenv, direnv)

Thing you want to do

Build an environment for developing application programs in Python.

For the time being, set the goals as follows.

Traditionally, pyenv-virtualenv has been used for this purpose.

pyenv-virtualenv is convenient, but I didn't like how to give command arguments, so this time pythonz, [virtualenv] ](Https://virtualenv.pypa.io/), direnv will be used in combination. Using direnv also has the advantage of being useful for building environments other than Python.

Role of each tool

1. Installation procedure

1-0 Preparation for introduction

I assume a Linux environment, but I think that the procedure is almost the same for other OSs. I'm using Debian 7 (Wheezy). When using this method on Windows, it is recommended to create a virtual environment with vagrant etc. As a premise, the following preparations are assumed.

Preparation example:


$ sudo apt-get install build-essential zlib1g-dev libbz2-dev libssl-dev libreadline-dev libncurses5-dev libsqlite3-dev libgdbm-dev libdb-dev libexpat-dev libpcap-dev liblzma-dev libpcre3-dev curl python-pip

$ sudo yum install zlib-devel bzip2-devel openssl-devel readline-devel ncurses-devel sqlite-devel gdbm-devel db4-devel expat-devel libpcap-devel xz-devel pcre-devel curl
$ curl -L https://bootstrap.pypa.io/get-pip.py | sudo python

1-1 Installation of pythonz

There are two ways to install pythonz:

  1. Install as a user with administrator privileges
  2. Install as a general user

Here, install with the authority of a general user.


$ curl -kL https://raw.github.com/saghul/pythonz/master/pythonz-install | bash
$ echo '[[ -s $HOME/.pythonz/etc/bashrc ]] && source $HOME/.pythonz/etc/bashrc' >> $HOME/.bashrc
$ exec $SHELL

This way, when a new Python is installed, it will be installed in each user's home directory.

1-2 Installation of virtualenv

It is recommended to use pip to install virtualenv.


$ sudo pip install virtualenv

If you cannot use pip or install as a general user, refer to the procedure on the Official Site and install from the source code. It would be better to do it.

1-3 Installation of direnv

Executable files are distributed for each OS and CPU type on direnv official github site. Download the appropriate executable file and copy it to a directory in your PATH.


$ curl -L -o direnv https://github.com/zimbatm/direnv/releases/download/v2.5.0/direnv.linux-amd64
$ sudo install direnv /usr/local/bin
$ echo 'type direnv > /dev/null 2>&1 && eval "$(direnv hook bash)" ' >> $HOME/.bashrc
$ exec $SHELL

Here, it is installed by a user with administrator privileges, but you can install it in the user's home directory as long as it is in your PATH.

If you can use Go language, it is easy to install from the source code.

2. Build a Python development environment

Assuming a "project sandbox" to be developed with Python 2.7.9, let's practice creating a new development environment.

2-1 Installation of Python 2.7.9

Check the version of Python that can be installed with pythonz list -a, and then specify the version to install. Please wait patiently as the installation will take some time.


$ pythonz list -a 
# Available Python versions
  # cpython:
     2.4
     2.4.1
     2.4.2
     2.4.3
... 

$ pythonz install 2.7.9

You can find a list of installed Python with pythonz list.


$ pythonz list
# Installed Python versions
  CPython-2.7.9

You can find the location of the installed Python interpreter with pythonz locate version number.


$ pythonz locate 2.7.9
/home/jnotoya/.pythonz/pythons/CPython-2.7.9/bin/python

If you want to run the installed Python directly, you can use the shell's command substitution feature and write something like $ (pythonz locate version number).


$ $(pythonz locate 2.7.9) --version
Python 2.7.9
$ $(pythonz locate 2.7.9) -c 'print("Hello, world")'
Hello, world

2-2 Creating and setting the project directory

Create a directory to develop the project in it. You can freely decide the name and location of the project directory. Here, create a project directory with the name "sand_box" in the user's home directory.


$ pwd
/home/jnotoya
$ mkdir sand_box

After creating the project directory, create the directory configuration file .envrc. The configuration file can be created and edited with the direnv edit command, but here it is easily created from the command line.


$ echo 'layout python $(pythonz locate 2.7.9)' > sand_box/.envrc
$ direnv allow sand_box

After layout python, use the pythonz feature to specify the location of the Python interpreter as$ (pythonz locate version number). Since .envrc is passed to the shell, you can use the shell's command substitution function$ (...).

Check the direnv manual page etc. for how to write the configuration file.

2-3 Initialization of development environment

The first time you visit the project directory you created, virtualenv will run automatically to initialize the development environment. The initialization process copies the Python interpreter installed with pythonz into the project directory.


$ cd sand_box
direnv: loading .envrc
Running virtualenv with interpreter /home/jnotoya/.pythonz/pythons/CPython-2.7.9/bin/python
New python executable in /home/jnotoya/sand_box/.direnv/python-2.7.9/bin/python
Installing setuptools, pip...direnv: ([direnv export bash]) is taking a while to execute. Use CTRL-C to give up.
done.
direnv: export +VIRTUAL_ENV ~PATH

Running the python command in the project directory runs the appropriate Python interpreter that was copied during the initialization process, instead of the original Python on the system.


$ pwd
/home/jnotoya/sand_box
$ type python
python is /home/jnotoya/sand_box/.direnv/python-2.7.9/bin/python
$ python -c 'import sys;print(sys.version)'
2.7.9 (default, Jan 31 2015, 08:18:30) 
[GCC 4.7.2]

2-4 Library installation

Install the libraries used by your project with the pip command. If you run pip in the project directory, the library will be installed in the project directory for use only in this environment and will not affect the entire system, so you can easily install the library.

Here, I will install the PrettyTable library.

$ pwd
/home/jnotoya/sand_box
$ pip install PrettyTable
$ python -c 'import prettytable as pt; help(pt.PrettyTable)'

2-5 Suspension of development work

You can interrupt your development work by exiting the project directory. Outside the project directory, you can still use the Python interpreter provided on your system.


$ pwd
/home/jnotoya/sand_box
$ cd ..
direnv: unloading
$ type python
python is /usr/bin/python
$ python --version
Python 2.7.3

2-6 Daily work

It's very easy because you can usually do development work without being conscious of switching environments. There is no need to switch the environment with the workon and deactivate commands like virtualenvwrapper. If it doesn't work, make sure that somewhere in .bashrc there is a description like the following that reads the settings of pythonz and direnv.

.bashrc


[[ -s $HOME/.pythonz/etc/bashrc ]] && source $HOME/.pythonz/etc/bashrc
type direnv > /dev/null 2>&1 && eval "$(direnv hook bash)" 

If you rename the project directory after initialization, you will need to make some adjustments. Please be careful.

Recommended Posts

Build Python development environment (pythonz, virtualenv, direnv)
Build python environment with direnv
[ev3dev × Python] Build ev3dev development environment
I tried to build a Mac Python development environment with pythonz + direnv
Build python virtual environment with virtualenv
Clean python environment with pythonz and virtualenv
Python development environment construction
About Python development environment
python2.7 development environment construction
Development environment in Python
Build Python development environment with Visual Studio Code
[Python] Build a Django development environment with Docker
Build a Python development environment on your Mac
python development environment -use of pyenv and virtualenv-
Build a Python development environment on Raspberry Pi
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Manage python environment with virtualenv
Build python3 environment with ubuntu 16.04
Build a GVim-based Python development environment on Windows 10 (1) Installation
Build Python environment on Windows
Organize your Python development environment
Build a Python development environment on Mac OS X
Build a machine learning application development environment with Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Build python environment on windows
Build a Python development environment using pyenv on MacOS
[For organizing] Python development environment
Build a Python environment offline
python memo (for myself): About the development environment virtualenv
Build an Ubuntu python development environment on Google Cloud Platform
Build a Python development environment in Eclipse (add HTML editor)
Build a GVim-based Python development environment on Windows 10 (2) Basic settings
How to build a python2.7 series development environment with Vagrant
UpNext2 Development Record # 1 Build Python CI environment in VS Code
Build Mysql + Python environment with docker
Prepare your first Python development environment
[Python3] Development environment construction << Windows edition >>
Python development environment options for May 2020
Python development environment construction on macOS
Vim + Python development environment setting memo
Emacs settings for Python development environment
Emacs Python development environment construction memo
Checking the NAOqi Python development environment
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
Build a python3 environment on CentOS7
Prepare Python development environment with Atom
Build a local development environment for Lambda + Python using Serverless Framework
Try using virtualenv, which can build a virtual environment for Python
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Build python 1.0
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
Python development environment for macOS using venv 2016
Easily build a development environment with Laragon
Build Jupyter Lab (Python) environment with Docker
[Development environment] Python with Xcode [With screen transition]
Python 2.7, 3.4, 3.5 extension module build environment on Windows
Method to build Python environment in Xcode 6
Blender 2.82 or later + python development environment notes
Build Python3 and OpenCV environment on Ubuntu 18.04
Build a python environment on MacOS (Catallina)
I want to build a Python environment