Forums

Syntex Error in manage.py - ) from exc

I have read all of the relevant questions on this forum but nothing works. Firstly pythonanywhere shows an error in manage.py line 16 which is ) from exc This worked well but after I put secret key and db variable in .env file, I guess manage.py is unable tp won't pick them. When I run python manage.py collectstatic OR python manage.py shell it give me error:

TypeError: can only concatenate str (not "NoneType") to str
    DB_NAME = 'faisalakhlaq$'+ db_n
db_n is a variable in production.py 
db_n = os.getenv("DB_NAME")

The site is running fine without any problems. But I cannnot collectstatic and make change.

You need to set the environmental variable DB_NAME when you run collectstatic. Is it set in the wsgi file for your web app? That is not executed when you run the management command.

Hi, Thank you for replying. Actually I have all the variable in the .env file. Now I can run python manage.py shell but if I try to run a task file with

python manage.py telecompanies/tasks.py

It give me the SECRET KEY error:

Traceback (most recent call last):

File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/init.py", line 224, in fetch_comman d app_name = commands[subcommand] KeyError: 'telecompanies/tasks.py' During handling of the above exception, another exception occurred: Traceback (most recent call last):

  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from
_command_line
    utility.execute()
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 231, in fetch_comman
d
    settings.INSTALLED_APPS
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
    self._setup(name)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/conf/__init__.py", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/conf/__init__.py", line 196, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

I have this task in Tasks on PA, to run once a day and that give me the following import error.

Traceback (most recent call last):
  File "/home/faisalakhlaq/mobiletilbud/telecompanies/tasks.py", line 5, in <module>
    from telecompanies.tilbud_spider import TelenorSpider, TeliaSpider, ThreeSpider, YouSeeSpider
ModuleNotFoundError: No module named 'telecompanies'

2020-12-14 14:58:05 -- Completed task, took 2.00 seconds, return code was 1.

The telecompanies is the app name. There is problem in the import. In local it works fine but I don't know how to import from same and other apps when I deply on heroku. I want to fix this before I can move to custom domain. So in order to figure out that import error I try to run the task using bash but then it goes to SECRET Key problem as pasted above. Thank you for helping.

Have you set up your virtualenv to load the .env file? https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/

If you cannot import something in a scheduled task, then you should check your sys.path in the task. Have a look at our help page (the theory section at the start) so that you can understand how Python searches for the modules that you try to import: https://help.pythonanywhere.com/pages/DebuggingImportError/

Hi glenn, I have read the two above links multiple time could couldn't figure out the problem unfrotunately. You can see some of the configurations:

(mobileenv) 21:50 ~/mobiletilbud (main)$ nano ../.virtualenvs/mobileenv/bin/postactivate                                                    
(mobileenv) 21:51 ~/mobiletilbud (main)$ python -i /var/www/www_mobiletilbud_dk_wsgi.py
>>> import mobiletilbud.settings
>>> print('\n'.join(sys.path))
/home/faisalakhlaq/mobiletilbud
/var/www
/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python38.zip
/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8
/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/lib-dynload
/usr/lib/python3.8
/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages
/home/faisalakhlaq/mobiletilbud
>>> exit()
(mobileenv) 21:53 ~/mobiletilbud (main)$ cat ../.virtualenvs/mobileenv/bin/postactivate                                                     
#!/bin/bash
# This hook is sourced after this virtualenv is activated.
set -a; source ~/mobiletilbud/.env; set +a

(mobileenv) 21:53 ~/mobiletilbud (main)$ echo $SECRET_KEY
(mobileenv) 21:55 ~/mobiletilbud (main)$

echo secret_key give nothing. I am trying to loaddata to the database and here is what I get.

