Obsidian/.archive/3-99 Research/6 Researching Techniques/Setting up a virtual python environment (venv).md

70 lines
2.3 KiB
Markdown
Executable File

---
creation date: 2024-08-22
modification date: Thursday 22nd August 2024 09:17:34
tags:
---
# Preamble
### Notable Links
[[2 pymodbus On the BeagleBone]]
## What are we doing?
Figuring out two things: a) what the hell is a virtual environment and b) how to get linux to quit complaining to me that I'm breaking system packages.
## Why are we doing it?
I would like to do this the right way. I think what we've been doing isn't the way to really go.
## Who cares?
Me. My poor computer.
# What the fuck is Venv?
It's a way to separate environments of projects. This keeps dependencies working if versions change, or if dependencies break with new versions. This keeps every project with its own environment.
# How to create an environment
[I'm following this guy's tutorial](https://www.youtube.com/watch?v=Kg1Yvry_Ydk).
``` bash
pip3 list
#should be able ot find virtualenv
python3 -m venv beaglebone_env
#This caused me an issue at first. Said I needed to install apt install python3.12-venv
sudo apt install python3.12-venv
#worked!
python3 -m venv beaglebone_env
#actually activate the python environment
source beaglebone_env/bin/activate
>> (beaglebone_env) danesabo@danesabo-desktop: ~/Documents/python_environments
#the environment is successfully loaded and running! You can check where it is using "which python"
# You can leave the environment using
deactivate
#to delete the environment
rm -rf beaglebone_env/
```
## We can create something called a 'requirements.txt'
This file allows us to recreate the environment quickly on another device.
```bash
#While in the environement you want to create a requirements list for
pip freeze > env_req.txt
# will create a requirements file with all required dependencies
# to install from a requirements list
pip install -r env_req.txt
```
## There's another way to create a virtual environment, within a project.
```bash
mkdir my_project
python3 -m venv my_project/venv
source my_project/venv/bin/activate
#Then, this will activate the virtual environment within the project!
#This is the more common way to do virtual environments. It keeps the requirements and dependencies in the project, rather than in two different projects.
#Vritual environments are usually not synced to git projects though, but requirments documents are instead.
#Do NOT put project files in the virtual environement.
```