{"id":90800,"date":"2026-08-06T00:48:31","date_gmt":"2026-08-05T16:48:31","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/90800.html"},"modified":"2026-08-06T00:48:31","modified_gmt":"2026-08-05T16:48:31","slug":"%e3%80%8apython-%e9%a1%b9%e7%9b%ae%e9%85%8d%e7%bd%ae%e7%9a%84%e7%bb%88%e6%9e%81%e6%96%b9%e6%a1%88%ef%bc%9aconfig-py-pyproject-toml-%e6%b7%b1%e5%ba%a6%e8%a7%a3%e8%af%bb%e3%80%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/90800.html","title":{"rendered":"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848\uff1aconfig.py + pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b"},"content":{"rendered":"<h3>\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848&#xff1a;config.py &#043; pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b<\/h3>\n<h4>\u5f15\u8a00<\/h4>\n<p>\u524d\u4e24\u7bc7\u6587\u7ae0\u89e3\u51b3\u4e86\u5bb9\u5668\u90e8\u7f72\u548c\u5f00\u53d1\u81ea\u52a8\u5316\u7684\u95ee\u9898\u3002\u4f46\u5e94\u7528\u5185\u90e8\u662f\u5982\u4f55\u8bfb\u53d6\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u7684&#xff1f;\u4f9d\u8d56\u5e93\u53c8\u662f\u5982\u4f55\u7ba1\u7406\u7684&#xff1f;<\/p>\n<p>\u672c\u6587\u6df1\u5165\u5230 Python \u4ee3\u7801\u5c42\u9762&#xff0c;\u89e3\u6790\u4e24\u4e2a\u6838\u5fc3\u6587\u4ef6&#xff1a;<\/p>\n<ul>\n<li>\n<p>app\/core\/config.py&#xff1a;\u914d\u7f6e\u603b\u63a7\u4e2d\u5fc3&#xff0c;\u667a\u80fd\u52a0\u8f7d\u00a0.env\u00a0\u6587\u4ef6\u5e76\u89e3\u6790\u4e3a Python \u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>pyproject.toml&#xff1a;\u9879\u76ee\u8eab\u4efd\u8bc1\u660e&#xff0c;\u7edf\u4e00\u7ba1\u7406\u4f9d\u8d56\u548c\u4ee3\u7801\u89c4\u8303\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8bfb\u5b8c\u672c\u6587&#xff0c;\u4f60\u5c06\u5f7b\u5e95\u7406\u89e3\u4ece\u201c\u73af\u5883\u53d8\u91cf\u201d\u5230\u201cPython \u4ee3\u7801\u201d\u7684\u5b8c\u6574\u6570\u636e\u6d41\u3002<\/p>\n<h4>\u4e00\u3001config.py&#xff1a;\u914d\u7f6e\u603b\u63a7\u4e2d\u5fc3<\/h4>\n<h5>1.1 \u73af\u5883\u8bc6\u522b\u5668\u00a0get_environment()<\/h5>\n<p>python<\/p>\n<p>class Environment(str, Enum):<br \/>\n    DEVELOPMENT &#061; &#034;development&#034;<br \/>\n    STAGING &#061; &#034;staging&#034;<br \/>\n    PRODUCTION &#061; &#034;production&#034;<br \/>\n    TEST &#061; &#034;test&#034;<\/p>\n<p>def get_environment() -&gt; Environment:<br \/>\n    match os.getenv(&#034;APP_ENV&#034;, &#034;development&#034;).lower():<br \/>\n        case &#034;production&#034; | &#034;prod&#034;:<br \/>\n            return Environment.PRODUCTION<br \/>\n        case &#034;staging&#034; | &#034;stage&#034;:<br \/>\n            return Environment.STAGING<br \/>\n        case &#034;test&#034;:<br \/>\n            return Environment.TEST<br \/>\n        case _:<br \/>\n            return Environment.DEVELOPMENT<\/p>\n<ul>\n<li>\n<p>\u8bfb\u53d6\u00a0APP_ENV\u00a0\u73af\u5883\u53d8\u91cf&#xff0c;\u652f\u6301\u6a21\u7cca\u5339\u914d&#xff08;prod\u00a0\u548c\u00a0production\u00a0\u90fd\u80fd\u8bc6\u522b&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u6807\u51c6\u5316\u7684\u679a\u4e3e\u7c7b\u578b&#xff0c;\u4f9b\u540e\u7eed\u903b\u8f91\u5224\u65ad\u4f7f\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>1.2 \u667a\u80fd\u6587\u4ef6\u52a0\u8f7d\u5668\u00a0load_env_file()<\/h5>\n<p>python<\/p>\n<p>def load_env_file():<br \/>\n    env &#061; get_environment()<br \/>\n    env_files &#061; [<br \/>\n        os.path.join(base_dir, f&#034;.env.{env.value}.local&#034;),  # \u6700\u9ad8\u4f18\u5148\u7ea7<br \/>\n        os.path.join(base_dir, f&#034;.env.{env.value}&#034;),        # \u73af\u5883\u4e13\u5c5e<br \/>\n        os.path.join(base_dir, &#034;.env.local&#034;),               # \u901a\u7528\u672c\u5730\u8986\u76d6<br \/>\n        os.path.join(base_dir, &#034;.env&#034;),                     # \u515c\u5e95<br \/>\n    ]<br \/>\n    for env_file in env_files:<br \/>\n        if os.path.isfile(env_file):<br \/>\n            load_dotenv(dotenv_path&#061;env_file)<br \/>\n            return env_file<br \/>\n    return None<\/p>\n<p>ENV_FILE &#061; load_env_file()  # \u6a21\u5757\u52a0\u8f7d\u65f6\u7acb\u5373\u6267\u884c<\/p>\n<p>\u4f18\u5148\u7ea7\u94fe&#xff1a;<br \/>\n.env.development.local\u00a0&gt;\u00a0.env.development\u00a0&gt;\u00a0.env.local\u00a0&gt;\u00a0.env<\/p>\n<ul>\n<li>\n<p>.local\u00a0\u6587\u4ef6\u901a\u5e38\u88ab\u00a0.gitignore\u00a0\u5ffd\u7565&#xff0c;\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u672c\u5730\u8986\u76d6\u56e2\u961f\u914d\u7f6e&#xff0c;\u800c\u4e0d\u5f71\u54cd\u4ed6\u4eba\u3002<\/p>\n<\/li>\n<li>\n<p>\u8be5\u51fd\u6570\u5728\u6a21\u5757\u5bfc\u5165\u65f6\u7acb\u5373\u6267\u884c&#xff0c;\u786e\u4fdd\u00a0Settings\u00a0\u7c7b\u5b9e\u4f8b\u5316\u524d\u73af\u5883\u53d8\u91cf\u5df2\u88ab\u6ce8\u5165\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>1.3 \u8f85\u52a9\u89e3\u6790\u5668\u00a0parse_list_from_env()<\/h5>\n<p>python<\/p>\n<p>def parse_list_from_env(env_key, default&#061;None):<br \/>\n    value &#061; os.getenv(env_key)<br \/>\n    if not value:<br \/>\n        return default or []<br \/>\n    value &#061; value.strip(&#034;\\\\&#034;&#039;&#034;)<br \/>\n    if &#034;,&#034; not in value:<br \/>\n        return [value]<br \/>\n    return [item.strip() for item in value.split(&#034;,&#034;) if item.strip()]<\/p>\n<p>\u4f5c\u7528&#xff1a;\u5c06\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u00a0&#034;http:\/\/a.com,http:\/\/b.com&#034;\u00a0\u89e3\u6790\u4e3a Python \u5217\u8868\u00a0[&#039;http:\/\/a.com&#039;, &#039;http:\/\/b.com&#039;]\u3002<\/p>\n<p>\u4f7f\u7528\u573a\u666f&#xff1a;CORS \u8de8\u57df\u767d\u540d\u5355\u00a0ALLOWED_ORIGINS\u3002<\/p>\n<h5>1.4 Settings \u7c7b \u2014\u2014 \u914d\u7f6e\u6620\u5c04\u4e2d\u5fc3<\/h5>\n<p>python<\/p>\n<p>class Settings:<br \/>\n    def __init__(self):<br \/>\n        self.ENVIRONMENT &#061; get_environment()<\/p>\n<p>        # \u5e94\u7528\u914d\u7f6e<br \/>\n        self.PROJECT_NAME &#061; os.getenv(&#034;PROJECT_NAME&#034;, &#034;FastAPI LangGraph Template&#034;)<br \/>\n        self.DEBUG &#061; os.getenv(&#034;DEBUG&#034;, &#034;false&#034;).lower() in (&#034;true&#034;, &#034;1&#034;, &#034;t&#034;, &#034;yes&#034;)<\/p>\n<p>        # \u6570\u636e\u5e93\u914d\u7f6e<br \/>\n        self.POSTGRES_HOST &#061; os.getenv(&#034;POSTGRES_HOST&#034;, &#034;localhost&#034;)<br \/>\n        self.POSTGRES_PORT &#061; int(os.getenv(&#034;POSTGRES_PORT&#034;, &#034;5432&#034;))<br \/>\n        self.POSTGRES_DB &#061; os.getenv(&#034;POSTGRES_DB&#034;, &#034;food_order_db&#034;)<\/p>\n<p>        # \u5e94\u7528\u73af\u5883\u4e13\u5c5e\u8986\u76d6<br \/>\n        self.apply_environment_settings()<\/p>\n<ul>\n<li>\n<p>\u7c7b\u578b\u8f6c\u6362&#xff1a;bool\u3001int\u3001float\u3001Path\u00a0\u81ea\u52a8\u8f6c\u6362\u3002<\/p>\n<\/li>\n<li>\n<p>\u9ed8\u8ba4\u503c\u515c\u5e95&#xff1a;\u5982\u679c\u73af\u5883\u53d8\u91cf\u672a\u8bbe\u7f6e&#xff0c;\u4f7f\u7528\u4ee3\u7801\u4e2d\u7684\u786c\u7f16\u7801\u9ed8\u8ba4\u503c&#xff08;\u5982\u6570\u636e\u5e93\u9ed8\u8ba4\u00a0localhost&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>apply_environment_settings()&#xff1a;\u6839\u636e\u00a0ENVIRONMENT\u00a0\u81ea\u52a8\u8bbe\u7f6e\u00a0DEBUG\u3001LOG_LEVEL\u00a0\u7b49\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>1.5 \u73af\u5883\u4e13\u5c5e\u8986\u76d6\u7684\u9632\u8986\u76d6\u8bbe\u8ba1<\/h5>\n<p>python<\/p>\n<p>def apply_environment_settings(self):<br \/>\n    env_settings &#061; {<br \/>\n        Environment.DEVELOPMENT: {&#034;DEBUG&#034;: True, &#034;LOG_LEVEL&#034;: &#034;DEBUG&#034;},<br \/>\n        Environment.PRODUCTION: {&#034;DEBUG&#034;: False, &#034;LOG_LEVEL&#034;: &#034;WARNING&#034;},<br \/>\n    }<br \/>\n    for key, value in env_settings.get(self.ENVIRONMENT, {}).items():<br \/>\n        env_var_name &#061; key.upper()<br \/>\n        if env_var_name not in os.environ:  # \u5173\u952e\u5224\u65ad<br \/>\n            setattr(self, key, value)<\/p>\n<p>\u6838\u5fc3\u54f2\u5b66&#xff1a;\u53ea\u6709\u5f53\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u6ca1\u6709\u663e\u5f0f\u8bbe\u7f6e\u8be5\u503c\u65f6&#xff0c;\u624d\u4f1a\u8986\u76d6\u3002\u8fd9\u610f\u5473\u7740\u00a0Docker \u4f20\u5165\u7684\u53d8\u91cf\u6216 Shell \u624b\u52a8\u5bfc\u51fa\u7684\u53d8\u91cf\u62e5\u6709\u6700\u9ad8\u4f18\u5148\u7ea7\u3002<\/p>\n<hr \/>\n<h4>\u4e8c\u3001config.py \u4e0e\u6240\u6709\u914d\u7f6e\u6587\u4ef6\u7684\u5173\u7cfb<\/h4>\n<table>\n<tr>\u6570\u636e\u6765\u6e90\u5982\u4f55\u8fdb\u5165 config.py\u4f18\u5148\u7ea7<\/tr>\n<tbody>\n<tr>\n<td>\u7cfb\u7edf\u73af\u5883\u53d8\u91cf&#xff08;export POSTGRES_HOST&#061;1.2.3.4&#xff09;<\/td>\n<td>os.getenv()\u00a0\u76f4\u63a5\u8bfb\u53d6<\/td>\n<td>\u6700\u9ad8<\/td>\n<\/tr>\n<tr>\n<td>Docker Compose\u00a0env_file\u00a0\u6ce8\u5165<\/td>\n<td>\u5bb9\u5668\u542f\u52a8\u65f6\u6ce8\u5165\u00a0os.environ<\/td>\n<td>\u9ad8<\/td>\n<\/tr>\n<tr>\n<td>set_env.sh\u00a0source \u52a0\u8f7d\u7684\u00a0.env<\/td>\n<td>Shell \u5bfc\u51fa\u540e&#xff0c;Python \u7ee7\u627f<\/td>\n<td>\u4e2d<\/td>\n<\/tr>\n<tr>\n<td>load_env_file()\u00a0\u52a0\u8f7d\u7684\u00a0.env\u00a0\u6587\u4ef6<\/td>\n<td>load_dotenv()\u00a0\u5199\u5165\u00a0os.environ<\/td>\n<td>\u4f4e<\/td>\n<\/tr>\n<tr>\n<td>\u4ee3\u7801\u4e2d\u7684\u786c\u7f16\u7801\u9ed8\u8ba4\u503c<\/td>\n<td>os.getenv(&#034;KEY&#034;, &#034;default&#034;)<\/td>\n<td>\u6700\u4f4e&#xff08;\u515c\u5e95&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u6218\u63a8\u6f14&#xff1a;\u672c\u5730\u6267\u884c\u00a0make dev&#xff0c;POSTGRES_HOST\u00a0\u7684\u503c\u6765\u81ea\u00a0.env.development\u00a0\u4e2d\u7684\u00a0db&#xff1b;\u82e5\u60f3\u4e34\u65f6\u8fde\u63a5\u5176\u4ed6\u6570\u636e\u5e93&#xff0c;\u53ef\u76f4\u63a5\u00a0POSTGRES_HOST&#061;192.168.1.100 make dev&#xff0c;\u6b64\u65f6\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4f18\u5148\u7ea7\u6700\u9ad8&#xff0c;\u8986\u76d6\u6587\u4ef6\u914d\u7f6e\u3002<\/p>\n<hr \/>\n<h4>\u4e09\u3001pyproject.toml&#xff1a;\u9879\u76ee\u8eab\u4efd\u4e0e\u4f9d\u8d56\u7ba1\u7406<\/h4>\n<h5>3.1 \u9879\u76ee\u5143\u6570\u636e\u4e0e\u6838\u5fc3\u4f9d\u8d56\u00a0[project]<\/h5>\n<p>toml<\/p>\n<p>[project]<br \/>\nname &#061; &#034;langgraph-fastapi-template&#034;<br \/>\nversion &#061; &#034;0.1.0&#034;<br \/>\nrequires-python &#061; &#034;&gt;&#061;3.13&#034;<br \/>\ndependencies &#061; [<br \/>\n    &#034;fastapi&gt;&#061;0.121.0&#034;,<br \/>\n    &#034;langchain&gt;&#061;1.0.5&#034;,<br \/>\n    &#034;langgraph&gt;&#061;1.0.2&#034;,<br \/>\n    &#034;psycopg[binary]&gt;&#061;3.3.2&#034;,<br \/>\n    &#034;python-dotenv&gt;&#061;1.1.0&#034;,<br \/>\n    &#034;uvicorn&gt;&#061;0.34.0&#034;,<br \/>\n    # &#8230; \u5171 30&#043; \u4e2a\u5e93<br \/>\n]<\/p>\n<ul>\n<li>\n<p>\u8eab\u4efd\u4fe1\u606f&#xff1a;\u9879\u76ee\u540d\u79f0\u3001\u7248\u672c\u3001\u6700\u4f4e Python \u7248\u672c\u3002<\/p>\n<\/li>\n<li>\n<p>\u6838\u5fc3\u4f9d\u8d56&#xff1a;\u6240\u6709\u8fd0\u884c\u65f6\u5fc5\u9700\u7684\u5e93\u3002config.py\u00a0\u4e2d\u7684\u00a0from dotenv import load_dotenv\u00a0\u5c31\u6765\u6e90\u4e8e\u6b64\u5904\u7684\u00a0python-dotenv\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>3.2 \u53ef\u9009\u4f9d\u8d56\u4e0e\u5206\u7ec4<\/h5>\n<p>toml<\/p>\n<p>[project.optional-dependencies]<br \/>\ndev &#061; [&#034;black&#034;, &#034;isort&#034;, &#034;flake8&#034;, &#034;ruff&#034;]<br \/>\ncache &#061; [&#034;redis&gt;&#061;7.4.0&#034;, &#034;valkey[libvalkey]&gt;&#061;6.1.0&#034;]<\/p>\n<p>[dependency-groups]<br \/>\ndev &#061; [&#034;detect-secrets&#034;, &#034;pre-commit&#034;, &#034;pyright&#034;]<br \/>\ntest &#061; [&#034;httpx&#034;, &#034;pytest&#034;]<\/p>\n<ul>\n<li>\n<p>\u5f00\u53d1\u4f9d\u8d56&#xff08;ruff\u3001pyright&#xff09;\u4e0e\u751f\u4ea7\u4f9d\u8d56\u5206\u79bb\u3002<\/p>\n<\/li>\n<li>\n<p>cache\u00a0\u7ec4\u5305\u542b\u00a0valkey&#xff0c;\u5bf9\u5e94\u00a0config.py\u00a0\u4e2d\u7684\u00a0VALKEY_HOST\u00a0\u914d\u7f6e&#xff0c;\u7f13\u5b58\u6a21\u5757\u53ef\u63d2\u62d4\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>3.3 \u5de5\u5177\u7edf\u4e00\u914d\u7f6e\u00a0[tool.*]<\/h5>\n<p>toml<\/p>\n<p>[tool.ruff]<br \/>\nline-length &#061; 119<br \/>\nexclude &#061; [&#034;migrations&#034;, &#034;venv&#034;]<\/p>\n<p>[tool.ruff.lint]<br \/>\nselect &#061; [&#034;E&#034;, &#034;F&#034;, &#034;B&#034;, &#034;ERA&#034;, &#034;D&#034;]<br \/>\nignore &#061; [&#034;E501&#034;, &#034;D203&#034;]<\/p>\n<p>[tool.pyright]<br \/>\ntypeCheckingMode &#061; &#034;standard&#034;<br \/>\nreportDuplicateImport &#061; &#034;error&#034;<\/p>\n<ul>\n<li>\n<p>Ruff&#xff1a;\u884c\u957f\u5ea6 119&#xff0c;\u542f\u7528 Google \u98ce\u683c Docstring \u68c0\u67e5\u3002<\/p>\n<\/li>\n<li>\n<p>Pyright&#xff1a;\u6807\u51c6\u4e25\u683c\u6a21\u5f0f&#xff0c;reportDuplicateImport\u00a0\u89c6\u4e3a\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p>\u7edf\u4e00\u914d\u7f6e&#xff1a;\u6240\u6709\u4ee3\u7801\u68c0\u67e5\u5de5\u5177&#xff08;Ruff\u3001Black\u3001Pyright\u3001Pytest&#xff09;\u7684\u914d\u7f6e\u90fd\u96c6\u4e2d\u5728\u6b64&#xff0c;\u6839\u76ee\u5f55\u4e0d\u518d\u6709\u00a0.flake8\u3001.isort.cfg\u00a0\u7b49\u96f6\u6563\u6587\u4ef6\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>3.4 \u4e0e Dockerfile \u7684\u5173\u7cfb<\/h5>\n<p>dockerfile<\/p>\n<p>COPY pyproject.toml uv.lock .\/<br \/>\nRUN uv sync &#8211;frozen &#8211;no-install-project<\/p>\n<p>Dockerfile \u76f4\u63a5\u590d\u5236\u00a0pyproject.toml&#xff0c;uv sync\u00a0\u8bfb\u53d6\u00a0dependencies\u00a0\u5217\u8868\u5b89\u88c5\u4f9d\u8d56\u3002\u5982\u679c\u00a0pyproject.toml\u00a0\u7f3a\u5c11\u00a0psycopg&#xff0c;\u5bb9\u5668\u4e2d\u7684\u7a0b\u5e8f\u5c31\u8fde\u63a5\u4e0d\u4e0a\u6570\u636e\u5e93\u3002<\/p>\n<h5>3.5 \u4e0e Makefile \u7684\u5173\u7cfb<\/h5>\n<p>makefile<\/p>\n<p>lint: uv run ruff check .<br \/>\ntypecheck: uv run pyright<\/p>\n<p>Makefile \u8c03\u7528\u00a0uv run\u00a0\u6267\u884c\u5de5\u5177&#xff0c;\u800c\u5177\u4f53\u68c0\u67e5\u89c4\u5219&#xff08;\u5982\u884c\u957f\u5ea6 119\u3001\u5ffd\u7565\u54ea\u4e9b\u9519\u8bef&#xff09;\u5168\u90e8\u6765\u6e90\u4e8e\u00a0pyproject.toml\u00a0\u4e2d\u7684\u00a0[tool.ruff]\u00a0\u914d\u7f6e\u3002<\/p>\n<hr \/>\n<h4>\u56db\u3001\u5b8c\u6574\u914d\u7f6e\u6d41\u8f6c\u95ed\u73af<\/h4>\n<p>text<\/p>\n<p>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502  1. \u5f00\u53d1\u8005\u514b\u9686\u9879\u76ee                                              \u2502<br \/>\n\u2502     \u2193                                                         \u2502<br \/>\n\u2502  2. \u6267\u884c source set_env.sh development                         \u2502<br \/>\n\u2502     \u2192 \u81ea\u52a8\u4ece .env.example \u590d\u5236\u4e3a .env.development              \u2502<br \/>\n\u2502     \u2192 \u586b\u5165\u771f\u5b9e OPENAI_API_KEY\u3001POSTGRES_PASSWORD               \u2502<br \/>\n\u2502     \u2193                                                         \u2502<br \/>\n\u2502  3. \u6267\u884c make dev                                              \u2502<br \/>\n\u2502     \u2192 Makefile \u8c03\u7528 run_with_env&#xff0c;source set_env.sh           \u2502<br \/>\n\u2502     \u2192 set_env.sh \u5bfc\u51fa\u6240\u6709\u53d8\u91cf\u5230 Shell                          \u2502<br \/>\n\u2502     \u2192 \u542f\u52a8 uvicorn&#xff0c;Python \u8fdb\u7a0b\u7ee7\u627f\u73af\u5883\u53d8\u91cf                     \u2502<br \/>\n\u2502     \u2193                                                         \u2502<br \/>\n\u2502  4. config.py \u6a21\u5757\u52a0\u8f7d                                         \u2502<br \/>\n\u2502     \u2192 load_env_file() \u4f18\u5148\u52a0\u8f7d .env.development.local         \u2502<br \/>\n\u2502     \u2192 \u82e5\u4e0d\u5b58\u5728&#xff0c;\u52a0\u8f7d .env.development                          \u2502<br \/>\n\u2502     \u2192 Settings \u7c7b\u8bfb\u53d6 os.getenv()&#xff0c;\u7c7b\u578b\u8f6c\u6362                    \u2502<br \/>\n\u2502     \u2192 apply_environment_settings() \u6839\u636e\u73af\u5883\u8865\u5168\u9ed8\u8ba4\u503c          \u2502<br \/>\n\u2502     \u2193                                                         \u2502<br \/>\n\u2502  5. \u4e1a\u52a1\u4ee3\u7801\u4f7f\u7528 settings.POSTGRES_HOST                        \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<hr \/>\n<h4>\u4e94\u3001\u603b\u7ed3<\/h4>\n<ul>\n<li>\n<p>config.py\u00a0\u662f\u5e94\u7528\u5c42\u7684\u201c\u7ec8\u70b9\u7ad9\u201d&#xff0c;\u65e0\u8bba\u914d\u7f6e\u6765\u81ea\u00a0.env\u00a0\u6587\u4ef6\u3001Docker \u6ce8\u5165\u8fd8\u662f Shell \u5bfc\u51fa&#xff0c;\u90fd\u5728\u8fd9\u91cc\u88ab\u7edf\u4e00\u4e3a Python \u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>pyproject.toml\u00a0\u662f\u9879\u76ee\u7684\u201c\u7269\u6599\u6e05\u5355\u201d&#xff0c;\u5b9a\u4e49\u4e86\u6240\u6709\u4f9d\u8d56\u5e93\u548c\u4ee3\u7801\u89c4\u8303&#xff0c;\u88ab\u00a0Dockerfile\u00a0\u548c\u00a0Makefile\u00a0\u5171\u540c\u5f15\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u6574\u4e2a\u914d\u7f6e\u4f53\u7cfb\u9075\u5faa\u00a012-Factor App\u00a0\u539f\u5219&#xff1a;\u914d\u7f6e\u4e0e\u4ee3\u7801\u4e25\u683c\u5206\u79bb&#xff0c;\u6539\u53d8\u914d\u7f6e\u65e0\u9700\u91cd\u65b0\u6784\u5efa\u955c\u50cf\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u81f3\u6b64&#xff0c;\u6211\u4eec\u4ece\u5bb9\u5668\u90e8\u7f72&#xff08;\u7b2c\u4e00\u7bc7&#xff09;\u2192\u00a0\u5f00\u53d1\u81ea\u52a8\u5316&#xff08;\u7b2c\u4e8c\u7bc7&#xff09;\u2192\u00a0\u5e94\u7528\u914d\u7f6e&#xff08;\u7b2c\u4e09\u7bc7&#xff09;&#xff0c;\u5b8c\u6574\u8986\u76d6\u4e86\u4e00\u4e2a\u751f\u4ea7\u7ea7 FastAPI &#043; LangGraph \u9879\u76ee\u7684\u6240\u6709\u914d\u7f6e\u7ef4\u5ea6\u3002\u5e0c\u671b\u8fd9\u4e2a\u7cfb\u5217\u80fd\u5e2e\u52a9\u4f60\u642d\u5efa\u51fa\u4f18\u96c5\u3001\u9ad8\u6548\u3001\u53ef\u7ef4\u62a4\u7684\u540e\u7aef\u9879\u76ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848&#xff1a;config.py  pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b\u5f15\u8a00\u524d\u4e24\u7bc7\u6587\u7ae0\u89e3\u51b3\u4e86\u5bb9\u5668\u90e8\u7f72\u548c\u5f00\u53d1\u81ea\u52a8\u5316\u7684\u95ee\u9898\u3002\u4f46\u5e94\u7528\u5185\u90e8\u662f\u5982\u4f55\u8bfb\u53d6\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u7684&#xff1f;\u4f9d\u8d56\u5e93\u53c8\u662f\u5982\u4f55\u7ba1\u7406\u7684&#xff1f;\u672c\u6587\u6df1\u5165\u5230 Python \u4ee3\u7801\u5c42\u9762&#xff0c;\u89e3\u6790\u4e24\u4e2a\u6838\u5fc3\u6587\u4ef6&#xff1a;app\/core\/config.py&#xff1a;\u914d\u7f6e\u603b\u63a7\u4e2d\u5fc3&#xff0c;\u667a\u80fd\u52a0\u8f7d\u00a0.env\u00a0\u6587\u4ef6\u5e76\u89e3\u6790\u4e3a Python \u5bf9\u8c61\u3002pyproject<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,87,100],"topic":[],"class_list":["post-90800","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-87","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848\uff1aconfig.py + pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/90800.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848\uff1aconfig.py + pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848&#xff1a;config.py pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b\u5f15\u8a00\u524d\u4e24\u7bc7\u6587\u7ae0\u89e3\u51b3\u4e86\u5bb9\u5668\u90e8\u7f72\u548c\u5f00\u53d1\u81ea\u52a8\u5316\u7684\u95ee\u9898\u3002\u4f46\u5e94\u7528\u5185\u90e8\u662f\u5982\u4f55\u8bfb\u53d6\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u7684&#xff1f;\u4f9d\u8d56\u5e93\u53c8\u662f\u5982\u4f55\u7ba1\u7406\u7684&#xff1f;\u672c\u6587\u6df1\u5165\u5230 Python \u4ee3\u7801\u5c42\u9762&#xff0c;\u89e3\u6790\u4e24\u4e2a\u6838\u5fc3\u6587\u4ef6&#xff1a;app\/core\/config.py&#xff1a;\u914d\u7f6e\u603b\u63a7\u4e2d\u5fc3&#xff0c;\u667a\u80fd\u52a0\u8f7d\u00a0.env\u00a0\u6587\u4ef6\u5e76\u89e3\u6790\u4e3a Python \u5bf9\u8c61\u3002pyproject\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/90800.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-05T16:48:31+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/90800.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/90800.html\",\"name\":\"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848\uff1aconfig.py + pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-05T16:48:31+00:00\",\"dateModified\":\"2026-08-05T16:48:31+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/90800.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/90800.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/90800.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848\uff1aconfig.py + pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848\uff1aconfig.py + pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/90800.html","og_locale":"zh_CN","og_type":"article","og_title":"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848\uff1aconfig.py + pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848&#xff1a;config.py pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b\u5f15\u8a00\u524d\u4e24\u7bc7\u6587\u7ae0\u89e3\u51b3\u4e86\u5bb9\u5668\u90e8\u7f72\u548c\u5f00\u53d1\u81ea\u52a8\u5316\u7684\u95ee\u9898\u3002\u4f46\u5e94\u7528\u5185\u90e8\u662f\u5982\u4f55\u8bfb\u53d6\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u7684&#xff1f;\u4f9d\u8d56\u5e93\u53c8\u662f\u5982\u4f55\u7ba1\u7406\u7684&#xff1f;\u672c\u6587\u6df1\u5165\u5230 Python \u4ee3\u7801\u5c42\u9762&#xff0c;\u89e3\u6790\u4e24\u4e2a\u6838\u5fc3\u6587\u4ef6&#xff1a;app\/core\/config.py&#xff1a;\u914d\u7f6e\u603b\u63a7\u4e2d\u5fc3&#xff0c;\u667a\u80fd\u52a0\u8f7d\u00a0.env\u00a0\u6587\u4ef6\u5e76\u89e3\u6790\u4e3a Python \u5bf9\u8c61\u3002pyproject","og_url":"https:\/\/www.wsisp.com\/helps\/90800.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-05T16:48:31+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/90800.html","url":"https:\/\/www.wsisp.com\/helps\/90800.html","name":"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848\uff1aconfig.py + pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-05T16:48:31+00:00","dateModified":"2026-08-05T16:48:31+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/90800.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/90800.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/90800.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u300aPython \u9879\u76ee\u914d\u7f6e\u7684\u7ec8\u6781\u65b9\u6848\uff1aconfig.py + pyproject.toml \u6df1\u5ea6\u89e3\u8bfb\u300b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/90800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=90800"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/90800\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=90800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=90800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=90800"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=90800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}