ModuleNotFoundError: 找不到模块名 ‘server’,请使用云SQL运行

huangapple go评论92阅读模式
英文:

ModuleNotFoundError: No module named 'server', Run with Cloud SQL

问题

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

URL_DATABASE = 'mysql+pymysql://root:12345@34.101.234.148:3306/test'

engine = create_engine(URL_DATABASE)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

我可以在本地运行它,但当我在GCP部署时出现许多错误。

我多次更改了URL_DATABASE,然后添加了0.0.0.0/0,解决了SQLAlchemy的问题,但出现了类似这样的错误:"ModuleNotFoundError: No module named 'server'",我只需要在GCP上运行和部署它。

英文:
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

URL_DATABASE = 'mysql+pymysql://root:12345@34.101.234.148:3306/test'

engine = create_engine(URL_DATABASE)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

i can run it in local but when i deploy it in GCP many error

I change the URL_DATABASE so many times, then i add 0.0.0.0/0 its done with sqlalchemy problem but make some thing like that "ModuleNotFoundError: No module named 'server'" i just need to run and deploy it on GCP

答案1

得分: 1

你的错误很可能是因为在启动Cloud Run服务时,在你的requirements.txt文件中缺少了一个依赖项,与上面发布的实际数据库连接代码无关。

附带说明:我建议使用Cloud SQL Python Connector来连接FastAPI应用程序中的实例,而不是允许0.0.0.0/0的白名单,因为这会使实例可被任何IP地址访问(不安全)。这是一个连接到Cloud SQL并部署到Cloud Run的FastAPI应用程序的示例

英文:

Your error is most likely due to a missing dependency in your requirements.txt file for starting your Cloud Run service and not related to the actual database connection code posted above.

Sidenote: I'd recommend using the Cloud SQL Python Connector for connecting to your instance from a FastAPI application instead of allow-listing 0.0.0.0/0 as that allows your instance to be reachable by any IP address (not secure). Here is an example of a FastAPI application that connects to Cloud SQL and is deployed to Cloud Run.

答案2

得分: 0

看起来对我来说是一个Python错误。我不认为它与Cloud SQL本身有关。您确定您的计算机上所有模块都正确安装了吗?我不确定您的哪个模块被称为“server”。由于我们只有代码的一小部分,并且没有关于代码部署过程的信息,因此很难说清楚是哪个模块。

英文:

Looks like a python error for me. Don't think it is related to the Cloud SQL itself. Are you sure all the modules are installed on your machine correctly? I am not sure which one of your modules is called 'server'. It is hard to say which one since we have only small piece of the code and have no information about the code deployment process.

huangapple
  • 本文由 发表于 2023年7月31日 21:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定