E19 Installation, Virtual Environments, and Packages

Note: you may have to replace the command 'python' with 'python3'.

Installation & Virtual Environments

Windows

Install Python
  • Download/install Python from the Windows store. This should give you version 3.11

  • In the Start Menu, check that there are at least two different ways of accessing Python:

    • 'Python 3.x', which should open a command prompt window with Python

    • 'IDLE (Python 3.x)', which should open an interactive Python shell.

Create a Virtual Environment & activate it
  • Open the 'Command Prompt' (NOT Python) and do the following

cd <the path to a folder where you will write code>
pip install virtualenv
Select language

This installs the 'virtualenv' tool using the package manager 'pip', which comes with the standard windows installation of Python. If this doesn't work, you might want to try 'venv' instead of 'virtualenv' -- one of these should work on your machine.

Next, we will use the tool virtualenv to create an environment. To do this

python -m virtualenv <some name for your virtual environment, e.g., 'env1'>
Select language

This should create a folder, named 'env1' (or whatever name you gave your environment) that contains your environment. There can be multiple environments, but only one can be 'active' at a time. Right now, none is active -- let's activate the environment you created by running a script called 'activate' inside the 'Scripts' subfolder of the 'env1' folder. In the command prompt, you do this by entering:

env1\Scripts\activate
Select language

Now you should see something like this on the command prompt:

(env1) C:\Users\emasroo1\Documents\E19\Code> 
Select language

this tells you that the environment 'env1' is now active.

Installing packages inside a virtual environment

Your virtual environment is a 'walled garden' of Python packages. Any package installations here will not affect your global installation of Python or other environments.

Let's install a package from inside this environment using the package manager 'pip'.

pip install numpy
pip install matplotlib
Select language

Now, you can interact with Python in two different ways:

  • 'Command Line Python': enter 'python' into the command line while your virtual environment is active, or 'python3'. This should launch Python inside the same window.

  • IDLE: enter 'python -m idlelib.idle' into the command line while your virtual environment is active. This should launch the 'IDLE' interface in a new window.

Inside either of these Python interfaces, try the following import statements and verify that they work. (They will only work if you are in an environment where the packages 'numpy' and 'matplotlib' have been installed)

import numpy
from matplotlib import pyplot
Select language

Once you have verified that these import statements work, you should check that the import statements don't work in the regular instantiation of Python. Open up IDLE or the Python command prompt from the Start Menu, and try the code above. If they don't work, you've successfully installed your packages only inside the 'env1' environment, and not outside it!

Deactivating an environment

While an environment is active in your command prompt i.e., you can see (environment name) before the rest of the prompt, you can enter

deactivate
Select language

to deactivate the environment. Remember that activating and deactivating an environment of Python is specific to each window of the command prompt.

Mac

Install Python

Install Python using the official Package Installer available at python.org. Use the default settings, and once you are done, check that Python is installed by opening a Terminal and entering 'which Python' or 'which Python3'. You should then see the location of the binary for 'python3'.

which Python3
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3
Select language
Creating a virtual environment
  • Open the Terminal (NOT Python) and do the following

cd <the path to a folder where you will write code>
pip install virtualenv
Select language

This installs the 'virtualenv' tool using the package manager 'pip', which comes with the standard Mac installation of Python. If this doesn't work, you might want to try 'venv' instead of 'virtualenv' -- one of these should work on your machine.

Next, we will use the tool virtualenv to create an environment. To do this, type

python -m virtualenv <some name for your virtual environment, e.g., 'env1'>
Select language

This should create a folder, named 'env1' (or whatever name you gave your environment) that contains your environment. There can be multiple environments, but only one can be 'active' at a time. Right now, none is active -- let's activate the environment you created by running a script called 'activate' inside the 'Scripts' subfolder of the 'env1' folder. In the command prompt, you do this by entering:

source env1/bin/activate
Select language

Now you should see something like this on the command prompt:

