Loading video player…

Start Setting Up Your Development Environment

Time to start setting up your development environment! The IDE you’ll see used in this course is PyCharm. This IDE has some advantages because it will perform some tasks for you automatically. For example, it creates a virtual environment when you start a new project. In this course, you’ll still see how to set one up by hand.

First head over to your terminal and make a new folder called django-portfolio by using the mkdir command. Then, move into the folder called django-portfolio and create a virtual environment with venv. You’ll use the command python 3 -m venv .env, where .env is the name of your virtual environment.

Before you can install any packages in there, you still need to activate the virtual environment. Run the command source .env/bin/activate. Your prompt will change, and you’ll be inside a virtual environment, so any changes you make will be limited to this environment. Then you can install django with pip.

00:00 Okay. Let’s get started setting up this development environment. As I mentioned before, I will be using PyCharm for this. PyCharm has some advantages in that it does some of the things that we’re going to do now for you, automatically.

00:14 It creates a virtual environment when you start a new project. So, you wouldn’t have to do some of those things by hand, but to make sure that everyone can work along, we’re just going to set it up by hand. So, for this, I will head over to my terminal.

00:28 You can find your terminal either using Spotlight, typing “terminal,” and then just open up the app. I have it here on fullscreen so that it’s easier for you to see what’s going on.

00:40 I’m sitting inside of my Documents/ folder. Let’s take a look. This is where some knowledge of the command-line interface comes in handy. Inside of this folder, currently we have some Mac-generated files, a materials/ folder, and I have a folder portfolio-done/, which contains the finished project that we looked at in section one.

01:00 So what I’m going to do now, I’m just going to create a new folder in here. I’m going to use the mkdir (make directory) command and call it django-portfolio.

01:13 Okay, let’s check that again.

01:17 Now we have another folder in here called django-portfolio. Great.

01:23 We’ll move into that folder. What we want to do next is create a virtual environment. For creating a virtual environment, if you’re using anything above Python 3.5, you will be using venv.

01:38 Over here, inside of my new folder, I’m going to say python3, because I want to use the Python 3 version, -m, for module—I always think about it as being for make, because we want to make a virtual environment.

01:52 So, I’m using this venv module and I’m going to create a virtual environment that I will name here .env. You can name this anything, so this could be my_venv, or whatever you want to call it, but I will call it .env. This . (dot) makes it a hidden folder and, generally, you don’t want to change anything inside of the virtual environment.

02:12 So, we wait for this to be done. Now,

02:17 you can see that inside of this folder we have a new folder called .env. This contains our virtual environment. Now, before we can install any packages in here to make sure that they’re safe from the rest of the computer, we will still need to activate the virtual environment.

02:34 I will show you how that goes. We need to source it, so I can say source .env/bin/activate. This is just a folder structure, so I’m going inside of .env/ folder, inside of the bin/ folder, and then I’m then sourcing the activate script.

02:52 After running this command, you can see our prompt changed here. I have (.env) sitting here at the beginning, and this tells you you’re now inside of a virtual environment, which means you’re safe, good to go.

03:03 We can install packages and anything that happens is just going to stay constrained to this virtual environment. So, if we delete this .env/ folder, everything is going to be gone and your computer is not going to be affected by it.

03:17 Now we’re going to go ahead and install Django using pip.

03:22 That’s as simple as saying pip install django.

03:29 You can see, if you have a working internet connection, it’s going to fetch Django from the web, and it’s just going to install it inside of your virtual environment.

03:38 Let’s wait for this to be finished. We get a prompt that we should update pip. You don’t need to do this, but let’s go ahead and call this anyways.

03:54 All right, now with this command,

03:59 we can see which packages do we have installed in our virtual environment. And you can see there’s Django of a current version and some other things that come with the install of Django.

04:08 Great! That means we’re all set with Django. That essentially completes our setup of the development environment that we need. I’m going to show you now how to open it up in PyCharm. But here, if you’re using a different text editor, you will simply just open it up with that text editor of your choice.

04:28 So for this, I head over to PyCharm, open it up, and I’m not going to create a new project, which would do this creation of the virtual environment for me, but instead, I’m heading over to Documents/ and opening up this django-portfolio/ folder.

04:51 So, this is how my PyCharm looks when I open it up. And you can see, I have a specific theme. Yours is probably white if you open it up—don’t worry, you can change all this in the settings. What I’m going to do now also, to make it easier for you to see when I’m working on this, I’m going to activate the presentation mode.

05:13 Okay, we have the presentation mode, and I can switch back on our file structure here on the side, and what we also need is going to be the terminal.

05:30 Okay, and that should make it easy for us to see what’s happening.

05:36 Inside of here, I have the .env/ folder that we created outside of PyCharm. Now, if I had made this project just by creating a new project with PyCharm, this .env/ folder would already be connected and we would be using it automatically. I’m just going to make sure to connect it. Down here in the terminal, we can see that the virtual environment is already switched on by seeing this (.env) here in the terminal.

06:01 But because PyCharm is a complex program, sometimes things can go wrong, and then it can be difficult to find your way around. So if you open up your project

06:13 and you cannot see this (.env) before—let’s say it looks like this—then this would mean that PyCharm is currently not recognizing the virtual environment that we created outside.

