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.
cd
to the project directory.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.
$ 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
There are two ways to install pythonz:
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.
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.
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.
Assuming a "project sandbox" to be developed with Python 2.7.9, let's practice creating a new development environment.
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
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.
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]
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)'
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
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