To manage the libraries or packages you've used and installed in your Django project, you can use pip (Python's package installer) along with a requirements.txt file. Here's how to do it:
- To list all the installed packages in your Django project, you can create a
requirements.txtfile by running the following command in your terminal:pip freeze > requirements.txt - This command will generate a
requirements.txtfile that lists all the packages you've installed withpip, along with their versions.
- Make sure to include the
requirements.txtfile in your version control system (like Git) so that it can be shared across different devices or with other developers.
- When you switch to a new device or set up your Django project on another machine, you can install all the required packages by running:
pip install -r requirements.txt
- This command reads the
requirements.txtfile and installs all the listed packages.
- It's a good practice to use a virtual environment to manage your project's dependencies separately from other Python projects on your machine.
- You can create a virtual environment with:
python -m venv myenv
- Activate the virtual environment:
- On Windows:
myenv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
- After activating the virtual environment, you can install packages using
pip, and they will be isolated to your project.
- Whenever you install a new package, remember to update your
requirements.txtfile by running:pip freeze > requirements.txt
This approach ensures that your Django project's dependencies are well-managed and easily transferable across different devices, similar to how npm manages dependencies in Node.js projects.
- Create a new project
python -m venv myenv
myenv\Scripts\activate
django-admin startproject < project_name >
python manage.py startapp < app_name >- Alternatively you can use Pipfile to manage your dependencies
to install all your dependencies:
pipenv installwhen installing a new dependencies:
pipenv install < package_name >- Activate your Virtual Environment
myenv\Scripts\activateand then migrate your SQL files
python manage.py makemigrations
python manage.py showmigrations
python manage.py migrate- Run your app
python manage.py runserver- To create a Django Super User
python manage.py createsuperuser