Forums

Tasks pythonanywhere

Hello! Please? help me. If I run it manually, it works correctly, it can import all modules. If I run it via Tasks - it can't see modules 2025-04-06 11:54:05 -- Started task

Traceback (most recent call last): File "/home/LabJournal/LabBookeq/users/tasks.py", line 4, in <module> from .models import monthly_payment, Company, CompanyBalanceChange ImportError: attempted relative import with no known parent package

2025-04-06 11:54:13 -- Completed task, took 8.57 seconds, return code was 1.

2025-04-06 11:57:07 -- Started task

Traceback (most recent call last): File "/home/LabJournal/LabBookeq/users/tasks.py", line 4, in <module> from users.models import monthly_payment, Company, CompanyBalanceChange ModuleNotFoundError: No module named 'users'

2025-04-06 11:57:19 -- Completed task, took 12.25 seconds, return code was 1.

my task: /home/LabJournal/LabBookeq/eqvenv/bin/python /home/LabJournal/LabBookeq/users/tasks.py

If you are trying to import from a specific directory, you need to make sure that the working directory is correctly set when the task starts. Add cd /the/path; to the start of your always on task. Replace /the/path with the correct working directory for your code.

Sorry, I cant understand. This path for task is correct ( /home/LabJournal/LabBookeq/eqvenv/bin/python /home/LabJournal/LabBookeq/users/tasks.py ) - it can find the file and open it. But in the opened file it cant open modules ( .models or users.models ), although it correct and can be open in all another cases. In which place should I point "cd /the/path" and wich path sould I point? /home/LabJournal/LabBookeq/users/models.py ?

Not the path to the file - the working directory. You cd into a directory when you want to run that code. You also need to cd into that directory as part of running the code for the task

"You also need to cd into that directory as part of running the code for the task" - in which place should I cd into directory to achieve modul users.models ?

This is not working /home/LabJournal/LabBookeq/eqvenv/bin/python cd /home/LabJournal/LabBookeq/users /home/LabJournal/LabBookeq/users/tasks.py

/home/LabJournal/LabBookeq/eqvenv/bin/python: can't open file '/home/LabJournal/cd': [Errno 2] No such file or directory

2025-04-07 10:59:24 -- Completed task, took 9.85 seconds, return code was 2.

cd /home/LabJournal/LabBookeq/users /home/LabJournal/LabBookeq/eqvenv/bin/python /home/LabJournal/LabBookeq/users/tasks.py

2025-04-07 11:04:16 -- Started task

bash: line 0: cd: too many arguments

2025-04-07 11:04:24 -- Completed task, took 8.60 seconds, return code was 1.

cd /home/LabJournal/LabBookeq/users & /home/LabJournal/LabBookeq/eqvenv/bin/python /home/LabJournal/LabBookeq/users/tasks.py it is not working too

When you run it manually, what are the exact commands that you run? That is, if you were to start a new Bash console from the "Consoles" page, what would you type into it?

In bash the same problem.

cd LabBookeq  ; source eqvenv/bin/activate; python /home/LabJournal/LabBookeq/users/tasks.py   - Traceback (most recent call last):
  File "/home/LabJournal/LabBookeq/users/tasks.py", line 4, in <module>
    from users.models import monthly_payment, Company, CompanyBalanceChange
ModuleNotFoundError: No module named 'users'.

But If I run it via "request" - it works correctly. Why so? Why it can't see modul users?

[edit by admin: formatting]

If I put this in tasks.py : import sys sys.path.append('/home/LabJournal/LabBookeq') - the answer is:

Traceback (most recent call last):
  File "/home/LabJournal/LabBookeq/users/tasks.py", line 8, in <module>
    from users.models import monthly_payment, Company, CompanyBalanceChange
  File "/home/LabJournal/LabBookeq/users/models.py", line 2, in <module>
    from django.contrib.auth.models import User
  File "/home/LabJournal/LabBookeq/eqvenv/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/LabJournal/LabBookeq/eqvenv/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 57, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/LabJournal/LabBookeq/eqvenv/lib/python3.9/site-packages/django/db/models/base.py", line 129, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/LabJournal/LabBookeq/eqvenv/lib/python3.9/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/home/LabJournal/LabBookeq/eqvenv/lib/python3.9/site-packages/django/apps/registry.py", line 137, in check_apps_ready
    settings.INSTALLED_APPS
  File "/home/LabJournal/LabBookeq/eqvenv/lib/python3.9/site-packages/django/conf/__init__.py", line 102, in __getattr__
    self._setup(name)
  File "/home/LabJournal/LabBookeq/eqvenv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the
 environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

[edit by admin: formatting]

Help me please

The error message is pretty clear about what you need to do:

Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Do one of those things.

But I already have it in my wsgi : import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'LabJournal.settings')

application = get_wsgi_application()

What should I do?

The WSGI file is only used when running your website -- the scheduled task system doesn't use it.

It sounds like what you are trying to do is run some Django code in a scheduled task. For that, I would suggest that you use a custom management command.

.

Using this instruction I was able to set up the task. Thanks for your help.

Great, glad to hear you were able to get it working!