2

I have a file :

def fetchArticles(topic):
    testing()
def testing():
    print("testing")
print("starting")
fetchArticles("World")

It is present in a app in django and when i run it as a standalone file using python3 xyz.py it runs perfectly fine but when i run it in django shell using python3 manage.py shell < xyz.py it is not able to locate the testing function.

It shows this error :

starting
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/shell.py", line 101, in handle
    exec(sys.stdin.read())
  File "<string>", line 12, in <module>
  File "<string>", line 4, in fetchArticles
NameError: name 'testing' is not defined

Can anyone help me to resolve this issue ?

Prashant Pokhriyal
  • 3,727
  • 4
  • 28
  • 40
  • Try moving the definition of `testing` so it comes before `fetchArticles` in your file. – Ammar Alyousfi Nov 02 '17 at 11:48
  • write testing function before fetchArticles functions – Vaibhav Nov 02 '17 at 12:22
  • Still not working. There seems to be some issue that shell is not defination for this file because older code is working fine in django shell, problem is only when i add something new. – Shubham Garg Nov 02 '17 at 12:24
  • try with opening new shell window and import script again – Vaibhav Nov 02 '17 at 12:26
  • It is working fine with python3 manage.py shell then using exec to run the file but it is still not working when i am invoking it directly using python3 manage.py shell < xyz.py . – Shubham Garg Nov 02 '17 at 13:27

0 Answers0