简体   繁体   English

如何通过命令行将 env 文件传递给 FastAPI 应用程序

[英]How to pass env file to FastAPI app via command line

I have the following file that reads in an .env file:我有以下在.env文件中读取的文件:

from pydantic import BaseSettings, HttpUrl


class Settings(BaseSettings):
    url: HttpUrl

    class Config:
        env_file = "config.env"

settings = Settings()

What do I need to do to be able to pass config.env on start?我需要做什么才能在启动时传递config.env

So python -m uvicorn main:app --reload --env config.env所以python -m uvicorn main:app --reload --env config.env

Is there any help FastApi or Uvicorn provide for this? FastApi 或 Uvicorn 对此有什么帮助吗?

You can pass all the envs from config.env using python-dotenv .您可以使用python-dotenv从 config.env 传递所有环境。 To pass a custom path (from): https://stackoverflow.com/a/50356056/14882170传递自定义路径(来自): https://stackoverflow.com/a/50356056/14882170

import os
from dotenv import load_dotenv

# Get the path to the directory this file is in
BASEDIR = os.path.abspath(os.path.dirname(__file__))

# Connect the path with your '.env' file name
load_dotenv(os.path.join(BASEDIR, 'config.env'))

test_var = os.getenv("TEST_VAR")

print(test_var)

Because this is your custom code block, so uvicorn or starlette don't support this.因为这是你的自定义代码块,所以 uvicorn 或 starlette 不支持这个。 I think you should custom your command line by Typer .我认为您应该通过Typer自定义命令行。 Then you can pass env file by command line.然后你可以通过命令行传递 env 文件。

From the docs posted here , you need call the variables from the.env class in the Settings class.此处发布的文档中,您需要在设置 class 中调用来自 .env class 的变量。

config.py配置文件

from pydantic import BaseSettings

class Settings(BaseSettings):
foo: str

class Config:
    env_file = "config.env"

where config.env has the following:其中 config.env 具有以下内容:

foo="bar"

Then you pass the contents to the get_settings function wrapped by a lru_cache decorator and return those values.然后将内容传递给由 lru_cache 装饰器包装的get_settings function并返回这些值。

@lru_cache()
def get_settings():
  return config.Settings()

After that you can inject those parameters into the "/info" path of the fastapi app:之后,您可以将这些参数注入到 fastapi 应用程序的“/info”路径中:

@app.get("/")
def read_root():
  return {"Hello": "World"}


@app.get("/info")
async def info(settings: config.Settings = Depends(get_settings)):
  return {"foo": settings.foo}

The full main.py should be like this:完整的 main.py 应该是这样的:

from functools import lru_cache
  from fastapi import Depends, FastAPI
  import config

  app = FastAPI()


  @lru_cache()
  def get_settings():
    return config.Settings()


  @app.get("/")
  def read_root():
    return {"Hello": "World"}

  @app.get("/info")
  async def info(settings: config.Settings = Depends(get_settings)):
    return {"foo": settings.foo}

Then just start your app without calling the env file in the command:然后只需启动您的应用程序,而无需在命令中调用 env 文件:

python -m uvicorn main:app --reload 

The curl output should be something like this: curl output 应该是这样的:

curl http://127.0.0.1:8000/info
{"foo":"bar"}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM