Will’s Best Fool-Proof Python Environment Recipe

Python virtual envs (on mac/linux):

Why python -m?

  • Use `python -m` to have run the `pip` command so that it is linked to the version of python. So if you have a different python version you want to use, like python 3.12, you can run whatever your alias is to that version of python like: `python@3.12 -m` to be sure that you are using the correct version of python to install and compile your modules.

Step 1: Create a virtual environment to isolate your modules. In this case the 2nd `venv` is whatever you want to call your virtual env. I always use “venv” as the name to keep it easy for scripting:
`python -m venv venv`

Step 2: Initialize the environment

Mac:

`source venv/bin/activate`

Step 3: Install your modules

  • Install whatever modules you want here
  • You can also install from a requirements file like `python -m pip install -r requirements.txt` (or whatever your path to requirements.txt is)

`python -m pip install <module-names-here>`

Step 4: Use your new setup

  • You can now work within this virtual environment and run your code