This article corresponds to ** Part.1 ** of the articles related to "Learn how to use Docker through building an environment for Django + MySQL".
In this article, we will first use ** venv
to build a Python3
virtual environment **.
Create a virtual environment for a specific project with venv
based on any version of Python3
prepared on the host PC with Pyenv
.
The purpose is ** to be able to manage the version of Python
on a project-by-project basis ** and to make the installation of packages by ** pip
more efficient / lightweight **.
This article uses ** Python 3.7.7
**.
# Python 3.7.7 installation
$ pyenv install 3.7.7
#3 version of Python3 to use.7.To 7
$ pyenv global 3.7.7
#Version confirmation
$ python3 --version
Python 3.7.7
** Create a directory for your project and then create a virtual environment in it **.
#Creating a project directory
$ mkdir django_starter
#Move directory
$ cd django_starter
#Creating a virtual environment
$ python3 -m venv .venv
The .venv
at the end of the command is the pathname. Here, it is set to .venv
according to the convention, but of course it is okay to put a directory between them to deepen the hierarchy or give it a name of your choice.
Then ** enable the virtual environment and confirm the activation **.
#activation
$ source .venv/bin/activate
#Check "Where are you using Python?"
(.venv) $ which python
(Abbreviation)/django_starter/.venv/bin/python
If the virtual environment name you gave earlier is displayed before the shell user name, you can confirm that it is enabled. I think that ** Python3` for this project is placed in the path of the virtual environment placed in the project, and I will use it **. By the way,
$ deactivate
You can get out of the virtual environment just by executing.
Use pip
in the virtual environment to install the required packages.
Install some of the ones that are confirmed to be used for the purpose of this article group in advance.
#Installation
(.venv) $ pip install django==3.0.8 mysqlclient==2.0.1 pathlib==1.0.1
#Check installed packages
(.venv) $ pip freeze
Next, create requirements.txt
based on these.
(.venv) $ pip freeze >> requirements.txt
The contents should look like this.
requirements.txt
asgiref==3.2.10
Django==3.0.8
mysqlclient==2.0.1
pathlib==1.0.1
pytz==2020.1
sqlparse==0.3.1
In the future, when sharing the environment or copying the environment into the container, installation will be performed based on the description in this file. ($ pip install -r requirements.txt
)
From the next time, I will set up Docker
related files, but before that, I will create a Django
project.
(.venv) $ pwd
(Abbreviation)/django_starter
(.venv) $ django-admin startproject config .
Of course, the name of the directory generated by the command can be anything, but if it is the same name as the parent directory, it will be confusing, so leave it as config
.
.
Is the path specification of the creation location.
django_starter
├── config
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── .venv
│ └── (Abbreviation)
└── requirements.txt
By using a virtual environment, you can ** avoid installing packages that are not related to the project and manage only what you need **. Similarly, if you have something that you only use for this project, ** you don't have to pollute your local main environment (?) **, which is very nice.
We will use this to build an environment inside the container that will be put into practice.
In the next article, we will consider ** the description of the Dockerfile **. Click here for more ↓ "2. Consider the description of Dockerfile"
Thank you for visiting.
Recommended Posts