Skip to main content
Colin Jaffe/6 min read

Python Virtual Environments with Conda for Project Isolation

This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.

Learn how to create and manage Python virtual environments using Conda to keep your project dependencies isolated and organized.

Our next step will be to create a Python virtual environment as part of our setup here. Now this is best practice in Python development. It allows you to install any software you want, but only for the particular project you're working on.

So maybe you're building a data visualization app, a dashboard as we are, and you want to have dash and pandas, and maybe another project is a machine learning project, and you'd want to have probably still pandas, but also TensorFlow and other machine learning libraries. And for a third one, you might be building an API with Python, in which case you would want to have Flask available for you, and maybe a SQL library like SQL Alchemy. Now all of these libraries are great, but you don't want to package them all into every project.

Writing and Running Code

You also might even have projects that rely on different versions of the same package, and that's what Python virtual environments are for. And it's best practice, you rarely start a project without creating a virtual environment for it, and you could easily have 10 or 20 of these. So let's create our first one.

We'll use anaconda and the conda command, and we're going to create an environment called dvenv for data viz environment. We'll type in conda create name dvenv Python equals 3.9, and hit return on that. Now pay careful attention when you type that in to the spaces.

There's spaces and they're meaningful just as they are in Python. There are no spaces in dash name here, there are no spaces in dvenv, and there are no spaces in this Python equals 3.9. Now it gives you a lot of output in this one, and then it asks you, do you want to proceed? You can type in y and enter, unless you, you know, see something you object to here. Or you can also, because y is the square bracket option here, that means it's the default.

If we just hit ENTER, that's the same thing as y and then enter. All right, now this may take a moment. I want to talk, oh, great, it's done.

Then I now want to talk about our syntax we just used. We just did this conda create command here. Let me pull that up again down here where we're working.

And I want to demystify this terminal line, because this is a fairly complicated line of terminal syntax, which is a little different from Python, but I think if you squint at this, you can see the Python syntax. The equivalent Python syntax would be this. And you don't need to write this, because this is Python.

It's not going to be valid terminal command. So, this would be the equivalent. And you can see they're extremely similar.

Command Line Usage

So, conda is our basic command, like a library. In fact, we did install Anaconda like it's a library. And then it has a little method, a subcommand.

In Python, this would be a method on an object. Conda would be the object. Conda is a terminal program, and it has a second command you can enter.

I want to create something with conda. And then we have the equivalent of we're executing conda create with some arguments. And just like here, where we can have some named arguments, we can have a named argument here.

The syntax is different. In fact, the syntax is different here, although it's closer to Python's syntax. So, we're saying create one whose name is dvenv and whose Python version is 3.9. And you can see it's very similar syntax when you come right down to it.

Keyboard Shortcuts

So, there's nothing, you know, magic happening here on the terminal. It's just an environment like Python, except it works in a different way, and it works more, you know, on a basic level of your computer. All right.

Let me exit out of that. You can not run a terminal command. I mean, I didn't want to run that by choosing with the keyboard shortcut control C. And that's what I did there.

All right. So, we've created an environment, and we can activate it by doing conda activate dvenv, as it says up here. Conda activate and then the name of the environment you want to activate.

Layers and Organization

Now, there's no output from that directly, but we did see something change here, which is this dvenv here. We did have the terminal, say, base, and its prompt area here. Now, instead, it says dvenv and its prompt area.

And that's the environment. It was already telling us the name of the environment, which was just the base, the global default Python environment, but now we've activated another one. If we want to go back to the base or activate a different one, we can do conda deactivate.

We do that again, no printing. It'll only tell you that if something went wrong, but we're back to base. I want to also highlight how I wrote those commands so quickly, which is, if you've written a command in this session of your terminal, you could press the up arrow on your keyboard to go back to the previous commands you've entered and press down on it to go back forward in history.

So, up arrow goes back in history, down arrow goes forward in history. So, if I want to activate it again, I could go from here. Instead of already typing conda activate dvenv, I'm going to press up.

Oh, that's my previous command, conda deactivate, and my previous to previous, my next to last command entered, up, press up arrow again to get to that. And then you can edit this if you need to. Sometimes you do, like you type in something wrong and then you press up to get it back and correct it and type press ENTER again, but I just press up till I get to conda activate dvenv and enter again.

And now I'm back in the data visualization environment that I wanted. Okay, we're going to do a couple more commands to configure and install our environment in the next part.

RELATED ARTICLES