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 Pygator

Pygator on Oct. 12, 2019

Do you know how to set the interpreter like that for VS code? I recently tried this IDE out and like the bundled terminal with it, but i also got into Sublime awhile back after Dan mentioned it’s awesomeness.

Avatar image for Martin Breuss

Martin Breuss RP Team on Oct. 16, 2019

There are a couple of great resources on VSCode here on Real Python. It’s definitely been gaining popularity and is quite similar to Sublime Text, if you’re already familiar with that one.

Avatar image for Kevin Walsh

Kevin Walsh on Feb. 19, 2020

two questions: 1. can you create the virtual environment just using pycharm? or do i have to do it outside of pycharm and then import if needed? 2. when i start pycharm it shows python 3 with Django. Do i have to create the environment as shown or is it built in with Pycharm?

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 21, 2020

Hi @Kevin Walsh. PyCharm can automatically create a venv for you when you start up a new project.

If you say New Project instead of Open you get to a dialog where you only need to click on Create and the virtual environment will be made for you by PyCharm.

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 21, 2020

Also check out the walkthrough regarding that here in our PyCharm section of the Editor Course.

Avatar image for fredmord

fredmord on Feb. 21, 2020

I am stuck with how I can set up pycharm environment.

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 21, 2020

What are you stuck with @fredmord? Did you check out the resource I linked above? I’m happy to try to help, so please send me some more info so we can try to tackle the issue :)

Avatar image for tomkristensen0372

tomkristensen0372 on Feb. 25, 2020

source .env/bin/activate. does not work in Windows. Does someone have a recommendation how we solve this in Windows 10 ? Thanks!

Avatar image for Geir Arne Hjelle

Geir Arne Hjelle RP Team on Feb. 25, 2020

Hi Tom, on Windows you should use something like the following instead:

C:\> venv\Scripts\activate.bat

Ref: realpython.com/get-started-with-django-1/#set-up-your-development-environment

Avatar image for Ricky White

Ricky White RP Team on Feb. 26, 2020

Hi Tom.

If you are using cmd (Command Prompt) then use Geir’s suggestion above. However, if you are using Powershell, the use activate.ps1 instead if activate.bat at the end of your path.

Avatar image for fredmord

fredmord on Feb. 27, 2020

I am confused. don’t know either you are using mac or windows for your tutorials. I’m completely stuck on how to set up the environment. Do I have use the Terminal or Pycharm?

Avatar image for fredmord

fredmord on Feb. 27, 2020

How do I create a virtual environment? Can’t I use pycharm to learn the tutorials?

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 27, 2020

Hi @fredmord. There are different ways to get to the same goal, you can do it through your OS’s terminal or let PyCharm do the work.

For simplicity’s sake, I’d suggest you to use PyCharm. It automatically sets up a new virtual environment for you when you start a new project.

So just start a new project, verify that you can see the virtual environment folder as visible in the video I linked, and that when you open your PyCharm terminal, you can also see that the virtual environment is activated. This is also shown in the linked video.

Hope that helps! If you want to dive deeper and understand what virtual environments are about, why they are recommended to use, how to set them up etc., please check out the article on virtual environments that is also posted at the top of this page.

Avatar image for fredmord

fredmord on Feb. 27, 2020

Okay, thank you. Then I will follow your video and use pycharm to learn.

Avatar image for fredmord

fredmord on Feb. 27, 2020

At pycharm I chose new project from file menu and I selected Django

Avatar image for fredmord

fredmord on Feb. 27, 2020

“projects can either be opened in a new window or replace the project in a existing window or be attached to the already opened projects.” “How would you like to open the project?” This Window. New Window. Attach. Cancel.

The above is what I found when I started iniated a new project from pycharm.

Thank you.

Avatar image for fredmord

fredmord on Feb. 27, 2020

(venv) C:\Users\hp\PycharmProjects\untitled>

Please the above was what I found in my terminal.

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 28, 2020

Looks okay @fredmord. Only thing is that it seems you didn’t input a name for your Django project, so it is called untitled.

You could try to make a new one and give it a name, e.g. django-porfolio and select to open it in This Window.

Your terminal prompt shows that PyCharm successfully created a virtual environment called venv for you and that it is activated:

(venv) C:\Users\hp\PycharmProjects\untitled>

^^ the (venv) part at the beginning indicates that all went well.

You can now install Django:

(venv) C:\Users\hp\PycharmProjects\untitled> pip install django

although it might already be installed, since you explicitly started a Django project with PyCharm, and therefore PyCharm might have already done also that for you.

In short: You’re good to go. :)

Avatar image for fredmord

