Welcome back to my personal biomedical data science blog, [VS]Codes! Today’s blog post will be a brief tutorial on how to create and activate a virtual environment for your Python project.
I’ve recently been working on a lot of code with older Python packages that are not supported for the most recent versions of Python. So, I’ve needed to find a way to make use of an alternate version of Python for my project. The solution? Setting up a Python virtual environment.
The following StackOverflow post provides a great overview of the benefits of Python virtual environments. Here’s a brief summary of its key takeaways:
What are virtual environments?
- “Virtual environments are lightweight, self-contained Python installations, designed to be set up without requiring extensive configuration or specialized knowledge.”
Why is it bad to use your system’s Python installation?
- “Running with the system Python and libraries limits you to one specific Python version, chosen by your OS provider. Trying to run all Python applications on one Python installation makes it likely that version conflicts will occur among the collection of libraries. It’s also possible that changes to the system Python will break other OS features that depend on it.”
What do virtual environments do?
- “Virtual environments avoid the need to install Python packages globally. When a virtual environment is active,
pip
will install packages entirely within the environment, which does not affect the base Python installation in any way.”
- “Virtual environments avoid the need to install Python packages globally. When a virtual environment is active,
Now let’s go into the steps for setting up a Python virtual environment.
These next steps assumes that you are working on a Mac. Our example will focus on setting up a Python 3.10 virtual environment. Refer to the StackOverflow post above for instructions for Windows systems.
Step 1: Install the version of Python that you need
First, install Python 3.10 if you don’t already have it. If you’re on macOS, you can use Homebrew to install Python 3.10 from your Terminal:
brew install python@3.10
Then, verify your installation:
python3.10 --version
Step 2: Create a Virtual Environment
Now, we can create a new virtual environment for our project using Python 3.10:
Navigate to your project folder in your Terminal:
cd path/to/your/project
Create a virtual environment using the following command:
python3.10 -m venv [environment_name]
For instance, if we wanted to name our environment .breaking_bad
, our command would be:
python3.10 -m venv .breaking_bad
It is good practice to start the name of your virtual environment with a period (.) so that the folder becomes hidden by default on your system - in the process, you can keep your project directory clean, help prevent accidental Git commits, and more easily avoid potential namespace conflicts.
Finally, we can active the virtual environment we’ve just created!
source .breaking_bad/bin/activate
After activation, our terminal prompt should show the name of our environment (e.g. (.breaking_bad)$
).
Step 3: Install your required Python packages
Once inside your virtual environment, you can install any Python packages that you want! For instance:
(.breaking_bad)$ pip install medspacy
And ta-da! Mission accomplished!