Python Development Workflow with Pyenv
I’ve been using Python programming language professionally for a year now. Previously, I only use python mostly on my uni assignment or some side project work using Raspberry Pi. Since the first time I know python, I have been used a combination of
virtualenvwrapper to manage my local python virtual environment and set up my python development workflow. If you are not using virtual environment on your development workflow, I suggest you to start using it because it will save you with many problems in regards to package versioning and also makes your system environment clean.
My typical python development workflow:
$ mkdir my-awesome-project
The above steps are what I usually do when I start a new project.
- I create a new folder for my project.
- Create new
- Install all of my project dependencies
- Set default folder for the virtual environment that’s newly created.
Once I’ve done above step, whenever I want to work on
my-awesome-project I can just open up a new terminal and type
workon my-awesome-project, it automatically activates
my-awesome-project virtualenv and enters my project directory.
That workflow works well for me since the first time I knew python and never change since then. However, then I found a problem want to try a new version of python, for example, I have python 3.5 installed on my machine, and I want to try version 3.6 before decided to upgrade to a new version. In my legacy workflow, there is no way I can try a new version of python without actually install it on my machine.
nvm (nodejs) and
rbenv (ruby), that manage different version of language runtime installed on one machine. I google similar thing for python and then
pyenv come up. This tool allows us to install multiple versions of python on one machine.
In the next couple of sections, I will describe how you can install this tool in OSX and Ubuntu 18.04 and also how I incorporate this tool into my development workflow and make it more seamless and convenient than before.
pyenv in OSX is pretty straightforward. Assume you have
homebrew installed already
$ brew update
The steps I provide below works well on Ubuntu 18.04. I assume it will work with the previous version (but I don’t guarantee it)
$ apt-get update
After it’s installed successfully, you can put this at the end of your shell configuration file (
.zshrc) so it will automatically load when you launch your terminal.
$ export PATH="~/.pyenv/bin:$PATH"
After I successfully install
pyenv, I can start to play around with a different version of python and also incorporate it into my development workflow. The first thing that I do is install the python version that I want, in this case, I want to keep python 3.6 and also want to play with python 3.7.
pyenv install 3.6.6
After everything is successfully installed, I can start playing around with it.
notes: if you want to make python 3.6.6 as your default system python version, then you can run this
$ pyenv global 3.6.6
$ mkdir new-awesome-project
The above step is what I’m doing to start a new project
- Create a new folder for my project
- Create new virtual environment using python 3.6.6 and name it
- Go into the project folder
.python-versionfile and inside it put the name of the virtual environment that this project will use. After adding
.python-version, Your shell will automatically detect the required virtual environment for this project and activate it immediately. So now every time I enter the directory, it will
new-awesome-projectvirtual environment, and if I exit the directory, it will
Let say you want to try to play around with python 3.7. You can do a similar thing like the previous steps.
$ mkdir python37-sandbox
After I run above command, I immediately can play around with python 3.7 in a new virtual environment.
For the existing project, what you can do is:
- create virtualenv for the existing project
.python-versionfile in the root folder of the existing project
- put the name of the virtualenv inside that file
- pip install again all requirements for the project
Now every time you enter the project directory, it will activate the virtualenv
I found that
pyenv is a convenient package that helps me to manage different version of python installed on my machine. The integration with my shell also makes it easy to activate the virtualenv (hint: it will be active immediately after we enter the directory)
I hope you enjoy reading this post and this information can be useful to you.
If you encountered this error while installing
python 3.7.0 on ubuntu
BUILD FAILED (Ubuntu 18.04 using python-build 20180424)
You can try to install the required package first
$ apt-get install libffi-dev
Python Development Workflow with Pyenv