Setup Python Development Environment – Ubuntu
Lets Prepare Your Computer for Some Coding
Downloading Python 3
Let’s start from installing Python 3 on your operating system – you can download it here. I recommend Python 3 as this is what is currently being developed all the time and version 2.7 is no longer modernised as there were issues that could not be solved without rewriting too much. In the end it’s like with a steam engine and jet propulsion. Sure you can use both but which one wins the race eventually?
Installing Python 3 on Linux (Ubuntu)
Linux Ubuntu nowadays comes with Python 2.7 / 3.5 preinstalled but we want the newest goods don’t we?
- Download the newest python version (Python-3.6.2.tar.xz file at the moment of writing this post).
- Extract the python-3.6.2.tar.xz file.
- Open terminal in the extracted python’s directory by right clicking on the folder and selecting “open in terminal” option.
- To build and install Python 3.6.2 type the following commands:
- You should see your Python installs in /usr/bin/python…
- After a successful installation you can check your Python versions by using command: python -V or python3 -V
Choice of Your IDE (Integrated Development Environment)
Now that we have the installation of the programming language done we can carry on and install a proper Integrated Development Environment (IDE). Although Python always comes with its basic IDE called IDLE (and if you want you can write scripts even in the most basic text editor like Notepad) I strongly suggest that you get used to a software used by professionals as it will make things a lot easier later on.
For me my IDE of choice for Python is PyCharm from JetBrains (note that the free of charge community version does not have some advanced features but you do not need them to start with).
Some of PyCharm’s benefits:
- It is cross platform – so if you learn it once, you can use it pretty much the same way on Win / Linux / Mac (but so are other editors too).
- It has syntax highlighting for plenty of languages.
- It is continously being upgraded.
- It contains pretty much all the necessary tools for your programming needs in one neat package and I mean it has looooads of stuff in it.
- There are numerous plugins and improvements from JetBrains or other sources.
- And so on and so on.
Other Choices of IDEs
Whichever editor you choose please print a cheatsheet with all keyboard shortcuts for it (or at least the most important ones) and force yourself to use them instead of navigating everything with a mouse. It will be a pain in the backside at first but within a week you will thank me for forcing you to do that. The way I do it is I look at the menu element I want to select or a function to perform and I check what is it’s shortcut in my cheatsheet. I use the shortcut. Eventually you do not have to look at the cheatsheet anymore (same principle works perfectly if you want to master any computer game). It works. Try it. You will like it.
The default keymap for PyCharm is here.
Using Virtual Environments (And Wrappers for Them)
Pretty quickly you will find that your main Python installation gets clogged up with additional packages and libraries that you install. To prevent any unwanted interference it is best to keep a separate container with fresh Python installation for each project that you do. The earlier you start doing this the less mess there will be in the end.
This container is called Virtual Environment and it is like an additional clean installation of Python based on your initial version. Thanks to that approach you can have as many different environments as you wish each with a completely different set of additional items installed on top of them.
There are two methods to create virtual environments. As a programmer you need to know command line so that is our method number 1. I know – why cannot they make it easier :)?. Well the wise men in our community had similar thoughts and they created virtual environment wrappers. Using them creation of a new environment is super easy so this will be our way to make a new project space. Method number 2 is just use PyCharm’s great Project interface.
Create Virtual Environment on Linux
Most of the steps are similar to Windows installation but there are some big differences…
Install Libraries and Create Your Environment
To prevent security issues we do not want to install libraries using sudo pip3 install as that exposes the root of your Linux installation to anything that can be placed on the internet. We will install the libraries in the user’s .local folder instead.
Configure Default Folders and Python Version for Virtualenvwrapper
In the next code box we define:
- default folder to store our environments in (WORKON_HOME variable).
- default python version that will be used when environment is created (VIRTUALENVWRAPPER_PYTHON variable).
- where our base virtualenv library was installed (VIRTUALENVWRAPPER_VIRTUALENV variable).
- location of virtualenvwrapper.sh file which needs to be sourced (executed within the terminal instance).
The location of user’s home directory is stored under variable: $HOME (equivalent to: /home/your_user_name/ folder).
The .bashrc file which we need to add commands to in the next code box is in folder: HOME/.bashrc
Now Let’s Create a Few Virtual Environments
By default due to the way we setup the VIRTUALENVWRAPPER_PYTHON variable our environment will be created using Python 3.6.2. We should also test how to create a different Python version environment though.
Results of creating and testing the default environment can be seen below.
Please note that the prompt in the Terminal now has changed to: (default_env) kilthar@ubuntu:~$ – you have your new Virtual Environment’s name in parenthesis on the left of it. This means that this environment is active currently. There can be only one environment active in any Terminal instance. When switching environments the one used so far gets deactivated first and a new one activated.
All your environments get stored in folder setup in variable: WORKON_HOME ($HOME/.virtualenvs in this tutorial) in .bashrc file.
Create and Test a User Defined Python Version Virtual Environment
Here’s the effect of creating and testing user defined Python version environment.
Please note that the prompt in the Terminal now has changed to: (python27_env) kilthar@ubuntu:~$ which highlights the active environment’s name. Also notice that the Python’s version is 2.7 as requested in the mkvirtualenv command above.
Learn to Activate, Deactivate & Remove Environments
Now let’s see how to deactivate and activate an already existing environment. Since our python27_env environment is currently active let’s deactivate it. For this we use deactivate command.
You can notice straight away that the environment’s name has disappeared from the left hand side of the command prompt – it means no environment is currently active. Now that this environment is deactivated we can delete it.
Remove Environment No Longer in Use
See the terminal window below for how to do this. The command is: rmvirtualenv.
Activate Already Created Environment by Invoking Workon Command
Make a Project’s Folder & Bind It to the Environment
First make a new folder where you want to store your code.
Now we can bind the folder we are in to the environment (navigate to it using cd command). By default if you use setvirtualenvproject without specifying any project folder the current working directory gets bound to the environment currently active. We can also bind any folder to any environment in one command if we want to. Check reference for more details at: http://virtualenvwrapper.readthedocs.io/en/latest/command_ref.htmlLet’s start a new terminal, activate the default_env environment and see that the terminal prompt changes to the folder we bound previously.
And that’s it for now. We have covered Python 3 installation on Linux Ubuntu. Making and managing virtual environments and selecting Integrated Development Environment.
Your system should be ready for programming in Python 3. See you in the next post where we launch PyCharm and create our first project and learn most important settings of the IDE.