(mobileenv) 21:55 ~/mobiletilbud (main)$ python manage.py loaddata datadump.json                                                            
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from
_command_line
    utility.execute()
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/base.py", line 368, in execute
    self.check()
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/base.py", line 392, in check
    all_issues = checks.run_checks(
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/checks/registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/checks/model_checks.py", line 18, in check_all_mod
els
    models = apps.get_models()
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/apps/registry.py", line 179, in get_models
    self.check_models_ready()
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/apps/registry.py", line 141, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
(mobileenv) 21:56 ~/mobiletilbud (main)$

And if I try to run the same command with Django-admin then this is shown

(mobileenv) 21:56 ~/mobiletilbud (main)$ django-admin loaddata datadump.json                                                                
Traceback (most recent call last):
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/base.py", line 368, in execute
    self.check()
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/management/base.py", line 392, in check
    all_issues = checks.run_checks(
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/checks/registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/core/checks/urls.py", line 35, in check_url_namespaces_
unique
    if not getattr(settings, 'ROOT_URLCONF', None):
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
    self._setup(name)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/conf/__init__.py", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/site-packages/django/conf/__init__.py", line 177, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/faisalakhlaq/.virtualenvs/mobileenv/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mobiletilbud'
During handling of the above exception, another exception occurred:

Many thanks for any help in advance.

It looks like you need to run your manage.py with the --settings pointing to your settings or with DJANGO_SETTINGS_MODULE variable set in the environment you run your loaddata management command. See https://docs.djangoproject.com/en/3.1/topics/settings/#designating-the-settings

Hi fjl, thank you for answering my question. Now I have the DJANGO_SETTINGS_MODULE in my .env file and manage.py file as well (pointing to the production.py setting file). Still if I try to run a task script from bash using python manage.py someapp/script.py it give me SECRET_KEY setting must not be empty. And the task I have created gives an error:

File "/home/faisalakhlaq/mobiletilbud/telecompanies/tasks.py", line 6, in <module>
    from .tilbud_spider import TelenorSpider, TeliaSpider, ThreeSpider, YouSeeSpider
ImportError: attempted relative import with no known parent package

I think I have done something wrong somewhere but cannot figure it out. Only one problem that I can see is when I start the bash in the virtualenv the first line that appears is:

bash: 2nwoa3m=-ut6dlhl=a: command not found

and running the manage with --settings= give me

ModuleNotFoundError: No module named 'mobiletilbud/settings/production'

Kindly respond if you can figure out anything. Thank you.

That error starting the Bash console sounds strange -- do you get the same error if you start a normal Bash console from the "Consoles" page, and then run the command

workon mobileenv

...?

The way you are running the management commands also looks a little odd; normally a custom management command would live in a directory like someapp/management/commands/name_of_command.py and you would run it using just

./manage.py name_of_command

More information here.

Hi giles. When I start console without virtualenv then there is no error like that and afterwards when I do workon i get:

00:36 ~ $ workon mobileenv
bash: 2nwoa3m=-ut6dlhl=a: command not found
[1]+  Done                    export SECRET_KEY=.........

And sorry that is my bad, running a script with manage.py which is not a command, I was myself wondering how a file will run but I saw it somewhere in the forums that if we want to test our tasks then we can run them in the bash with python manage.py .. command. Should I provide a management command in the task as well? Because my task fails with import error.

If you want your script that you run as a scheduled task to use django you should write it as management command.

Hej fjl, All the other issue remain unsolved unfortunately but the one with the task is most important as there is no workaround. I have a task.

python3.8 /home/faisalakhlaq/mobiletilbud/telecompanies/management/commands/fetch_tilbud.py

It gives me import error somewhere. I have read all the above links that were recommended. Here is the error:

from .tilbud_spider import task_fetch_offers
ImportError: attempted relative import with no known parent package

instead of .tilbud_spider if I write telecompanies.tilbud_spider still I get some error. You can see the fetch_tilbud.py is inside the telecompanies app. Locally it works fine.

This SO post has an excellent explanation of how relative imports work - search for "PEP 328" in the page

Hi glenn. Thank you for replying. Today I will ask for help again after spending so many hours to figure this out.

I am getting import error for my scheduled task which is a command. I followed this question and I can see I can run the command from the bash. But that works only if I put the actual values in my settings file. i.e. currently my settings has SECRET_KEY = os.getenv("SECRET_KEY"). If I change it to e.g SECRET_KEY= 'sdfdsff/asd/anything-secret'. Then I am able to run my command.

The website is working fine but only the tasks have the problem. I also followed ImportError issues from your forum and it seems to work perfectly for me. Here is its output:

python -i /var/www/www_somname_wsgi.py from telecompanies.tilbud_spider import task_fetch_offers import telecompanies print(telecompanies.file) /home/path/to/telecompanies/init.py import mobiletilbud.settings.production

If I have to run any command using django-admin or manage.py e.g. compilemessage, I have to put the actual values in the settings.py file and only after that the command will run. Thank you.

os.getenv gets the value from the environment. If you have not set it in the environment of the console or the task, then it will not be there.

You can either set the values in your .bashrc, or use an env file: https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/

Hi again glenn. I had all the environment variable set in .env file and now I have them in .bashrc as well

export SECRET_KEY=---sdfdsfdsfds
export DB_NAME=---sdfdsfdsfds
...............................
...............................
..............................

Still I have to go and change the settings file and give the exact value for running a management command. otherwise it will throw: raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.") django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Any ideas? Many thanks.

Make sure you're loading the correct .env file in your wsgi file (as described in the link that I posted earlier) and that you have reloaded your web app after making additions to the env file or changes to the wsgi file.