06:26 There’s a way to fix that. I just want to show you this, probably you’re not going to have to use it, but in case, so that you know where to look. Okay, so you go to PyCharm and Preferences…, and you just scroll to your project settings here—that’s going to have the name of whatever the project is that you’re working on.

06:49 Then, you select Project Interpreter, and here, it’s going to tell you, okay, which is the project interpreter that you’re currently working with, and you can see the installed packages coming up here.

07:00 We see that ours is already activated here, which yours is probably also going to be, but if it gets some error or Django is not showing up here, then you’re going to have to connect to the correct one.

07:12 You can do this by clicking on this cogwheel symbol here and then Add…, and then we want to use our existing environment, so click Existing environment, and now we have to find the path to our Python interpreter.

07:27 You do this by clicking these three little dots here, and it’s going to open up the file structure. Now you have to navigate your file structure—this is my personal folder—into Documents/, and then inside of your project,

07:42 and here we are in the .env/ folder. So, you’re going to have to find the Python interpreter and that’s sitting inside of .env/, then inside of bin/, and now we want to choose the interpreter, and you can just use this python3 link here.

07:59 It’s going to direct you to the right Python installation, so that’s what you want to choose. So, you have to navigate the file structure all the way to your Python interpreter, then click OK. Now you see it’s showing up here.

08:13 Click OK again, and then apply this—Apply. And then, whatever you installed before when we created the virtual environment should show up here—the installed packages. Okay.

08:28 We’ll let PyCharm do its job, and now if I close this and open up a new terminal,

08:40 you can see we’re back again. (.env) shows up, and if I say pip freeze, it’s going to tell me all the installed packages. And again, we can see Django is installed here.

08:52 That shows us that we have our virtual environment activated, and that means we’re ready to continue working. And one more thing: if you’re still getting some kind of error popping up, just try to actually close PyCharm and open it again.

09:07 Sometimes there’s, like, a swap file where some information gets stored, and it gets refreshed if you just go PyCharm > Quit, and then open it up again.

09:15 I’m not going to do this now but just so you know, if you have set the virtual environment like this—you have the path defined there, and it’s still throwing an error—just try to restart. It’s the classic turn it off and on again.

09:29 So, we’re all set. This is our environment folder, and we can move on with the next section, where we’re going to create a Django project.

Avatar image for andrewcraigrn

andrewcraigrn on June 9, 2022

Hello!

I’m getting this error: “Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases” when I attempt to start the virtual environment. However, I have Python 3 installed on my computer. Any ideas?

Avatar image for andrewcraigrn

andrewcraigrn on June 9, 2022

Disregard. I figured it out. I actually didn’t have Python installed and I thought I did. I installed from Microsoft store and not it works fine.

Avatar image for Paula Poblete

Paula Poblete on Sept. 17, 2022

Is there documentation I can use to understand all this? I am confused by the concept of environment and what is to activate? should I now have a folder called “.env” ?

Avatar image for Alex Huang

Alex Huang on May 19, 2023

Hello,

I am working through this amazing tutorial on my windows computer, and I am stuck at a roadblock. I have replayed the video multiple times to make sure I didn’t miss a step. The problem is that my terminal is not recognizing the virtual environment I set up on my Django-portfolio folder. I tried the method of going into settings > interpreter > changing the interpreter to python3 inside ‘site-packages’, but python3 is non-existant. Furthermore, Pycharm doesn’t seem to know that I have Django installed (as seen in my terminal, I did!). Here are some photos: drive.google.com/drive/folders/1INfTkeYIBiOzfv8Tn20SUoH0aHnZ6PzL?usp=sharing

Thank you in advance for your help!

Avatar image for Martin Breuss

Martin Breuss RP Team on June 5, 2023

@Paula Poblete seems like I missed your question, so this comes too late 😬 but to re-iterate from some earlier comments, we have an in-depth tutorial on virtual environments that should be a good resource for anyone who wants to understand them better.

@Alex Huang like Rick mentioned earlier, the command on Windows is probably python instead of python3 (depends a bit on how you installed Python).

Unfortunately I can’t access the photos you shared on Drive.

Anyways, if PyCharm isn’t recognizing your Django installation, then you probably have the wrong interpreter selected. You can choose the interpreter and set it to the one inside your virtual environment.

Hope this helps!

Avatar image for blanca9

blanca9 on July 19, 2024

How can I work with VSCODE? I dont want to buy PyCharm License.

Avatar image for Martin Breuss

Martin Breuss RP Team on July 19, 2024

@blanca9 yes, you can use VS Code instead of PyCharm (or choose any other code editor/IDE). However, PyCharm also has a free community edition that you can use without buying a license.

Avatar image for Jess

Jess on Jan. 12, 2025

Hi! I noticed when I did “pip freeze” I got asgiref==3.8.1 instead of pytz. Is this going to cause anything I should be aware of? I am well versed in JS but brand new to python and django.

Thanks!

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Jan. 13, 2025

@Jess When you run pip freeze in your terminal, this command prints out all the third-party libraries that are currently installed in your virtual environment. Oftentimes, external libraries installed by hand come with their own set of transitive dependencies. In this case, asgiref is one of the dependencies of the Django framework, while pytz used to be one, but has been superseded by the standard library since Python 3.9 and is no longer required by Django. This course was recorded before the release of Python 3.9.

Avatar image for Jess

Jess on Jan. 13, 2025

@Bartosz That tracks with what I had thought, thanks so much! :D

Become a Member to join the conversation.