There are loads of tutorials out there for getting started with Python, but all of the ones I’ve come across miss some critical information. It’s often that the writer makes unfounded assumptions and beginners are left to piece and hobble together scraps from across the web to accomplish a simple task.
In the following, I’ll give you some basics about creating a Python virtual environment. I really do mean basics, things that I’m not going to assume you know about this process. Use this as a reference when you want to revisit how to start a new project or you blank on a terminal command.
By the way, I am working on a Mac, so your commands may look slightly different on other OS.
What you will need:
- A code editor. I use Sublime.
- Basic knowledge of your terminal.
- Python installed. If you have a Mac, you already have a version of Python installed.
If you want to check for Python on your computer, use the following command:
My output for this command was:
What’s a virtual environment?
A virtual environment is a place for you to install packages that are specific to your project. Packages give your code special abilities. For example, I built a Slack bot called Nice Vegan Bot. You can type @niceveganbot and “fact please” in any Slack channel and you will be returned a fact about veganism from my MySQL database. For this bot, I used the package slackClient, which allowed me to feed my application with my Slack API token.
How to create a virtual environment.
Let’s make a new Python project and add a virtual environment.
In the following instructions, you will be asked to run commands that start with
pip. pip is the package installer for Python. You already have pip if you have Python, so go ahead and run those commands.
Install virtualenv, a popular virtual environment package. Skip this step if you already have virtualenv installed. This command installs virtualenv globally so that you can add it to as many Python projects as you’d like.
pip install virtualenv
For reference, you can run this command to check which pip packages you have installed globally:
CD into your Python project.
Create a virtual environment specific to this project. The second part of the command below is anything we want to name the environment. So in this case, I called it ENV.
Now we need to activate our new environment, ENV. Run the following command. If you named your environment anything other than ENV, replace ENV with the name of your environment.
Now you’ll know that your environment is running when it appears in your project command line. You’ll see that the environment is listed in parentheses.
(ENV) Falons-MacBook-Pro:sample-application falondarville$
At this point, you can go ahead and install the packages you need for this specific project. The Python Package Index is your go-to reference for pip packages available.
Finally, to stop your environment, run
Now you know how to add a virtual environment to your Python project to keep your dependencies separate and potentially avoid package conflicts.