[컴][파이썬] No module named 'MySQLdb' 에러

import mysqldb

No module named 'MySQLdb' 에러

환경

  • 환경 : windows 10 , Windows Subsytem for Linux(WSL)
  • celery : version 4.4.2
  • SQLAlchemy : v1.3.17

설치된 packages

celery==4.4.2
SQLAlchemy==1.3.17
mysql-connector-python-rf==2.2.2

No module named 'MySQLdb'

이 상황에서 celery 의 task 를 실행시켰는데, 아래처럼 error 가 떴다. No module named 'MySQLdb' 가 주요한 이유였다.
[2020-06-01 14:26:50,785: WARNING/ForkPoolWorker-8] /usr/local/lib/python3.8/dist-packages/celery/app/trace.py:568: RuntimeWarning: Exception raised outside body: ModuleNotFoundError("No module named 'MySQLdb'"):
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/celery/backends/database/session.py", line 37, in get_engine
    return self._engines[dburi]
KeyError: 'mysql://id:pass@127.0.0.1/fundedv2'
...

해결

해결방법은 아래에서 찾을 수 있다. 간략하게 이야기를 하면, pymysql 를 설치하고, 이 PyMySQLMySQLdb 인 것처럼 인식하게 하면 된다.
그래서 pymysql 을 설치하고
pip3 install pymysql
 
project source에서 가장 top level file __init__.py에 아래 code 를 넣어주면 된다.
import pymysql
pymysql.install_as_MySQLdb()


[컴] RabbitMQ temporary queue

queue / 임시큐 / 송수신 다른 큐/ queue /

RabbitMQ temporary queue

아래처럼 임시 queue 를 만들 수 있다.
result = channel.queue_declare(queue='', exclusive=True)

한명 접속 후 삭제

exclusive=True 주면, cosumer 의 connection 이 close 되면, queue 가 삭제된다.(참고:Queues — RabbitMQ)

example

"""
                                  to_b_rk     +-----------------+
                                              |                 |
+--------+                          +------>  |     to_b_q      |          +---------+
|        |                          |         |                 +--------> |         |
|        |                          |         +-----------------+          |         |
|        |       +----------+       |                                      |         |
|    A   +-----> | to_b     |       |                                      |   B     |
|        |       |          +-------+                                      |         |
|        |       +----------+                                              |         |
|        |       +----------+                 +------------------+         |         |
+----+---+       | to_a     |                 |                  |         |         |
     ^      +--> |          +---------------->+ <random_name>    +-----+   +---+-----+
     |      |    +----------+                 | temporary queue  |     |       |
     |      |                  <random_name>  +------------------+     |       |
     |      |                                                          |       |
     |      |                                                          |       |
     |      |                                                          |       |
     |      +------------------------------------------------------------------+
     |                                                                 |
     |                                                                 |
     +-----------------------------------------------------------------+


"""

parameters = pika.URLParameters('amqp://guest:guest@127.0.0.1:5672/%2F')
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

to_a = 'to_a'
to_b = 'to_b'
to_b_q = 'to_b_q'
to_b_rk = 'to_b_rk'

# to_b queue
channel.exchange_declare(exchange=to_b,
                         exchange_type='direct')
channel.queue_declare(queue=to_b_q)  # idempotent
channel.queue_bind(exchange=to_b,
                   queue=to_b_q,
                   routing_key=to_b_rk)

# to_a queue, random temporary queue
channel.exchange_declare(exchange='to_a',
                         exchange_type='direct')
result = channel.queue_declare(queue='', exclusive=True)
recv_queue = result.method.queue
channel.queue_bind(exchange='to_a',
                   queue=recv_queue,
                   routing_key=recv_queue)

# send message to to_b queue
data = {}
data['to_a_queue'] = recv_queue
message = json.dumps(data)
channel.basic_publish(exchange=to_b,
                      routing_key=to_b_rk,
                      body=message)


