프로젝트

django GCP로 배포 중 트러블 슈팅(Ubuntu운영체제)

Y0un9Ki 2024. 5. 11. 00:16

오늘 django를 배포하는 와중에 문제점이 생겼던 것을 해결하면서 해결한 것을 블로그에 작성하고자 한다.

 

https://srilankakim66.tistory.com/70

 

[Django] Django Google Cloud Platform를 이용하여 배포하기(SSH shell에 python 최신버전 다운)

오늘은 django에서 진행한 프로젝트를 google cloud platform에 배포를 진행 해볼 예정이다. @@ 필자는 이미 만들어놓은 django 프로젝트에 대해서 GCP에 배포를 할것이며 Git을 통해 레포지토리에 있는 장

srilankakim66.tistory.com

 

위에 블로그 중간에 보면 uduntu의 가상머신을 사용하면서 python의 최신 버전(3.11버전)이 다운이 안되는 문제가 생겼었는데 그것을 해결하면서 python3.11버전으로 가상환경을 만드는 방법과 python3.11버전을 다운받는 방법에 대해서 작성해 놓았다.


두번재 트러블 슈팅은 필자는 django를 사용하면서 쿼리문을 사용하기 위해서 mysqlclient라는 라이브러리를 다운받아서 사용했다.

그런데 ubuntu에 가상머신에서 mysqlclient를 pip를 이용해서 다운받으려고 하니 계속해서 에러가 뜨는 문제에 직면했다.

 

에러 메세지를 한번 보자.

더보기

(venv) srilankakim66@instance-20240510-021431:~/final_project$ pip install -r requirements.txt 
Collecting asgiref==3.8.1 (from -r requirements.txt (line 1))
  Downloading asgiref-3.8.1-py3-none-any.whl.metadata (9.3 kB)
Collecting Django==5.0.6 (from -r requirements.txt (line 2))
  Downloading Django-5.0.6-py3-none-any.whl.metadata (4.1 kB)
Collecting djangorestframework==3.15.1 (from -r requirements.txt (line 3))
  Downloading djangorestframework-3.15.1-py3-none-any.whl.metadata (11 kB)
