Python Virtual Environments with Conda for Project Isolation

Create and activate a Python virtual environment using conda for isolated project-specific dependencies.

Create isolated Python development environments tailored to specific projects by leveraging virtual environments. This article explains how to use Anaconda's conda command to create and manage these environments efficiently for tasks like data visualization, machine learning, or API development.

Key Insights

  • Python virtual environments allow developers to install and manage project-specific libraries and versions, avoiding conflicts between dependencies across multiple projects.
  • Using the command conda create --name dvenv python=3.9, developers can create a new environment named "dvenv" with Python 3.9, while paying close attention to the syntax and spacing required by the terminal.
  • Noble Desktop demonstrates how to activate and deactivate environments using conda activate dvenv and conda deactivate, and emphasizes keyboard shortcuts like the up and down arrows to navigate command history efficiently.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

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.

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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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.

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.

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.

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.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram