4. Session 3 : Programming Environments#
4.1. Background#
A programming environment is a collection of software tools, systems, and software libraries that are used together to create and manage one or more software development projects. Whereas the number of possible instances of programming environments is very large (all the combinations of all possible components) they all set out to achieve some key goals
Seperate projects that are not logically connected
Keep track of project dependencies
Seperate project components from system wide components
A programming environment might also
Provide tools for converting an environment into a distributable repository
Provide version control
Provide acces control
… other things
For the purpose of this course the focus will be centred on first 3 goals. This is done because there are already good tools for 4, 5, and 6 that can easily be combined with the first 3 but are not dependent on them (seperation of concerns). For example, docker can be used to realise 4, git can be used for 5, and unix provides 6 by design.
Importantly, it would be difficult to describe an environment to be a programming environment if it did not have the first three characteristics, but the second three are not necessary.
Since we are using python for STOR609 focus will be made on two python tools that can be used together to provide a python based programming environment. These tools are pyenv and venv.
4.2. Online Resources#
Here are links to some excellent online resources for helping you use pyenv and venv.
4.2.1. Exercise#
Which of the 5Rs do you think that a programming environment might help improve ?
4.3. pyenv#
4.3.1. What is pyenv ?#
pyenv is a tool for installing and managing multiple python versions on the same system.
it lets you switch between python versions globally, per project, or per shell.
it works by shimming python executables and adjusting your
PATH, not by modifying system python.
4.3.2. Why use pyenv ?#
avoids version conflicts between projects that require different python versions.
lets you test code across multiple python versions easily.
keeps the system python untouched, which is important for OS stability.
works well with tools like virtualenv, venv, and poetry.
makes it easy to match production python versions locally.
4.3.3. Installing and configuring pyenv#
You installed pyenv in Session 1 using this shell script
4.3.3.1. Exercise#
Have a look at the shell script. Can you find which part of the shell script installs pyenv. What does this part of the script actually do ?
4.3.4. Activating pyenv#
pyenv is a program and needs to be active before you start programming. Since pyenv is just another program the way in which you start and stop can implemented by yourself. For STOR609 a shell script start-pyenv has been added to your home directory. Running this script will activate pyenv.
source ~/start-pyenv
or just
~/start-pyenv
4.3.4.1. Exercise#
In the context of pyenv what does activate mean ?
4.3.4.2. Exercise#
How would you deactivate pyenv ?
Note that if you are using multiple shells and/or terminals you will need to start pyenv in each one that you intend to use it in.
4.3.5. Listing installed python version#
pyenv versions
4.3.6. Listing available python versions#
pyenv install --list
4.3.7. Installing a version of python#
pyenv install <version>
4.3.8. Showing globally active python version#
pyenv global
4.3.9. Changing global python version#
pyenv global <version>
4.3.10. Remove python version#
pyenv uninstall <version>
4.3.11. Changing global python version#
pyenv local <version>
4.3.11.1. Exercise#
How would you find out if pyenv is active in your current shell ?
4.3.11.2. Exercise#
Install two more versions of python (one newish one, one quite old)
4.3.11.3. Exercise#
Change the global python version
4.3.11.4. Exercise#
Start a new shell. Change the local python version
4.3.11.5. Exercise#
Can you find some python code that will run in your “newish” python but not the “oldish” version ?
4.3.11.6. Exercise#
Remove the “oldish” version of python
4.3.11.7. Exercise#
Change the global version of python to the latest version you have installed.
4.3.11.8. Exercise#
Check which is the global version of python
4.3.11.9. Exercise#
Use python to determine which version of python is active.
4.4. venv#
4.4.1. What is venv?#
venv is Python’s built-in virtual environment module (available since python 3.3).
it creates an isolated python environment with its own copy of a python system, and associated tools (e.g. pip) and packages.
each virtual environment is just a directory containing python binaries and libraries.
4.4.2. Why use venv?#
prevents dependency conflicts between projects.
ensures each project has its own package versions.
keeps the global/system python clean.
makes projects reproducible using
requirements.txt.lightweight and requires no external tools.
4.4.3. Create a virtual environment#
python -m venv <environment-name>
Note - a common convention is to name the environment <project-name>-env.
Note - a python virtual environment has a directory structure associated with it. The top level for this structure a directory with the name of the environment.
4.4.4. Activate the environment#
source <environment-name>/bin/activate
4.4.5. Deactivate the environment#
deactivate
4.4.6. Recreate the environment (common workflow)#
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt
4.4.6.1. Exercise#
Activate pyenv and set the global python system as required by your IPD project.
4.4.6.2. Exercise#
Create a new python environment for your IPD project
4.4.6.3. Exercise#
Use tree to examine the directory structure that venv has created for you.
4.4.6.4. Exercise#
Activate your IPD environment
Note - it is convential to work inside the directory structure that venv creates when working on the associated project.
4.4.6.5. Exercise#
Change your current working directory to the one created by venv.
4.4.6.6. Exercise#
If you are using spyder install it in your IPD environment
4.4.6.7. Exercise#
If you are using jupyter notebook install it in your IPD environment.
4.5. IPD#
For the remainder of this session continue developing your IPD strategies and simulation, but now using the programming environment you created in the proceeding exercises.