(env1) % 
Select language

this tells you that the environment 'env1' is now active.

Installing packages inside a virtual environment

Your virtual environment is a 'walled garden' of Python packages. Any package installations here will not affect your global installation of Python or other environments.

Let's install a package from inside this environment using the package manager 'pip'.

pip install numpy
pip install matplotlib
Select language

Now, you can interact with Python in two different ways:

  • 'Command Line Python': enter 'python' into the Terminal while your virtual environment is active, or 'python3'. This should launch Python inside the same window.

  • IDLE: enter 'python -m idlelib.idle' into the command line while your virtual environment is active. This should launch the 'IDLE' interface in a new window.

Inside either of these Python interfaces, try the following import statements and verify that they work. (They will only work if you are in an environment where the packages 'numpy' and 'matplotlib' have been installed)

import numpy
from matplotlib import pyplot
Select language

Once you have verified that these import statements work, you should check that the import statements don't work in the regular instantiation of Python. Open up IDLE from the Launchpad, and try the code above. If they don't work, you've successfully installed your packages only inside the 'env1' environment, and not outside it!

Deactivating an environment

While an environment is active in your command prompt i.e., you can see (environment name) before the rest of the prompt, you can enter

deactivate
Select language

to deactivate the environment. Remember that activating and deactivating an environment of Python is specific to each window of the command prompt.

Best practices

  • Always launch Python from the command prompt using an environment that you have created.

  • It's best to use only one environment at a time so you don't have to remind yourself which window of Python was opened using which environment. It's tedious to try and find out which environment you're in once you have launched Python

Packages

Installing and importing packages

Python comes with some built-in functions, but much of its capabilities which will be used in this class are stored in 'packages'. Packages are exactly what they sound like -- they contain a set of functions that can be used to carry out specific tasks. Each package has its own website, its own set of dedicated (mostly volunteer) developers, and its own versioning system that gets updated over time with new features. In this class, the packages that we will use most often are the following:

  • numpy (for nunmerical computation)

  • scipy (for scientific computation)

  • matplotlib (provides plotting functionality)

Some packages come pre-installed with python. These include:

  • math (advanced mathematical functions)

  • os (interacting with the operating system)

There are two different things that you need to do with a package:

  • installing a package

    You install a package outside Python, using a package manager such as 'pip'. Typically, this means entering 'pip install numpy'. This installs the package on your computer. As explained earlier in this guide, you should install packages inside an environment, so that packages stay in their walled-gardens. So, for example, you could create an environment called 'E19', activate the environment, and then run 'pip install <package name>' for a few different packages. Then, whenever you are coding for this class, you should activate this environment (and deactivate when you're done!). If you only use one environment, you will only need to install once. But don't be afraid of installing the same package multiple times in multiple environments.

  • importing a package

    You import a package inside Python, i.e., your Python code has to import packages. There are a few different ways of doing this, but they all boil down to writing import statements at the top of most Python files you will write.

Using Packages

Let's try to write Python code that uses the function 'linspace', which creates an equally spaced array of numbers between two specified numbers. You can read the full documentation for linspace -- or any other function in numpy -- here.

import numpy
a = linspace(1,2,6)
0.1s

This didn't work, because even though you imported 'numpy', you didn't use the right namespace to access this function.

import numpy
b = numpy.linspace(1,2,6)
print(b)
0.4s
import numpy as someName
c = someName.linspace(1,2,6)
print(c)
0.3s

Often, you will see people use 'import numpy as np' because 'np' is a shorter name than 'numpy', but this is just a convention!

You can also specifically import a function from a package -- if you do this, you don't need to append the package name to the function every time you use it. If you know which functions you will use, this is not a bad idea.

from numpy import linspace
d = linspace(1,2,6)
print(d)
0.3s

You can also choose to give each imported function your own name:

from numpy import linspace as newName
e = newName(1,2,6)
print(e)
0.3s
Runtimes (1)