Search code examples
pythonwindowspipenv

TypeError when running an API from a GitLab project


I got access to a private GitLab project that contains the following branches: main, develop and new_branch. The machine learning project is pretty complex and contains various transformers for NLP, and many steps that generate some output from an input text. FastAPI has also been used.

I am trying to see how this works and run it. I use Windows. I know that I need Python 3.9 and to have pipenv installed and then the following instructions are given:

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/`.

I opened git CMD, cloned it, and it's the develop branch that appears in the folder (for the new_branch it says Pipeline: failed). I navigated to the folder of the project, created a virtual environment and installed the dependencies. The file that in GitLab was called .env.example already appear in my folder as .env with the necessary contents. I run pipenv run prebuild-win and everything else gets installed.

But when I run API using pipenv run start, I get:

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'

Did I do anything wrong?

I checked and there is a port, and I think it's because I skipped the creating an .env file step. I created an empty text file, copy and pasted the contents, and I get the same error. The .env.example contains:

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

Solution

  • int(os.getenv('PORT'))
    

    This line tries to read the environment variable PORT and convert it to an integer.

    The fact that you get the error

    TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

    indicates that os.getenv('PORT') returned None, i.e. that the environment variable did not exist.

    You need to find out how to set the appropriate environment variables. Apparently writing them into a file called .env isn't sufficient.