'ValueError: invalid literal for int with base 10: ' '

I am getting following error when I am trying to start the Django server.

> python manage.py runserver 0.0.0.0:8000
> 
> Traceback (most recent call last):   File "manage.py", line 10, in
> <module>
>     execute_from_command_line(sys.argv)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 363, in execute_from_command_line
>     utility.execute()   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 307, in execute
>     settings.INSTALLED_APPS   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/conf/__init__.py",
> line 56, in __getattr__
>     self._setup(name)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/conf/__init__.py",
> line 41, in _setup
>     self._wrapped = Settings(settings_module)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/conf/__init__.py",
> line 110, in __init__
>     mod = importlib.import_module(self.SETTINGS_MODULE)   File "/opt/python/python-2.7/lib64/python2.7/importlib/__init__.py", line
> 37, in import_module
>     __import__(name)   File "/u/agrawalo/beatthestreet/beatthestreet/config/settings.py", line 96,
> in <module>
>     'PORT': config('DB_PORT', cast=int),   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/decouple.py",
> line 197, in __call__
>     return self.config(*args, **kwargs)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/decouple.py",
> line 85, in __call__
>     return self.get(*args, **kwargs)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/decouple.py",
> line 79, in get
>     return cast(value) ValueError: invalid literal for int() with base 10: ''

Content in .env

SECRET_KEY=BeatTheStreet

DEBUG=False

ALLOWED_HOSTS=['*']

EVENT_STARTED=True
EVENT_ENDED=

# Production database details
DB_NAME=
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=


Solution 1:[1]

You can reproduce the error like this:

# python
>> int('')  # forcing an empty string to integer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

Basically in django its getting port value as empty string. So either you can check the value of port is being read correctly or provide a default value in case PORT is absent(or you can remove that from .env file):

import environ

import environ
env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)
environ.Env.read_env()
PORT = env.int('DB_PORT', default=5432)

Solution 2:[2]

You need to set values for your DB config. At the moment DB_PORT is set empty/null, hence the failure to convert it to in int.

Solution 3:[3]

I had the same thing, the DB_PORT was not empty, but still, the error was thrown. The reason: I tried to run a command with wrong parametrisation of the environment variables.

I had loaded my env_file.env with source env_file.env and echo $DB_PORT gave me 3306, the env file was clearly loaded and DB_PORT not empty.

When I then ran

sqlacodegen mysql+pymysql://{DB_USER}:{DB_PASS}@{DB_HOST}/{DB_NAME}?charset=utf8 --outfile {PATH+FILE_NAME}

I still got the error:

...

  File "/home/MY_USER/.local/lib/python3.8/site-packages/sqlalchemy/engine/url.py", line 787, in _parse_rfc1738_args
    components["port"] = int(components["port"])
ValueError: invalid literal for int() with base 10: '{DB_PORT}'

Well. "Professional beginner" as I am, I did not see that this kind of parametrization was meant to be placeholders, while I thought you could actually parametrize bash commands like this.

This ran into other errors, but the DB_PORT error was gone:

sqlacodegen mysql+pymysql://$DB_USER:$DB_PASS@$DB_HOST:$DB_PORT/$DB_NAME?charset=utf8 --outfile $FILEPATH

Apart from that reason above, I had the same error again when I just used an environment variable in the command above that was not in the env file, this did not have to be DB_PORT and still the error was thrown for DB_PORT! Therefore, check all of your environment variables, they must all be filled, and I see that one of the 2019 answers has already said this. I guess I oversaw it or did not believe it would be the problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Batman
Solution 3