fredmord on Feb. 28, 2020

I am grateful sir.

Hi, when I tried to do

(.env) zhou@TJ-MacBook-Pro django-portfolio % pip install django

It showed the following message, I don’t know what to do to fix it.

/Users/zhou/Desktop/Personal-projects/django-portfolio/.env/bin/pip: line 2: /Users/zhou/Desktop/Personal Projects/django-portfolio/.env/bin/python3: No such file or directory
/Users/zhou/Desktop/Personal-projects/django-portfolio/.env/bin/pip: line 2: exec: /Users/zhou/Desktop/Personal Projects/django-portfolio/.env/bin/python3: cannot execute: No such file or directory
Avatar image for Martin Breuss

Martin Breuss RP Team on March 16, 2020

Hello @TJ, looks to me like you might have deleted your virtual environment?

Double check that the .env folder exists in your current directory and that it contains a bin/ folder that again contains the python3 executable.

The error message currently tells you that it can’t find the python3 executable, which is why the install command fails.

You can delete and re-create the virtual environment in case something is missing in there.

Avatar image for Bill Lafferty

Bill Lafferty on May 21, 2020

Hi Martin,

When setting up the virtual environment, ‘python3’ is not recognized.

I’m on Windows 10, and am using Powershell through Anaconda. I referenced here for the setup.

Is it okay to use python -m venv .env instead? Or will that default to an older version of python?

Avatar image for Ricky White

Ricky White RP Team on May 21, 2020

Hi Bill.

The whole python3 thing is for UNIX based machine. In windows we have it a little easier, we just use the python command. This will use the default version of python. If you have several version installed on your machine, you can check the default version with python --version.

For your virtual env, you can use the python -m venv .env command without issue. Again it will use your default python version. Python 2 does not come pre-installed on windows as it does with UNIX operating systems, so we don’t have to deal with that incompatibility.

Hope that helps.

Avatar image for Bill Lafferty

Bill Lafferty on May 21, 2020

Hi Ricky,

Worked, thanks! I’ve set up the venv, but am unable to activate it. I see above there is a thread about activation on Windows.

I tried the following line (changed the venv name to virtual_env because I’m a hopeless noob)

virtual_env\Scripts\activate.ps1

Which lead to a Fully Qualified Error ID: Could not auto load module.

REsearched it some more and found this instruction, just want to make sure I’m on track?

*On Microsoft Windows, it may be required to enable the Activate.ps1 script by setting the execution policy for the user. You can do this by issuing the following PowerShell command:

PS C:> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser*

Avatar image for Bill Lafferty

Bill Lafferty on May 22, 2020

Hi all,

I’m unable to load the virtual environment. (Using Windows 10.)

  1. I created the django_portforlio folder as Martin instructed in the video
  2. Created ‘virtual_env’ (instead of venv)
  3. Set the Execution Policy to Unrestricted for CurrentUser, and then confirmed it using Get-ExecutionPolicy -List
  4. In djagno_portfolio, I tried virtual_env\Scripts\activate.ps1 and got error –> “virtual_env could not be loaded” (+ FullyQualifiedErrorId: CouldNotAutoLoadModule
  5. cd’d to the .virtual_env folder
  6. Tried virtual_env\Scripts\activate.ps1 again and got same error

NEXT STEP - Now that the ExecutionPolicy is unrestricted, should I start over from scratch?

NOTE - I’m signed in as admin in PowerShell

Thanks! Bill

Avatar image for Ricky White

Ricky White RP Team on May 22, 2020

Hi Bill. When you renamed the env did you do so by just changing the name of the folder? Or did you create it with your preferred name from the start? i.e. Did you type python -m venv virtual_env?

You shouldn’t need to adjust the privileges if it’s your own machine? Are you using a work/school computer?

Avatar image for Bill Lafferty

Bill Lafferty on May 23, 2020

Hi Ricky,

I was on my work pc (had my manager’s blessing but didn’t have admin rights). Switched to my personal laptop and it works fine.

Thanks! Bill

Avatar image for Ricky White

Ricky White RP Team on May 24, 2020

Glad we got to the bottom of it :)

Avatar image for 99janna

99janna on June 3, 2020

Hi Martin,

I’m using Atom on Mac. I was successfully able to get the folder created and go through all the steps to create the virtual environment. But I’ve gotten stuck now at the point where you have the terminal within your IDE displaying “(.env) martin@realpython$”, and Atom doesn’t have the same preferences to change like you did in the video. Is there an easy way to change the path within the terminal of Atom so that it also gives the (.env) path?

Thanks!!

Avatar image for Martin Breuss

Martin Breuss RP Team on June 3, 2020

Hi @99janna, to see that prompt you will need to activate the virtual environment that you created. You can do this in the same way as described in the video inside of the terminal that is included with VSCode.

You can acces the integrate terminal in VSCode by pulling up from the bottom of your window, or by using the menu items or shortcuts described in their official docs on the Integrated Terminal.

You can learn more in our VSCode Setup Guide and the associated video tutorial.

The command to activate your virtual environment is:

source <your_venv_name>/bin/activate

Hope that helps! :)

Avatar image for osintalex

osintalex on June 20, 2020

Hi. For some reason I got a command not found error when trying to run django-admin.

I’m using zsh on Mac; I uninstalled Django with pip, installed again with sudo pip install django and it worked fine.

Avatar image for mathieu01

mathieu01 on July 11, 2020

Keep getting “System cannot find the path specified” on command prompt on windows when I try to activate. Any help please?

Avatar image for Marijan Milovec

Marijan Milovec on Sept. 15, 2020

Will course be updated for Django 3, and if I am using django 3 what are differences for me? Thank you

Avatar image for Martin Breuss

Martin Breuss RP Team on Sept. 17, 2020

Hi Marijan, we’re currently not planning to update it to Django 3, frankly because there would be nothing that you need to do differently for building this app. :)

✨ You should be able to follow this tutorial as-is also on Django 3.

Avatar image for Martin Breuss

Martin Breuss RP Team on Sept. 17, 2020

Hi @mathieu01, sorry I missed your comment here!

If there’s someone else stumbling across troubles that might be related to your virtual environment when using Windows, check out some alternatives you can use in A Primer on Virtual Environments and An Effective Python Environment.

Tl;dr:

Hope that helps someone!

Avatar image for Kyle Martin

Kyle Martin on Oct. 9, 2020

For those with PyCharm installed already and had issues installing a venv in terminal (outside of pycharm) here was my fix. Keep in mind everything before my created Python folder was my hard drives letter. Yours may be C: or D: or Z:. I will not include the hard drive letter below.

  1. Create the directory you want where you want. I made a \Python\RealPython\django-portfolio folder
  2. Open PyCharm. File > Open… Open your created folder
  3. File > Settings. Project: django-portfolio. Project Interpreter.
  4. Cog Wheel. Add… New Environment. Location: \Python\RealPython\django-portfolio\ new folder name it . env.
  5. It should look like \Python\RealPython\django-portfolio.env
  6. Ok. Apply. Ok.
  7. On your django-portfolio folder in your PyCharm project right click and select “Open in terminal”
  8. You should see (.env)\Python\RealPython\django-portfolio
  9. Type “pip install django” without the quotes and upgrade if needed.
Avatar image for Martin Breuss

Martin Breuss RP Team on Oct. 9, 2020

Nice, thanks for posting @Kyle Martin. Here’s another nice Youtube tutorial that walks you over how to create a new virtual environment for your PyCharm project, maybe it helps with some visuals:

www.youtube.com/watch?v=W8C097f6Hcg

Avatar image for SreenivasaRao

SreenivasaRao on Jan. 22, 2021

Hi, I am having both 2.7 and 3.7 Python versions in my Windows system. PyCharm is recognizing only 2.7. How can I change it to 3.7?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Jan. 22, 2021

@SreenivasaRao The recommended practice is to create separate virtual environments for each project. Regardless of your operating system, you can select an existing virtual environment or create a new one in PyCharm. You may also pick one of your system-wide Python interpreters installed globally if you really want. The specific instructions can be found on JetBrains help page.

Avatar image for Patrick

Patrick on Jan. 10, 2022

When setting up the Development environment i have noticed a slight difference in what you type a command in , Visual Studio Code, Linux terminal or PyCharm terminal

Avatar image for Martin Breuss

Martin Breuss RP Team on Jan. 11, 2022

What differences did you notice @Patrick and which of the programs are you using?

Avatar image for siyalohngcobo

siyalohngcobo on April 13, 2022

Hi Guys. I’m using PyCharm. Which way can I create my virtual environment using PyCharm and not the terminal?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on April 13, 2022

@siyalohngcobo There are a few ways to create a new virtual environment in PyCharm. When you make a new project from scratch, the IDE will ask you whether you want to create a new virtual environment or choose an existing one. For a project you have already opened, there will be a dropdown menu in the bottom right corner of the window with options to manage the Python interpreter currently assigned to your project.

Head over to the official documentation for more information about creating and managing virtual environments in PyCharm: www.jetbrains.com/help/pycharm/creating-virtual-environment.html#python_create_virtual_env

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.

Become a Member to join the conversation.