Set Up Your Development Environment: Recap
In this section, you:
-
Created a virtual environment in the CLI:
ShellCopied!$ python3 -m venv .env
-
Activated the virtual environment:
ShellCopied!$ source .env/bin/activate
-
Installed Django inside the virtual environment:
ShellCopied!(.env)$ pip install django
-
Created a Django project management app inside the existing folder without duplicating the folder structure:
ShellCopied!(.env)$ django-admin startproject portfolio .
-
Visited the site at
http://localhost:8000
In the next section, you’ll start building a Django application inside the project that you just created.
00:00 Hey, and welcome to the final video in this Part 2 section, where we’re setting up our development environment. This is just going to be a quick recap and an outlook on what we’re going to do afterwards.
00:13
So, what you did in this section is: you created a virtual environment using the command line, and we use the command python3
make virtual environment—using this module—and then calling it .env
. Remember, you can name this whatever you want to. Next, we activated the virtual environment using the source
command, and then you have to give the path to the activate
script, which is [your environment name]/bin/activate
.
00:39
Then, you can see that you’re inside of the virtual environment, seeing this (.env
) thing on the left. After we activated it, the next thing we did is we installed Django.
00:50
We made sure we were inside of the environment, and then said pip install django
, which ran a couple of installs. Next, we created the Django project.
01:00
In this case, we made sure to not duplicate the folder structure, because we had created our folder outside. So we used this little .
(dot) here, at the end. We said django-admin startproject
which is the command you use, then gave it a name. In our case, that was portfolio
.
01:16
And then we use this little .
to just avoid this extra folder structure. And with this, we created our Django project, and the management app, and manage.py
file, which is something that we’re going to use a lot throughout working with Django.
01:30
And finally, we started the development server by typing runserver
, and then we visited the site, at http://localhost
port 8000
, and we saw something that looks similar to this. Great! In the upcoming section, Part 3, we’re going to start building a Django application.
01:49 So, that means inside of our Django project that we just created now, we’re going to make a separate app. And you remember from Part 1, the structure of a Django project includes often multiple apps, and we’re going to start off by building our first app in Part 3.
Martin Breuss RP Team on Oct. 16, 2019
Thanks for mentioning @regiscorblin! I’ve been working on UNIX machines, which creates a different folder structure than on Windows. Check for more info here in the docs.
But you’ve already mentioned it: it’ll be in the Scripts\
folder.
C:\> <venv>\Scripts\activate.bat
Gascowin on Oct. 18, 2019
I simply did the following (on Windows):
pip install pipenv
pipenv shell
to create the virtual environment.pipenv install django
to install the package.
Anytime I navigate to the folder and do ‘pipenv shell’ it activates the existing virtual environment or creates a new one if it does not exist already.
Also I was glad to know that: django-admin projname .
(with a period at the end) prevents the creation of the inner directories which in the past were very confusing for me. I often wrestled with whether I had to be in the projname directory as opposed to being in the projname/projname directory.
Thanks for that!
Martin Breuss RP Team on Oct. 18, 2019
pipenv
is a great way to handle virtual environments! There are plans for it to eventually replace Python’s current standard packaging manager pip
, and I hope it’ll happen.
It’s definitely worth to look into their docs and give it a try. For a walkthrough on using it, you can also check out this Guide on using pipenv.
Yeah, same for me! This little .
at the end of the django-admin startproject
command is a life-saver :)
Ravinder Reddy Pullagurla on Dec. 21, 2019
Excellent step by step explanation. I’m impressed with this course.
fredmord on March 4, 2020
Please the server or localhost:8000 didn’t appear after typing “django-admin startproject portfolio.” What is wrong with setup please?
Thanks for helping.
Martin Breuss RP Team on March 5, 2020
Hi @fredmord. After creating your project with that command, you need to cd
into the new folder. Then you need to start the development server with:
python manage.py runserver
The command you posted only creates a folder structure and a couple of files, but doesn’t start the server at localhost.
fredmord on March 6, 2020
“python: can’t open file ‘manage.py’: [Error 2] No such file or directory “
Please this is what I received after typing “python manage.py runserver.”
Please may I know what is wrong?
Thank you.
Martin Breuss RP Team on March 6, 2020
You need to be in the right directory. As I mentioned above, you will need to move into the right folder first. Make sure that when you type:
ls
that you can see the file manage.py
listed. If that is the case, you can run the command.
fredmord on March 8, 2020
“python: can’t open file ‘manage.py’: [Error 2] No such file or directory “
Please this is what I received after typing “python manage.py runserver.”
Please may I know what is wrong?
Thank you.
fredmord on March 8, 2020
I am stuck, nothing seems to be working for me to progress therefore, I have to quit for the mean time.
Martin Breuss RP Team on March 9, 2020
Hi @fredmord, sorry to hear that you haven’t yet solved your issue. Taking a break can often be helpful, but make sure to get back to trying!
Double-check the responses I posted to you further up. The error you are getting tells you that either:
- You are in the wrong directory (typing
ls
doesn’t show a file calledmanage.py
) - Or you haven’t yet created the Django project with the
startproject
command, which creates the folder structure and also yourmanage.py
file
Make sure you understand how files and folder structures work and practice using the command-line interface a bit, then come back here and give it another go. You can do it! 💯
fredmord on March 9, 2020
Please my current command prompt is: “(venv) C:\Users\hp\PycharmProjects>”
Martin Breuss RP Team on March 9, 2020
Ah! You are on Windows! 💡 That means your command-line works a bit differently than on UNIX systems (Mac and Linux). Check out this resource for some basic commands. To list the files in your directory, you need to run the command dir
.
Okay, so I hope that clears some things up. It certainly does for me :) Now, if you ran the command:
django-admin startproject portfolio
inside the folder at your current path, you need to next type:
cd portfolio
Now you should be inside a folder that contains the file manage.py
. Check up on that with typing:
dir
You can now run your development server with:
python manage.py runserver
Does it work for you?
fredmord on March 9, 2020
Below is what I had:
“You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for the app(s): admin, auth, contenttypes, sessions. Run ‘python manage.py migrate’ yo apply them.”
Please kindly throw more light on this.Thanks.
fredmord on March 9, 2020
The rest are:
“Django version 3.0.4, using settings ‘portfolio.settings’ Starting development server at 127.0.0.1:8000/ Quit the server with CTRL-BREAK.”
Now I don’t know what to do with the above information. The command prompt is just blinking and and I’m stuck.
Martin Breuss RP Team on March 10, 2020
That output means you started your development server successfully and you can now go to 127.0.0.1:8000
to access your Django app.
Please consider to continue watching the videos of this course, since I walk you over all of this in the upcoming content sections :)
Also, if you would like more real-time support it will be much easier if you post your questions on the community slack channel.
Good job for getting the development server running, and keep on working through the course material!
fredmord on March 10, 2020
Ok. Thanks.
emalfiza on March 17, 2020
Again, Martin you really made it easy for me to create a virtual environment. I would love to hear from you what a virtual environment is, why we need it? I googled it tho, but still not clear. And also I install django for this portfolio project and the django version is: Django==3.0.4, whereas you used different version, What you recommend shall I stick to this version coz I dont want to face hiccup uphill. Thank you indeed.
Martin Breuss RP Team on March 18, 2020
Hi @emalfiza! Glad the videos are helpful for you :)
More On Virtual Environments
We’ve got a great article and a video tutorial on virtual environments right here on Real Python. 🐍
Install Specific Package Versions
Generally it’s a good idea to stick with the same version as the tutorial. Exactly for the reason you mentioned: to avoid running into hiccups uphill.
I think you should be fine in this case even though it’s a big version bump from 2 to 3 for Django, but if you want to install the specific Django version you can type the following:
pip uninstall django
pip install django==2.2.1
Keep it up! 🙌
emalfiza on March 18, 2020
Cool, I finally understand the importance of venv…I am just revert back to the version of django that you used for the video. excited to learn more from you amazing pythonista.
Become a Member to join the conversation.
regiscorblin on Oct. 13, 2019
Hi, just finished part 2. It is probably obvious for experienced dev but on Windows venv doesn’t create the same folder structure. The interpreter is in Scripts not bin folder. Love it so far, the pace is perfect.