What it is:
A directory where you can download external packages that you use for your own program into. External packages are also just code that you can use in your program (python: import
). It also stores a version of your interpreter (in this blog's case python). Essentially, you should be using a copy of your interpreter when running your programs.
Virtual environments exist, because you do want to isolate these external packages on a per-project-level. You need to do this, because when you're writing your own programs, you are often dependent on these external packages - hence the name: "dependencies". As these dependencies are programs themselves, they change. Sometimes, they change in a way that you cannot use them for your program anymore. At the same time, potentially another program you are writing needs a version of the same dependency that won't work with your current program anymore. Ergo, you do have a conflict between these two programs' dependencies. That is why you isolate your packages.
What you need to understand
- Virtual environments are directories (or folders) on your computer and contains a copy of your python installation.
- An environment manager does two things: a) gives you access to packages that are available and b) makes sure that the program you're writing only accesses the packages you have in the program's virtual environment.
- The largest part of your environment manager is to give your python program access to the packages in its environment. That is why you need to activate your environment (we do that in the hands on).
- External packages are also called: dependencies, libraries, imports, packages, you get the idea.
- Python's standard main package management is separated into two functions: installing packages with
pip
and managing the actual environment (folder / directory) on your computer, e.g.venv
. They both come with python 3.
Caveats
- You probably installed packages already. Check the packages of your base python installation (e.g. type
pip freeze
in your terminal). If its a long list, just know that sometimes you may by chance use a package you have already installed - When debugging code that suddenly does not work anymore, look for changes within dependencies. That is super tricky and a bane in programming
Neat stuff
- My favorite package manager
pipenv
combines package installation and folder management. That's neat! - Package managers can give you an overview of all versions of all packages and install them exactly as they are (google "pip freeze"). That's neat!
Hands on
I assume you have python 3 installed. If not, you may take the 0th step here
I will show you
- how to create an environment with python's "venv"
- Install packages with "pip"
- Run a script from within an environment
- See code fail if the environment is not actiave
Installation
Managing dependencies starts with finding a place where to install them to. Then you will activate the environment so that when you run your programs they will know to use the packages from the environment you want them to use the dependencies from.
You find a place to install your packages to by navigating into a directory with your terminal where you want to mess around. For how to use the terminal the 0th step is here.
Once you navigated to your desired directory with your terminal type:
python3 -m venv ./venv
this creates an environment (i.e. the directory) with the name "venv" in your current directory (that is the./venv
- you could put any directory path there). Its good practice to put the environment in a folder that is named venv next to your code. What you did there is to create a folder "venv". There, you stuffed some more folders with some files. These will be used to administer your external packages.Important: Now you need to activate your environment! You need to be in the directory where you installed the environment into for the next command to work. Type
source venv/bin/activate
in your terminal. If it works, you see the name of the environment in brackets "()" before your cursor in your terminal. Basically, you told your terminal to execute code in a file called "activate" that lies in your virtuale environment in the venv and then in the "bin" directory. The "venv" folder, you created in the previous step. If you used a different name during the environment creation, just look for the bin directory or activate file and repeat the process with the correct path.Now that you see that you have your environment activated, you can install packages with
pip
. Also, when you run python code with the environment activated, your program will look into this environment for your dependencies.
To demonstrate how packages work, we create a python file with a single import. Then you install the import with the environment activated. Afterwards, you run the file with the environment activated. Then we do the whole process again, but without the environment activated, to see it fail.
In the same folder, where you created the virtual environment in the "venv" directory (or whatever name you chose for the folder):
- Create a file called "main.py". In the first line of the file write:
import xkcd
. Save the file. - Make sure the environment is activated. Just to be save, within the directory of the file and where you have your environment folder type:
source venv/bin/activate
. You should see(venv)
in front of your cursor. - Install the package xkcd (you should definitely google what it is. Never just install packages). With the environment activated from before type:
pip install xkcd
. There is some output telling you that the package is being installed. - In the terminal, being in the folder type:
python3 main.py
. Observe that nothing happens! As it should. Since the import does not do anything - Now deactivate the environment. You do that by, with the environment being active, typing:
deactivate
in the terminal. - You should notice that the
(venv)
disappeared. Then run the program again (python3 main.py
). Observe the failure: "ModuleNotFoundError: No module named 'xkcd'"
Next Steps
- Read up on pipenv. It's the new cool environment manager on the block. It removes the need to activate environments, has a file where you configure the packages you're installing and has a "lock" function that ascertain that you definitely are using the version of a dependency that you're expecting
- Get an Integrated Development Environment, IDE. Trust me!