Skip to content

tzucker02/Virtual_environment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 

Repository files navigation

Create a virtual environment for each Python Project

Virtual environments in Python create isolated spaces for your projects, preventing package conflicts and keeping your system Python clean. They're essential for managing dependencies reliably across different tasks.

I am compiling some pros and cons to having a virtual environment for each Python project you start. So far here is what I have:

(NOTE: Unless otherwise noted, the commands are the same on a MAC and a Windows box)

Pros

There a several reasons you should absolutely use virtual environments.

  • Dependency Isolation: Different projects can use specific package versions (e.g., one needs requests 2.25, another 2.31) without clashes.
  • System Protection: Avoids polluting the global Python installation, which could break OS tools or get overwritten during updates.
  • Reproducibility: Pin exact dependencies via requirements.txt, making it easy to replicate setups for teams or deployment.

Global installs lead to version mismatches, privilege issues (needing sudo), and hard-to-debug errors. Virtual environments sidestep these by containing everything per project.

Cons

  • Extra Complexity: Beginners face a learning curve for setup, activation, and management, adding steps to daily workflows.
  • Disk Space: Each environment duplicates Python and packages, consuming storage—especially with large libraries like TensorFlow.
  • Activation Hassle: Forgetting to activate leads to errors; must repeat per session or project switch.

How to create a virtual Environment

There are several fairly simple steps in making a virtual environment. So if you have decided that this is the best course for you, they are as follows:

1. Start a command prompt

  • This is the first step, and not very difficult. If you are used to using a Windows machine, then you have probably done this more than once.
  • Start by clicking on the windows icon (usually located on the far left of the toolbar).
  • Type the command cmd.exe and press enter or to start a terminal on a MAC
    • press Command + space to open spotlight
    • type terminal in the search
    • start the built-in terminal or use iterm
  • You have successfully started the command program when the black screen pops up on your screen. Note that this will often default to the working directory of the logged in user (c:\users<logged-in-user-name> followed by a greater than sign, e.g. c:\users\owner>) on a MAC your will either see your-macbook:~ username$, where "your-macbook" is the name of your MAC and username is your username, or on newer macs you might see username@your-macbook ~ %, again substitue your macbook name and your username for the currect prompt.
    • your‑MacBook is your computer’s hostname (name).
    • ~ means you’re in your home directory.
    • username is your macOS username.
    • $ or % is the “prompt character” showing you can type a command.

2. Navigate to the directory where you wish to create the virtual environment

  • If you want to create your virtual environment in the current directory, you need to do nothing at all.
  • If you want to create your virtual environment in another directory, you need to use the CD command to navigate to that directory before going forward. For instance, if you want to create the virtual directory in c:\users\owner\documents, use the command cd Documents, or cd c:\users\owner\documents.

3. Use the command:

python -m venv <name of venv> at the command prompt

  • You are still in the cmd.exe command processor or terminal.
  • You should see a screen something akin to the screenshot below:

imagemac terminal

  • Type in the command as it appears above, substituting the name of the virtual environment you wish to create.

4. Activate the virtual environment using the command

./<virtual-environment-name>/Scripts/activate
(e.g. ./tz_medium/Scripts/activate or just tz_medium /Scripts/activate)
On a MAC issue the command
source myenv/bin/activate

5. Use the requirements.txt file to duplicate my current environment in your new virtual environment.

  • After the step above, activating your virtual environment, go to the next step.
  • Use the command pip install -r requirements.txt

You have now created a new virtual environment, activated it and installed the requirements for the projects noted here. To get out of the virtual environment, and restore your usual prompt, simply give the command deactivate and press enter.

About

creating a virtual environment for your python projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors