Forums

Import not working on server

Hello everyone!

I'm having trouble trying to run a simple script in my web app and I can't see why it won't work.

This is the piece of code I'm trying to make work:

from contratos.models import Contrato
from django.db.models import Q

def gerar_relatorio_chassis():
  contratos = Contrato.objects.exclude(
    Q(proposta=True) |
    Q(status='Pendente') |
    Q(numero=None)
  )

Nothing fancy, right? Just a simple query in Django, but when I try running this code using the command python manage.py shell < file_name.py I get an error.

NameError: name 'Contrato' is not defined

Of course when I try running the same code on my machine it works hahaha. And yes, I'm running the command while my virtual environment is activated.

Can somebody help me?

PS: I have a similar code in another web app and it works fine.

Could you provide the full stack trace?

Of course! Here it is.

Traceback (most recent call last):
  File "/home/marketbi/consulta-dms/manage.py", line 22, in <module>
    main()
  File "/home/marketbi/consulta-dms/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/marketbi/.virtualenvs/dmsvenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/marketbi/.virtualenvs/dmsvenv/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/marketbi/.virtualenvs/dmsvenv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/marketbi/.virtualenvs/dmsvenv/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/home/marketbi/.virtualenvs/dmsvenv/lib/python3.8/site-packages/django/core/management/commands/shell.py", line 93, in handle
    exec(sys.stdin.read())
  File "<string>", line 208, in <module>
  File "<string>", line 14, in gerar_relatorio_chassis
NameError: name 'Contrato' is not defined

Are you sure that code is what you are running? That code cannot raise that exception. It appears that you are running that code through a management shell. Did you import the class in that shell?

Yes, I'm positive that this is the code running.

I've tried running the same file as a hourly task and had no success as well. What I can't understand is why this code doesn't work while a very similar code that I have in another web app runs without any issues.

The command of the hourly task is the following:

/home/marketbi/.virtualenvs/dmsvenv/bin/python /home/marketbi/consulta-dms/manage.py shell < /home/marketbi/consulta-dms/relatorios.py

I also have another task in this web app that cannot import functions or classes. Is it possible that there is a problem with this specific web app?

Make sure that you are setting the working directory of your code correctly: https://help.pythonanywhere.com/pages/ScheduledTasks/#make-sure-you-take-account-of-the-working-directory