This article is the 17th day article of Hamee Advent Calendar 2015.
I've summarized the steps to build a Python
development environment using pythonz
and direnv
on Mac
, mainly as a work memo for myself.
Here, as an example, we will configure the directory ~ / dev / myproject
to use version 3.5.0
of Python
. Also, since it explains the procedure for building the environment with bash
, please replace it with the shell you are using if necessary.
pythonz
?A tool that allows you to install different versions of Python
in your home directory. Please see here for details.
direnv
It is a tool for switching the environment for each directory. Please see here for details.
There are some tools and libraries required to use pythonz
and direnv
, so please install the following in advance.
You need a command line tool to use pythonz
.
$ xcode-select --install
Please execute and select [Install] to install.
homebrew
Since we use homebrew
to install direnv
,
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Please run to install homebrew
.
virtualenv
Because you need a Python
module called virtualenv
to run direnv
$ sudo pip install virtualenv
Please run to install virtualenv
. If pip
is not available, go ahead
$ sudo easy_install pip
Please run to install pip
.
pythonz
and direnv
pythonz
$ curl -kL https://raw.github.com/saghul/pythonz/master/pythonz-install | bash
To install pythonz
. (It will be installed in ~ / .pythonz
.)
Next, execute the following command to add the necessary settings to ~ / .bashrc
.
$ echo '[[ -s $HOME/.pythonz/etc/bashrc ]] && source $HOME/.pythonz/etc/bashrc' >> ~/.bashrc
direnv
Install direnv
using homebrew
. To install direnv
, run the following command.
$ brew install direnv
After the installation is complete, run the following command to add hook
to ~ / .bashrc
. (If you are using a shell other than bash
, please read as appropriate.)
$ echo 'export EDITOR={Editor to use}' >> ~/.bashrc
$ echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
To enable these settings
$ source ~/.bashrc
Please run the. Also, if there is a file called ~ / .bash_profile
If not, create a ~ / .bash_profile
with the following content.
~/.bash_profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Python
with pythonz
$ pythonz list --all
When you run, you will see a list of Python
versions that you can install with pythonz
:
# Available Python versions
# cpython:
3.1.3
3.1.2
3.1.5
3.1.4
2.4.3
...
To install any version of Python
$ pythonz install {The version you want to install}
Please run the. To install Python
of version 3.5.0
$ pythonz install 3.5.0
To execute. (Installed on ~ / .pythonz / pythons / CPython-3.5.0
.)
By the way, the version list of Python
installed using pythonz
is
$ pythonz list
You can check by executing. (It will be displayed as follows.)
# Installed Python versions
CPython-3.5.0
For other usage
$ pythonz help
Please run and check.
direnv
First, execute the following command.
$ direnv edit ~/dev/myproject
Then, the editor specified by ʻexport EDITOR = {editor to use}starts, so write the environment variable settings. To use
Python of version
3.5.0installed with
python zearlier in this directory, please describe the following settings. (This setting is saved in
~ / dev / myproject / .envrc`.)
~/dev/myproject/.envrc
layout python $(pythonz locate 3.5.0)
After completing the settings, change to the ~ / dev / myproject
directory. Then, I think that the environment will be initialized. If not initialized
direnv: error .envrc is blocked. Run `direnv allow` to approve its content.
If a message like this is output
$ direnv allow
I think that it will be initialized by executing.
This completes the settings for using Python
of version 3.5.0
in ~ / dev / myproject
. From now on, you can use version Python
of version 3.5.0
by moving to this directory.
Python
used for each directoryLet's see if we can really use version 3.5.0 of Python
with ~ / dev / myproject
.
First, in the current directory (~ / dev / myproject
)
$ python --version
To execute. Then
Python 3.5.0
Will be displayed. Then go to another directory to see if this setting is valid only in the current directory
$ python --version
I will try to execute. Next time
Python 2.7.10
Is displayed, and you can see that the version Python
of version 3.5.0
is not used (system Python
is used) in directories other than ~ / dev / myproject
. I will.
Using pythonz
and direnv
, I have summarized the setting procedure for using any version (3.5.0
) of Python
in any directory (~ / dev / myproject
). ..
This is my first time writing an article like this, so I think there were many points that I couldn't reach. However, I hope this article will help anyone other than myself ~~ if it helps to increase the population of Python users in the company ~~.
Recommended Posts