This guide will help you to install Python (version 3.7) on your computer. It is largly copied from Miguel Grinberg's post. Also, there is an explanation about what a virtual environment (venv) is, and why you should use it.
If you don't have Python installed on your computer, go ahead and install it now. If your operating system does not provide you with a Python package, you can download an installer from the Python official website. If you are using Microsoft Windows along with WSL or Cygwin, note that you will not be using the Windows native version of Python, but a Unix-friendly version that you need to obtain from Ubuntu (if you are using WSL) or from Cygwin. If you don't know what it is, don't worry just continue.
To make sure your Python installation is functional, you can open a terminal window and type python3
, or if that does not work, just python
. Here is what you should expect to see:
> python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The Python interpreter is now waiting at an interactive prompt, where you can enter Python statements. In future chapters you will learn what kinds of things this interactive prompt is useful for. But for now, you have confirmed that Python is installed on your system. To exit the interactive prompt, you can type exit()
and press Enter. On the Linux and Mac OS X versions of Python you can also exit the interpreter by pressing Ctrl-D. On Windows, the exit shortcut is Ctrl-Z followed by Enter.
For linux, just install python directly using the terminal
sudo apt update
sudo apt install python3.7
Next to python there are many packages available that have been created by other python users and open for you to use. For example there is package that makes it easier to handle grids called numpy
, they are available in a public repository, from where anybody can download them and install them. The official Python package repository is called PyPI, which stands for Python Package Index (some people also refer to this repository as the "cheese shop"). Installing a package from PyPI is very simple, because Python comes with a tool called pip that does this work (in Python 2.7 pip does not come bundled with Python and needs to be installed separately).
To install a package on your machine, you use pip
as follows:
$ pip install <package-name>
Interestingly, this method of installing packages will not work in most cases. If your Python interpreter was installed globally for all the users of your computer, chances are your regular user account is not going to have permission to make modifications to it, so the only way to make the command above work is to run it from an administrator account. But even without that complication, consider what happens when you install a package as above.
The pip tool is going to download the package from PyPI, and then add it to your Python installation. From that point on, every Python script that you have on your system will have access to this package. Imagine a situation where you have completed a web application using version 1.16.3 of numpy, which was the most current version of numpy when you started, but now has been superseeded by version 1.17. You now want to start a second application, for which you'd like to use the 1.17 version, but if you replace the 1.16.3 version that you have installed you risk breaking your older application. Do you see the problem? It would be ideal if it was possible to install numpy 1.16.3 to be used by your old application, and also install numpy 1.17 for your new one.
To address the issue of maintaining different versions of packages for different applications, Python uses the concept of virtual environments. A virtual environment is a complete copy of the Python interpreter. When you install packages in a virtual environment, the system-wide Python interpreter is not affected, only the copy is. So the solution to have complete freedom to install any versions of your packages for each application is to use a different virtual environment for each application. Virtual environments have the added benefit that they are owned by the user who creates them, so they do not require an administrator account.
When you are going to install Pycharm, you can ignore the following sections, since Pycharm offers an automatic way of creating an environment. In case you are interested in what is happening in the background, please read on.
Let's start by creating a directory where the project will live. I'm going to call this directory serpentine, since that is the name of the subject:
$ mkdir serpentine
$ cd serpentine
If you are using a Python 3 version, virtual environment support is included in it, so all you need to do to create one is this:
$ python -m venv serpentine
Note that in some operating systems you may need to use python3
instead of python
in the command above. Some installations use python
for Python 2.x releases and python3
for the 3.x releases, while others map python to the 3.x releases.
With this command, I'm asking Python to run thevenv
package, which creates a virtual environment named venv
. The venv
in the command is the name of the Python virtual environment package, and the serpentine
is the virtual environment name that I'm going to use for this particular environment. If you find this confusing, you can replace the command serpentine
with a different name that you want to assign to your virtual environment. In general I create my virtual environments with the name venv
in the project directory, so whenever I cd
into a project I find its corresponding virtual environment.
After the command completes, you are going to have a directory named venv where the virtual environment files are stored.
If you are using any version of Python from before 3.4 (and that includes the 2.7 release), virtual environments are not supported natively. For those versions of Python, you need to download and install a third-party tool called virtualenv before you can create virtual environments. Once virtualenv is installed, you can create a virtual environment with the following command:
$ virtualenv serpentine
Regardless of the method you used to create it, you should have your virtual environment created. Now you have to tell the system that you want to use it, and you do that by activating it. To activate your brand new virtual environment you use the following command:
$ serpentine\Scripts\activate
(serpentine) $ _
When you activate a virtual environment, the configuration of your terminal session is modified so that it uses this Python interpreter when you type python
. Also, the terminal prompt is modified to include the name of the activated virtual environment between brackets, (serpentine). The changes made to your terminal session are all temporary and private to that session, so they will not persist when you close the terminal window. If you work with multiple terminal windows open at the same time, it is perfectly fine to have different virtual environments activated on each one.
Now that you have a virtual environment created and activated, you can finally install 'your-package' in it:
(serpentine) $ pip install your-package
If you want to confirm that your virtual environment now has 'your-package' installed, you can start the Python interpreter,, by typing python
and import 'your-package' into it:
>>> import your-package
>>> _
If this statement does not give you any errors you can congratulate yourself, as 'your-package' is installed and ready to be used. Now by typing exit()
in the console we leave the python interpreter.
In order to delete a virtual environement we have to remove the entire folder. First make sure you are out of your virtual environment by typing deactivate
, then we have to move up to the directories Scripts
and serpentine
, this can be done by typing two times cd ..
(or once cd ../..
). And finally we remove the folder with the following command rmdir /S serpentine
. Then press y to confirm.
All commands bundled:
deactivate
cd ../..
rmdir /S serpentine
Basically it is a quick summary (copy and paste) of this post in the linux terminal.
sudo apt install virtualenv
sudo apt install virtualenvwrapper
echo "source /usr/share/virtualenvwrapper/virtualenvwrapper.sh" >> ~/.bashrc
sudo apt install python3-pip
pip3 completion --bash >> ~/.bashrc
pip3 install --user virtualenvwrapper
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc
echo "source ~/.local/bin/virtualenvwrapper.sh" >> ~/.bashrc
export WORKON_HOME=~/.virtualenvs
mkdir $WORKON_HOME
echo "export WORKON_HOME=$WORKON_HOME" >> ~/.bashrc
echo "export PIP_VIRTUALENV_BASE=$WORKON_HOME" >> ~/.bashrc
source ~/.bashrc
This creates a new virtual environment of python 3.7, with the name serpentine
mkvirtualenv -p python3.7 serpentine
You will see that the environment will be set up, and your prompt now includes the name of your active environment in parentheses (serpentine)
. Also if you now run
python -c "import sys; print(sys.path)"
you should see a lot of /home/user/.virtualenv/...
because it now doesn't use your system site-packages.
You can deactivate your environment by running
deactivate
and if you want to work on it again, simply type
workon serpentine
Finally, if you want to delete your environment, type
rmvirtualenv serpentine
For windows you have to cd all the way to your venv path.
virtualenv serpentine | make venv named serpentine
source venv/bin/activate | activate venv
deactivate | deactivate venv (name serpentine)
For deletion simply delete the folder of your venv.
mkvirtualenv -p python3.7 serpentine | make venv python version 3.7 and name serpentine
workon serpentine | activate venv (name serpentine)
deactivate | deactivate venv (name serpentine)
rmvirtualenv serpentine | delete venv