Create and configure a virtual environment for your project
This activity assumes you have already completed tutorial 2 and the week 1 coursework activity to create the repository.
Carry out the following:
- 1. Create and activate a Python virtual environment
- 2. Install Python dependencies in the virtual environment
- 3. Understand the purpose of the project files
- 4. Install the project code in your environment
1. Create and activate a Python virtual environment in the directory of the project
There are several tools that allow you to create Python virtual environment in a project such as Python venv, pipenv and poetry. You may use any.
venv
comes with Python so the following should work for all.
There are specific instructions for each IDE using their menu options to create a virtual environment:
Otherwise, you can use the PyCharm/VS Code Terminal with the following guidance.
Create a virtual environment
To create a virtual environment, open the Terminal in your IDE (VS Code, PyCharm). Check that you are in the directory of the project, if not, navigate to it.
Enter the code to create the "venv" in the Terminal.
- Unix/macOS
python3 -m venv .venv
- Windows
py -m venv .venv
The command will take a few minutes to complete.
Activate the virtual environment
You now need to activate the virtual environment for the current project.
- Unix/macOS:
source env/bin/activate
- Windows:
.\env\Scripts\activate
2. Install the dependencies in the environment
For the coursework1 you will need pandas. For coursework2 you will need pytest. These are listed in requirements.txt
Find the correct syntax for your operating system
Install the dependent libraries/packages using requirements.txt, e.g.: pip install -r requirements.txt
3. Understand the purpose of the project files
Aside from the coursework starter files, there are some essential files that you need to set up your project's code. You need to understand their purpose. These are:
.gitignore
this file lists files that should not be tracked by source code control. This includes standard Python packages and configuration files for your IDE. Other developers only need your code, they will have their own IDE config and their own version of Python packages.README.md
this file tells other developers useful information about your project. Check out this example for the Python pandas package. You should always have a README.md and this documentation explains why and how to create one.requirements.txt
this file lists any Python packages that your project needs in order for the code to run. The documentation explains the file format, at its simplest you list the names of the packages in the format they are in the PyPi index; with one on each line.pyproject.toml
this file contains metadata (info about your project) needed by Python tools that are used to build your code as a package. More on pyproject.toml here .LICENSE
this file is optional though GitHub guidance recommends you include a license . It tells other developers what rights they have to use/adapt your code.
4. Install the code in this repo in your environment
This is needed in coursework 2; however for convenience it is recommended you complete the step now.
Installing your code as a package in the following way means that any changes you make to the code will affect the installed package without needing to re-install it.
This process requires a configuration file about the project such as pyproject.toml
, setup.cfg
or setup.py
.
This repository has a pyproject.toml
configuration file.
Enter the code for your operating system in the Terminal of the project's root folder in your IDE.
pip install --editable .
or pip install -e .
The .
is not a typo, it is required. It refers to anything in the current directory tree in the command prompt of the
terminal.