python 에서 간단한 메시지 전송을 하려면 ref. 1 을 참고하자.

See Also

  1. 쿠...sal: [컴] RabbitMQ 자료들

Reference

  1. RabbitMQ tutorial - "Hello world!" — RabbitMQ

[컴][파이썬] formatter Black

pep / formatter / python pretty formatter / yapf

Black

CPython 을 만드는 python 조직에서 만들었다.

installation

pip3 install balck

run

black ~/p/prog.py

The Black code style

black 이 code 를 어떤 스타일로 바꾸는지 알 수 있다. 이 글 에 따르면, 기존의 다른 formatter 와는 다르게 black 은 스타일을 자기가 원하는대로 변경할 수 없다고 한다.

pyproject.toml Black configuration file

기본적으로 설정파일을 사용할 필요는 없다. 다만 --include , --exclude 등을 이용해서 원하지 않은 패턴등을 추가하거나, 뺄 수 있다.
[tool.black]
line-length = 88
--verbose 를 이용하면, 어느 .toml 을 사용하는지 확인할 수 있다.
black --verbose  ~/a/prog/python/gigas/gigas/lib/net.py

pyproject.toml

pyproject.toml 은 python project 의 build system requirements 를 저장하기 위한 configuration file 이다.

pre-commit

git 에 commit 하기 전에 black 을 한번 실행하게 할 수 있다. 자세한 내용은 아래 링크를 참고하자.

[컴][유틸] 수식 그려주는 도구

온라인에서 수학공식 그려주는 사이트 / Latex math equation / 수학공식 화면에 / 수학공식 그림 / 수식 그리기 / 적기 / 수식 그려주는 도구 / 수식 / 수학 식 / 공식 / 수학 기호 그려주는 / 컴퓨터에서 수학식을 적는 방법

수식 그려주는 도구

word 에 저장된 서식을 latex 로 변경하기

아래 그림처럼 MSWord 에서는 windows의 MIP(Math Input Panel) 을 이용해서 수식을 입력해 넣을 수 있다. 이 파일을 저장하고 pandoc 을 이용해서 아래처럼 command 를 실행하면 latex 수식이 만들어진다.

f:\>d:\a\apps\Pandoc\pandoc.exe x.docx -f docx -t latex
\[\sqrt{x = \frac{- b \pm \sqrt{b^{2} - 4\text{ac}}}{2a}a}\]

f:\>d:\a\apps\Pandoc\pandoc.exe x.docx -f docx -t markdown
$$\sqrt{x = \frac{- b \pm \sqrt{b^{2} - 4\text{ac}}}{2a}a}$$
word에 적힌 수식을 latex 로 전환하기

Reference

  1. Pandoc - Pandoc User’s Guide

[컴] Github Workflow

깃허브 / 깃헙 / 워크플로우 / 액션 /유틸 / 툴 / tools / tool / util

Github Workflow

on

event 가 발생할 때마다 workflow 를 동작하게 할 수 있다. 자세한 내용은 다음 링크들을 참고하자.
# push 가 발생하면 workflow 가 동작하게 된다.
on: push
# master 에 대한 event 가 발생했을 때 동작
on:
  # Trigger the workflow on push or pull request,
  # but only for the master branch
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

Reference

  1. Configuring a workflow - GitHub Help

[컴][유틸] Mermaid - markdown 형식으로 diagram 을 그릴 수 있다.

글로 그림 / 차트 그리기 / chart / draw chart / 챠트 / diagram / 다이어그램 / visio 대안

Mermaid

markdown 같은 형식으로 flow chart 를 그릴 수 있다.
vscode 에서 plugin 을 지원한다.

현재 지원하는 Diagrams

  • Flowchart
  • Sequence diagram
  • Class Diagram
  • State Diagram
  • Entity Relationship Diagram
  • User Journey
  • Gantt
  • Pie Chart

See Also

  1. Diagrams · Diagram as Code: python code 로 infra 를 그릴 수 있도록 해준다.