DEV Community

Discussion on: What are the codes to run in order, for a Django project? I have python3 and pip installed

Collapse
 
rehmatworks profile image
Rehmat Alam • Edited

Apart from a dockerized solution discussed by Robert, the simplest way to get started is to create a virtual environment on your local machine to install the packages needed by your Django application. Here are the steps that I follow whenever I need to work on a new project.

Step 1: Virtual Environment

I have created a venv directory in my home directory and I create virtual environments there. I create a new virtual environment for each of my projects to keep things separated and clean.

virtualenv -p python3 ~/venv/newproject

Step 2: Create a Project

Navigate to the directory where you want to create your project and then execute this command:

django-admin startproject newproject

Step 3: Activate Virtual Environment

Activate the virtual environment, and install Django in your virtual environment.

source ~/venv/newproject/bin/activate
pip install Django

That's it. Now you should install all the requirements of your project keeping the virtual environment activated and thus you will be able to freeze your requirements to requirements.txt cleanly. Moreover, you will be able to keep the rest of your main Python environment clean on your local machine.

I hope this helps. This is what I follow as a practice while building Django projects. And when it comes to deploying the project to the server, I freeze the project's requirements to a requirements.txt file from the virtual environment that I created just for that project.

Collapse
 
yobretyo profile image
Bret

thank you!!
this is what I've figured out so far.... is this good as well?:
just by entering/running....

1) python3 -m venv customname-env

2) source customname-env/bin/activate

3) python -m pip install Django

Collapse
 
rehmatworks profile image
Rehmat Alam

Yep, this is exactly what I've mentioned above. You are on the right track :)

Thread Thread
 
yobretyo profile image
Bret

but, I need a ENV first?

Thread Thread
 
rehmatworks profile image
Rehmat Alam • Edited

You can create the virtual environment before creating your project or after creating the project. To create the project, you use django-admin command and I assume that django-admin command is available globally on your machine. Once the project is created, you should create a virtual environment for it and then you should install all the requirements of the project in that virtual environment (including the main Django package).

So the virtual environment's role here is just to allow you a dedicated place to install your project's requirements. This way, you don't mess your system with the packages required by a certain project.

p.s. if django-admin command is not available globally in your terminal, first create the virtual, activate it, install Django package, and then create your project.