1

I am trying to run a script(myscript.py) using python manage.py shell < scripts/myscript.py but nothing happens.

Here is my code

myscript.py

def foo(x):
    print(x+1)

if __name__ == '__main__':
    x = 10
    foo(x)

any help please

Note:

I am new to django

I am using python 3.6 and django 1.10.6

  • Make your script print out `__name__` and you will see why. – Klaus D. Nov 13 '18 at 06:38
  • What output and error messages do you get? – Red Cricket Nov 13 '18 at 06:42
  • @RedCricket there is no error messages. it looks like `__name__` is not `'__main__'` Output: ```Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> >>> >>> >>> >>> >>> >>> ... ... ... ... ... ... ... ... ... ... >>> ... ... ... ... ... ... ... ... ... >>> ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... >>> >>> ... ... ... ... ... ... ... ... ... ... now exiting InteractiveConsole...``` – Pahara Siagian Nov 13 '18 at 06:56
  • @KlausD. ok thank you. I'll try it – Pahara Siagian Nov 13 '18 at 06:57

1 Answers1

2

Since you are importing that script into shell, value of variable __name__ will not be __main__ (it will be the module name myscript). That means here that part of code is not executing.

For more info

itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • thank you for the help so it means that i should not use `if __name__ == '__main__'`? or can I change `'__main__'` into my module name? for example: `if __name__ == 'myscript'` – Pahara Siagian Nov 13 '18 at 07:41
  • I you need to import the function, remove that line and call function directly in the shell after import – itzMEonTV Nov 13 '18 at 08:20