Virtual environment for Mac vs Windows
Author: Yanka Sikder
August 15, 2024
When switching between a Mac Device and a Windows device, I would confuse the way to create and activate the virtual environment. So, I wanted to write this for myself and others who wanted a better understanding of it from my words. I know this could be an easy google search, but after doing it so many times, I truly understood it after writing all the related information down in one area for me to look back at. So consider this a way for myself to use and maybe help others who struggle with remembering the difference. This is also my first 'tech' blog ever, so I know its not as technical or something with a bigger picture, but I wanted this to be informative and easy to read! You could almost say this is like a tech dictionary in a sense, but I prefer the term blog so it allows me to be a little more informal and interactive with readers :)
What is a virtual environment? Why use it?
This is an isolated space for self-contained Python Installations. This allows programmers to test their code utilizing different python libraries or versions. When you pip install ____ (something), these will be added to your virtual environment folder Each environment folder can contain different versions or libraries which allow for testing of different aspects which is all versatile to the user themself.
TLDR: Flexibility in testing different libraries/versions for code.
Why are the commands different for Mac and Windows?
Since they are two different Operating Systems, they each have a different way to use the command prompt and PowerShell. Thus there are different commands which activate different actions!
Instructions - terminal:
Mac
Create the virtual environment folder: python -m venv env
Activate it: source env/bin/activate
Windows
Create the virtual environment folder: python -m venv env
Activate it (each line is a different terminal command): cd env
cd Scripts
activate
~pip install what you need and test your program in the virtual environment~
Tips
Easier Install of Python Libraries
[1] During my early stages of development, I ran into a lot of issues due to my own confusion with git control. My problem being that since I didn't understand the difference in the commands, when I would switch devices, I would sometimes delete the environment and have to manually reinstall them. Obviously, this is time consuming when you have a lot of libraries or you are using an organization based project or forking a project from someone. With this issue, I learned the magic of this command: pip freeze > requirements.txt
This puts all your current python libraries into a requirements.txt file. When you want to create a new environment, or even a new project, with the same libraries, this speeds up installing libraries by a tendfold. The command to do this is: pip install -r requirements.txt
Gitignore env folder
[2] Another git control issue I had was that my whole virtual environment folder would upload to my GitHub Repository when I commited. Although this is allowed, it takes up a lot of storage and is not really needed in the repo. This is how I learned about the magic of .gitignore. I've always seen it as it comes building projects, but I never really understood the use for it. There may be other uses, but I'm focusing on solving the one where the entire env folder is uploaded to a the GitHub Repo branch. Basically all you have to do is put the name of your virtual environment folder and then a '/' on a line in the .gitignore file. If the folder is named env, then it would look like this in the file: env/
Next time you commit to a branch, the folder should not be on the Repo. If you are saving your project files locally, the env folder should still exist on your machine to use on your IDE. However, if you are only cloning from the Git Repo, then either remake the env every time or upload the env folder.
Thats it for this one! I know some of these may be like a "Duh" moment, but we all have to start somewhere. I hope this could be helpful to someone. Thank you for reading!