You can’t complete a real-life project in Python successfully without a virtual environment. Tools like virtualenvwrapper and virtualenv are common for creating and managing virtual environments for web development, while anaconda is widely used by data scientists.
Let’s examine how you should create and manage your Python virtual environments with the various management tools available.
How Virtual Environments Work
When you create a virtual environment, you’re instructing your machine to make an additional temporary copy of Python. That copy is independent of the Python version on your system variable. If you’re not familiar with this, take a look at the basics of Python virtual environments.
The created virtual environment doesn’t just work; you’ll need to activate it. In fact, anything you do outside of a virtual environment will not work without activation. This is a way to keep your global space a lot cleaner.
The basic principle is that the dependencies in virtual A will not work for virtual B—unless you install the dependency specifically for virtual B.
Despite this, a common pitfall for most newbies and even some experts is to install their dependencies in the global space before activation. That will never work; you should always activate before dependency installation.
How To Use The Various Environment Tools: Pros And Cons
As mentioned earlier, different environmental management tools exist for Python. Let’s take a quick look at each one of them, including how they work and their possible shortcomings.
Virtualenv is an awesome management tool for those that know their way around it. It’s pretty simple, though it can be frustrating for beginners.
To create a virtual environment with it on Windows, open up a Command Prompt window to your chosen location. Type mkdir [Folder] to make a new folder, replacing the text and brackets with your chosen name.
Next, type cd [Folder]to move into the new directory, followed by the command virtualenv [Environment Name]to create a virtual environment.
If you’re not familiar with the command line yet, take a look at some essential Command Prompt commands you should know.
Next, change folders into your virtual environment by typing cd [Environment Name]. Once you’re inside [Environment Name], type cd Scripts; be sure to use an uppercase S in Scripts. Once you’re inside the Scripts folder, activate the virtual environment by typing activate.