Starting your Python Dev Environment with Pyenv and Pipenv on a Redhat GNU/Linux based System
Hi folks! Some colleagues of mine asked me to teach them what is the best approach to start coding into python, using streamlined tools and best practices, so I decided to help them by stating this simple, but straightforward guide.
By now I got focused on Red Hat Based systems (CentOS, Fedora or the RedHat Linux itself).
The reason for this post is pretty simple: we need to have covered several versions of python in our machine. Thus we need to install some system packages in order to have the tools needed for build the Python ecosystem.
I hope you enjoy it. 😅
Let me start by explaining what is Pyenv and Pipenv.
🏁Remark: For that ones who don’t have time to read, go to the commands and fire’em up.
Pyenv — Simple Python version management
https://github.com/pyenv/pyenv
This project is analogue to RVM. It turns you being able to manage your own version of python. It’s became useful when dealing with newer versions of python that you base System don’t support. Fedora is the most updated GNU/linux distribution; It will use, naturally the highest version available, of course, but the pyenv has a lot of supported python versions (including conda, pypy, and other useful ones), so worth installing it. =)
Pyenv itself work downloading the tarball of package, compiling it and setting the environment variables so you can easily shift between python version plus it gives your the opportunity to handle your virtualenvs.
Pipenv — Python Dev Workflow for Humans
http://docs.python-requests.org/en/master/
Accordingly the official documentation,
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.
So I strongly advise that you start using this tool. In a nutshell it manages your package versions and dev/prod packages (in order to install linting tools, specific dev packages and, why not, neovim ❤️ package?)
Ok, how can I have it installed?
It’s pretty simple.
Go into your terminal and type. Remark: CentOS/Oracle Linux/RedHat GNU/Linux users should use “yum” command line. For Fedora users, replace it with “dnf”.
For make sure you’re using the latest version of your system, please do a:
$ sudo yum update;
If the command above performed changes to your system, a.k.a installed packages, I strongly advise to restart your computer in order to continue this guide.
$ sudo yum groupinstall "Development Tools"
The command above will install a bunch of tools needed for build C/C++ projects. It’s needed
$ sudo yum install libffi-devel zlib-devel bzip2-devel readline-devel sqlite-devel wget curl llvm ncurses-devel openssl-devel lzma-sdk-devel libyaml-devel redhat-rpm-config
With this command it will install commonly used libraries into our system, so pyenv will be able to use them to compile the python code.
Installing Pyenv
Moving forward let’s use the Pyenv Installer. For more information regarding the documentation, it can easily reached at https://github.com/pyenv/pyenv-installer;
So, let’s use the guide provided by github;
$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
After this, just append the pyenv initializator command on your “.bashrc” file
Remark: If you use ZSH instead of Bash, update the “.zshrc” file.
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
After that, logout/login or exec this command to reload configuration; (in case of zsh, change this file to .zshrc;
source ~/.bashrc
Try to exec the pyenv command. You should see something like this.
$ pyenv pyenv 1.2.8
Usage: pyenv <command> [<args>]Some useful pyenv commands are:
commands List all available pyenv commands
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
rehash Rehash pyenv shims (run this after installing executables)
version Show the current Python version and its origin
versions List all Python versions available to pyenv
which Display the full path to an executable
whence List all Python versions that contain the given executableSee `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
So far so good. Now let`s install the latest python version;
$ pyenv install 3.7.1 -v
It will create a lot of log messages. If your python version was installed successfully you would being able to see something like this;
$ pyenv versions
* system
3.7.1 (set by /home/dc-user/.pyenv/version)
To select the python command as your main python command, do the following;
$ pyenv global 3.7.1
So far, so good. Now it’s time to install pipenv on our brand new python 3.7.1; To do this, please exec the following step;
$ pip install --upgrade pip pipenv
And finally you will have the pipenv command ready. =)
$ pipenv --version
pipenv, version 2018.11.26
If you need assistance, please drop here your message and I will try to help you out.
Thanks for your time. Happy Coding! 🔨