본문 바로가기
언어/PYTHON

[Django] Python 환경 구성

by HmHjj 2022. 11. 30.
728x90
반응형

Django 사용을 위한 Python 구성 

python을 사용할 수 있는 환경에서 빠르게 서버를 운영할 수 있는 Django 설치를 진행해 보겠습니다. 

환경 구성 정보

ubuntu 20.04 LTS

 

python 설치 확인

ubuntu 설치 명령어 :  apt install python3 

python3 설치 확인 및 버전 확인 : python3 --version 

 

root@ubuntu2004:~# apt install python3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3 is already the newest version (3.8.2-0ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 131 not upgraded.

root@ubuntu2004:~# python3 --version
Python 3.8.10

 

가상 환경 설치 및 확인

필요한 python 명령어 설치 

pip 명령어 사용을 위한 패키지 설치 명령어 : apt install python3-pip

root@ubuntu2004:~# apt install python3-pip
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libexpat1-dev libpython3-dev libpython3.8-dev python-pip-whl python3-dev python3-wheel python3.8-dev
The following NEW packages will be installed:
  libexpat1-dev libpython3-dev libpython3.8-dev python-pip-whl python3-dev python3-pip python3-wheel python3.8-dev
0 upgraded, 8 newly installed, 0 to remove and 131 not upgraded.
Need to get 6,649 kB of archives.
After this operation, 25.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
....
....
Setting up python3-wheel (0.34.2-1) ...
Setting up libexpat1-dev:amd64 (2.2.9-1ubuntu0.6) ...
Setting up libpython3.8-dev:amd64 (3.8.10-0ubuntu1~20.04.5) ...
Setting up python3.8-dev (3.8.10-0ubuntu1~20.04.5) ...
Setting up python-pip-whl (20.0.2-5ubuntu1.6) ...
Setting up libpython3-dev:amd64 (3.8.2-0ubuntu2) ...
Setting up python3-dev (3.8.2-0ubuntu2) ...
Setting up python3-pip (20.0.2-5ubuntu1.6) ...
Processing triggers for man-db (2.9.1-1) ...

설치 완료 되면 pip 명령어를 사용할 수 있다. python에 관련된 패키지를 다운로드하는 것이 주목적인 명령어이다. 

root@ubuntu2004:~# pip

Usage:   
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and
                              CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair as trusted, even though it does not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with
                              --no-index.
  --no-color                  Suppress colored output
  --no-python-version-warning
                              Silence deprecation warnings for upcoming unsupported Pythons.

 

가상 환경 구성에 필요한 pipenv 설치 

pip install pipenv명령어로 python 사용 시 가상 환경을 이용하여 실행되도록 설정한다. 

이 가상환경을 구성하는 이유는 가상 환경으로 사용 시에 패키지에 따른 종속성을 없기에 사용하면 다른 패키지 사용이나 패키지 변경할 경우 다양하게 독립적인 환경 구성이 가능하다. 

가상 환경 종류에는 pipenv , virtualenv , anaconda , venv 여러 가지가 있으니 찾아보고 괜찮은 거 골라서 해도 좋다. 

간단히 테스트 용이면 venv 가 python 3.5 버전 이후에 지원되는 기능이니 설치 없이 사용이 가능하다. 하지만 결국에 적용한 패키지를 목록화하여 다시 설치 가능하게 하는 기능을 사용하려면 pipenv 나 virtualenv를 써야 하니 잘 선택하자. 

root@ubuntu2004:~# pip install pipenv
Collecting pipenv
  Downloading pipenv-2022.11.25-py3-none-any.whl (2.9 MB)
     |████████████████████████████████| 2.9 MB 12.1 MB/s 
Collecting virtualenv-clone>=0.2.5
  Downloading virtualenv_clone-0.5.7-py3-none-any.whl (6.6 kB)
Collecting virtualenv
  Downloading virtualenv-20.17.0-py3-none-any.whl (8.8 MB)
     |████████████████████████████████| 8.8 MB 11.1 MB/s 
Requirement already satisfied: certifi in /usr/lib/python3/dist-packages (from pipenv) (2019.11.28)
Requirement already satisfied: setuptools>=36.2.1 in /usr/lib/python3/dist-packages (from pipenv) (45.2.0)
Collecting platformdirs<3,>=2.4
  Downloading platformdirs-2.5.4-py3-none-any.whl (14 kB)
Collecting filelock<4,>=3.4.1
  Downloading filelock-3.8.0-py3-none-any.whl (10 kB)
Collecting distlib<1,>=0.3.6
  Downloading distlib-0.3.6-py2.py3-none-any.whl (468 kB)
     |████████████████████████████████| 468 kB 11.3 MB/s 
Installing collected packages: virtualenv-clone, platformdirs, filelock, distlib, virtualenv, pipenv
Successfully installed distlib-0.3.6 filelock-3.8.0 pipenv-2022.11.25 platformdirs-2.5.4 virtualenv-20.17.0 virtualenv-clone-0.5.7

 

설치가 완료 되면 가상 환경을 다음 명령어로 시작해 보자. 

시작 : pipenv shell

종료 : exit

root@ubuntu2004:~# pipenv shell
Creating a virtualenv for this project...
Pipfile: /root/Pipfile
Using /usr/bin/python3 (3.8.10) to create virtualenv...
⠇ Creating virtual environment...created virtual environment CPython3.8.10.final.0-64 in 623ms
  creator Venv(dest=/root/.local/share/virtualenvs/root-BuDEOXnJ, clear=False, no_vcs_ignore=False, global=False, describe=CPython3Posix)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==22.3.1, setuptools==65.6.3, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/root-BuDEOXnJ
Creating a Pipfile for this project...
Launching subshell in virtual environment...
 . /root/.local/share/virtualenvs/root-BuDEOXnJ/bin/activate
root@ubuntu2004:~#  . /root/.local/share/virtualenvs/root-BuDEOXnJ/bin/activate
(root) root@ubuntu2004:~#

가상 환경이 시작되면 Pipfile 이라는 환경 파일이 생성된다 이 파일에 설치 및 가상 환경 설정한 내용들이 업데이트된다. 

 

root에서 작업하긴 좀 그러니 OS에 user 추가해서 진행해 보자. 

Django 설치

pip 명령어로 django를 설치해 보자. 

(test) test@ubuntu2004:~$ pip install django
Collecting django
  Downloading Django-4.1.3-py3-none-any.whl (8.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.1/8.1 MB 10.7 MB/s eta 0:00:00
Collecting asgiref<4,>=3.5.2
  Downloading asgiref-3.5.2-py3-none-any.whl (22 kB)
Collecting backports.zoneinfo
  Downloading backports.zoneinfo-0.2.1-cp38-cp38-manylinux1_x86_64.whl (74 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 74.0/74.0 kB 13.3 MB/s eta 0:00:00
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.4.3-py3-none-any.whl (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.8/42.8 kB 2.4 MB/s eta 0:00:00
Installing collected packages: sqlparse, backports.zoneinfo, asgiref, django
Successfully installed asgiref-3.5.2 backports.zoneinfo-0.2.1 django-4.1.3 sqlparse-0.4.3
(test) cubecm@ubuntu2004:~$

그냥 설치를 진행 했으니 최신 버전인 4.1.3으로 설치된다.  버전 확인은 아래 명령어로 가능하다. 

(test) test@ubuntu2004:~$ python -m django --version
4.1.3

 

이제 Django 설치까지 완료 했으니 프로젝트 작업 시작하면 된다. 

728x90
반응형

'언어 > PYTHON' 카테고리의 다른 글

[Python] Selenium 예약 프로그램 기능  (0) 2023.04.11
[Django] Custom User 적용 AbstractUser  (0) 2022.12.08
[Django] Custom User 적용  (0) 2022.12.02
[Django] 프로젝트 실행  (1) 2022.12.01

댓글