#CCNA #concept
## About
Python is a programming language used in many applications, automations etc.
## Virtual environments
python environments are a self contained directory that includes its own Python interpreter and a copy of the `pip` package manager.
It allows for different versions to be installed on the same system inside their own capsuled environment.
This is especially helpful if you are working with different pythong projects which require different versions of packages, for example Project 1 uses `Django 3.2` with `mongodb 9` but Project 2 uses `Django 2.8` with `mongodb 10`
### Creating a new virtual environment
```bash
python3 -m venv myenv
```
the above command creates a new directory `myenv` which contains:
- seperate python interpreter
- lib directory with installed packages
- scripts to activate the environment
### Using the environment
first activate the new environment:
```bash
source myenv/bin/activate
```
Once the environment is active you can run applications/commands from that environment.
you may also then install new packages inside the environment:
```bash
pip install requests
```
### mass install packages inside environment
you can create a `requirements.txt` file that lists all dependencies for the environment, this helps with reproducing the exact environment.
the `requirements.txt` file can be installed with
```bash
pip install -r requirements.txt
```
### working with applications inside an environment
When creating an environment and building an application inside this environment you will need to activate the environemt each time you want to interact/use the application.
This can be done by activating the environment each time launching a shell:
```bash
source /path/to/environment/bin/activate
```
(or alternatively just adding the above source to your `.bashrc` or `.zshrc` file.)
You may also create an alias for commands like this:
```bash
alias ansible="source /path/to/ansible_project/ansible_env/bin/activate && ansible"
```
## 🔗Resources