【问题标题】:TypeError when running an API from a GitLab project从 GitLab 项目运行 API 时出现 TypeError
【发布时间】:2022-01-07 00:58:39
【问题描述】:

我可以访问一个包含以下分支的私有 GitLab 项目:maindevelopnew_branch。机器学习项目非常复杂,包含用于 NLP 的各种转换器,以及从输入文本生成一些输出的许多步骤。也使用了 FastAPI。

我正在尝试看看它是如何工作的并运行它。我使用 Windows。我知道我需要 Python 3.9 并安装 pipenv,然后给出以下说明:

Install all dependencies locally by running `pipenv install --dev`. 
Create an empty file named `.env` inside the root project folder. 
Copy and paste the contents of the `.env.example` file into `.env`. 
Run `pipenv run prebuild-win` to download the necessary static files needed by 3rd party libraries. 
Run API using the following command: `pipenv run start`. Go to `http://localhost:8000/`.

我打开 git CMD,克隆它,它是文件夹中出现的 develop 分支(对于 new_branch,它显示为 Pipeline: failed)。我导航到项目的文件夹,创建了一个虚拟环境并安装了依赖项。 GitLab 中名为 .env.example 的文件已经以 .env 的形式出现在我的文件夹中,其中包含必要的内容。我运行 pipenv run prebuild-win 并安装其他所有内容。

但是当我使用pipenv run start 运行 API 时,我得到:

Traceback (most recent call last):
  File "C:\Users\me\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\me\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\me\the-project\app\main.py", line 25, in <module>
    api = Api()
  File "C:\Users\me\the-project\app\main.py", line 13, in __init__
    self.config_service = ConfigService()
  File "C:\Users\me\the-project\app\core\config.py", line 17, in __init__
    self.port = int(os.getenv('PORT'))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

我做错了什么吗?

我检查并有一个端口,我认为这是因为我跳过了创建.env 文件的步骤。我创建了一个空文本文件,复制并粘贴了内容,我得到了同样的错误。 .env.example 包含:

APP_NAME=My app
VERSION=0.0.1
HOST=127.0.0.1
PORT=1234

【问题讨论】:

标签: python windows pipenv


【解决方案1】:
int(os.getenv('PORT'))

此行尝试读取环境变量PORT 并将其转换为整数。

你得到错误的事实

TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是 'NoneType'

表示os.getenv('PORT')返回None,即环境变量不存在。

您需要了解如何设置适当的环境变量。显然将它们写入一个名为 .env 的文件是不够的。

【讨论】:

  • 我最终设法将它们写入.env 文件,但谢谢!我会接受你的回答。现在的问题是,当我转到http://localhost:8000/ 时,我只得到{"name":"My app","version":"0.0.1","currentTime":"01/07/2022, 04:16:47"} 和终端"←[1mGET /favicon.ico HTTP/1.1←[0m" ←[31m404 Not Found←[0m。那是问题吗?我如何实际打开并使用该应用程序?
猜你喜欢
  • 2021-12-20
  • 2013-02-05
  • 1970-01-01
  • 2015-05-06
  • 2018-08-14
  • 1970-01-01
  • 2021-01-27
  • 2017-09-20
  • 2021-06-19
相关资源
最近更新 更多