Collecting mysqlclient==2.2.4 (from -r requirements.txt (line 4))
  Downloading mysqlclient-2.2.4.tar.gz (90 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 90.4/90.4 kB 1.7 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [30 lines of output]
      /bin/sh: 1: pkg-config: not found
      /bin/sh: 1: pkg-config: not found
      /bin/sh: 1: pkg-config: not found
      Trying pkg-config --exists mysqlclient
      Command 'pkg-config --exists mysqlclient' returned non-zero exit status 127.
      Trying pkg-config --exists mariadb
      Command 'pkg-config --exists mariadb' returned non-zero exit status 127.
      Trying pkg-config --exists libmariadb
      Command 'pkg-config --exists libmariadb' returned non-zero exit status 127.
      Traceback (most recent call last):
        File "/home/srilankakim66/final_project/venv/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/home/srilankakim66/final_project/venv/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/srilankakim66/final_project/venv/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-rcs32eg2/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-rcs32eg2/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-rcs32eg2/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 155, in <module>
        File "<string>", line 49, in get_config_posix
        File "<string>", line 28, in find_package_name
      Exception: Can not find valid pkg-config name.
      Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

위와 같이 필자는 다운받은 라이브러리를 requirements.txt로 정리를 하고 이것을 한번에 설치를 하려고 했더니 위와 같은 에러가 떳다. 

이것은 mysqlclient를 사용하기 위해서 필요한 패키지가 존재했고 그것이 없었기에 에러가 뜨는 것이었다.

 

pkg-config는 라이브러리를 컴파일하고 링크할 때 필요한 컴파일러와 링커의 플래그를 관리하는 도구이다.

그래서 필요한 라이브러리인 pkg-config를 다운 받아줬다.

sudo apt-get update
sudo apt-get install pkg-config

 

그리고 에러 메시지에서는 MySQL 또는 MariaDB 관련 라이브러리를 찾을 수 없다고 언급하고 있었다.

mysqlclient를 컴파일하기 위해서는 MySQL 또는 MariaDB의 개발 라이브러리가 필요했기에 이것 또한 다운을 받아 주었다.

sudo apt-get install libmysqlclient-dev

 

그러고 다시 모든 라이브러리를 다운받으려고 했더니 또 에러가 떳다.

진짜 뭐든 쉽게 되는 것이 없는 것 같다.ㅠㅠㅠㅠ 에러메세지를 보자.

더보기

(venv) srilankakim66@instance-20240510-021431:~/final_project$ pip install -r requirements.txt 
Requirement already satisfied: asgiref==3.8.1 in ./venv/lib/python3.11/site-packages (from -r requirements.txt (line 1)) (3.8.1)
Requirement already satisfied: Django==5.0.6 in ./venv/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (5.0.6)
Collecting djangorestframework==3.15.1 (from -r requirements.txt (line 3))
  Using cached djangorestframework-3.15.1-py3-none-any.whl.metadata (11 kB)
Collecting mysqlclient==2.2.4 (from -r requirements.txt (line 4))
  Using cached mysqlclient-2.2.4.tar.gz (90 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Collecting pillow==10.3.0 (from -r requirements.txt (line 5))
  Downloading pillow-10.3.0-cp311-cp311-manylinux_2_28_x86_64.whl.metadata (9.2 kB)
Collecting python-dotenv==1.0.1 (from -r requirements.txt (line 6))
  Downloading python_dotenv-1.0.1-py3-none-any.whl.metadata (23 kB)
Requirement already satisfied: sqlparse==0.5.0 in ./venv/lib/python3.11/site-packages (from -r requirements.txt (line 7)) (0.5.0)
Downloading djangorestframework-3.15.1-py3-none-any.whl (1.1 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 7.1 MB/s eta 0:00:00
Downloading pillow-10.3.0-cp311-cp311-manylinux_2_28_x86_64.whl (4.5 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.5/4.5 MB 50.0 MB/s eta 0:00:00
Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for mysqlclient (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [45 lines of output]
      Trying pkg-config --exists mysqlclient
      # Options for building extension module:
        extra_compile_args: ['-I/usr/include/mysql', '-std=c99']
        extra_link_args: ['-lmysqlclient']
        define_macros: [('version_info', (2, 2, 4, 'final', 0)), ('__version__', '2.2.4')]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-cpython-311
      creating build/lib.linux-x86_64-cpython-311/MySQLdb
      copying src/MySQLdb/cursors.py -> build/lib.linux-x86_64-cpython-311/MySQLdb
      copying src/MySQLdb/release.py -> build/lib.linux-x86_64-cpython-311/MySQLdb
      copying src/MySQLdb/_exceptions.py -> build/lib.linux-x86_64-cpython-311/MySQLdb
      copying src/MySQLdb/__init__.py -> build/lib.linux-x86_64-cpython-311/MySQLdb
      copying src/MySQLdb/times.py -> build/lib.linux-x86_64-cpython-311/MySQLdb
      copying src/MySQLdb/connections.py -> build/lib.linux-x86_64-cpython-311/MySQLdb
      copying src/MySQLdb/converters.py -> build/lib.linux-x86_64-cpython-311/MySQLdb
      creating build/lib.linux-x86_64-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/ER.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/CR.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants
      running egg_info
      writing src/mysqlclient.egg-info/PKG-INFO
      writing dependency_links to src/mysqlclient.egg-info/dependency_links.txt
      writing top-level names to src/mysqlclient.egg-info/top_level.txt
      reading manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
      reading manifest template 'MANIFEST.in'
      adding license file 'LICENSE'
      writing manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
      copying src/MySQLdb/_mysql.c -> build/lib.linux-x86_64-cpython-311/MySQLdb
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.linux-x86_64-cpython-311
      creating build/temp.linux-x86_64-cpython-311/src
      creating build/temp.linux-x86_64-cpython-311/src/MySQLdb
      x86_64-linux-gnu-gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -fPIC "-Dversion_info=(2, 2, 4, 'final', 0)" -D__version__=2.2.4 -I/home/srilankakim66/final_project/venv/include -I/usr/include/python3.11 -c src/MySQLdb/_mysql.c -o build/temp.linux-x86_64-cpython-311/src/MySQLdb/_mysql.o -I/usr/include/mysql -std=c99
      src/MySQLdb/_mysql.c:47:10: fatal error: Python.h: No such file or directory
         47 | #include "Python.h"
            |          ^~~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mysqlclient
Failed to build mysqlclient
ERROR: Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects

요번에는 mysqlclient 라이브러리를 설치하려는 과정에서 발생한 오류로, Python 개발 헤더 파일인 Python.h를 찾을 수 없어서 에러가 발생했다고 나와있었다.

 

이 파일은 Python 개발 패키지에 포함되어 있으며, 해당 패키지가 시스템에 설치되어 있지 않을 때 이런 오류가 발생할 수 있다고 한다.

 

해결 방법은 Python 개발 헤더와 라이브러리를 설치해주면 된다. 사용하고 있는 운영 체제에 따라 설치 방법이 다를 수 있고 필자는 Ubuntu운영 체제를 사용하기에 이것에 대한 해결코드를 작성해 놓겠다.

sudo apt-get update
sudo apt-get install python{파이썬 버전(3.xx)}-dev

 

필자는 위에 블로그에서 작성을 했듯이 python3.11버전을 다운 받았고 아래의 코드를 넣어주었다.

sudo apt-get update
sudo apt-get install python3.11-dev

 

그리고 혹시 몰라서 모든 가상환경 파일을 지워주고 다시 가상환경을 만들어 주었다.

 

그리고 다시 requeirements.txt를 이용하여 모든 라이브러리를 다운받았더니 모두 잘 다운되는 것을 볼 수 있었다!!!

 

진짜 개발은 너무나도 어려운것 같다 이것저것 생각해야 될것도 많고 항상 문제를 겪게 된다. 하지만 위와 같이 겪었던 문제를 회고하고 공부하고 다음번에 이러한 문제를 만났을 때 해결할 수 있다면 나중에 더 좋은 개발자가 될 것 같다!!!