I tried to build a Mac Python development environment with pythonz + direnv

Introduction

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.

What is pythonz?

A tool that allows you to install different versions of Python in your home directory. Please see here for details.

What is direnv

It is a tool for switching the environment for each directory. Please see here for details.

Preparation

There are some tools and libraries required to use pythonz and direnv, so please install the following in advance.

Command line tools

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.

Install pythonz and direnv

Install 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

Install 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

Environment

Install any version of 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.

Setting environment variables with 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 usePython of version 3.5.0installed withpython 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.

Check the version of Python used for each directory

Let'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.

Summary

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

I tried to build a Mac Python development environment with pythonz + direnv
I want to build a Python environment
Build Python development environment (pythonz, virtualenv, direnv)
[AWS] Development environment version that tried to build a Python environment with eb [Elastic Beanstalk]
I tried to build an environment for machine learning with Python (Mac OS X)
[Python] Build a Django development environment with Docker
Build a Python development environment on your Mac
I want to set up a GUI development environment with Python or Golang on Mac
Build python environment with direnv
I tried to draw a route map with Python
I want to easily build a model-based development environment
Build a Python development environment on Mac OS X
[Go + Gin] I tried to build a Docker environment
I tried to automatically generate a password with Python3
How to build a development environment for TensorFlow (1.0.0) (Mac)
I want to create a nice Python development environment for my new Mac
When I tried to create a virtual environment with Python, it didn't work
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
Easily build a development environment with Laragon
How to prepare Python development environment [Mac]
I tried a functional language with Python
Build Python environment with Anaconda on Mac
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
Build a Python environment on your Mac with Anaconda and PyCharm
[Python] A memo that I tried to get started with asyncio
I tried to create a list of prime numbers with python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
I tried to build a super-resolution method / ESPCN
Build a C language development environment with a container
Python: I tried to make a flat / flat_map just right with a generator
After buying a new Mac, use pyenv + poetry to build a Python environment.
I tried to communicate with a remote server by Socket communication with Python.
I tried to build a super-resolution method / SRCNN ①
Building a python environment with virtualenv and direnv
[AWS] Flask application deployment version that tried to build a Python environment with eb [Elastic Beanstalk]
Build Python development environment with Visual Studio Code
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
Build a python environment with ansible on centos6
I was addicted to creating a Python venv environment with VS Code
I want to make a game with Python
Build a Python environment on Mac (Mountain Lion)
Create a python3 build environment with Sublime Text3
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
Build a Django development environment with Doker Toolbox
I tried to discriminate a 6-digit number with a number discrimination application made with python
I tried to automate sushi making with python
I tried to build a super-resolution method / SRCNN ③
I tried to build a super-resolution method / SRCNN ②
Quickly build a Python Django environment with IntelliJ
I want to write to a file with Python
[Note] How to create a Mac development environment