簡體   English   中英

NameError when running Python script through Bash

[英]NameError When Running Python script through Bash

我正在嘗試通過 Bash 運行一些 python (Django)文件(對於某些 cronjobs); 但是我遇到了一些奇怪的錯誤。 安裝程序基本上是我使用 bash 運行的 a.sh 腳本,它加載一些源文件,然后通過 Django ZEA890F68C374CE43CE.CEE89B4347BDD26BFC6B7EE9A3B755DDZ 文件運行 python 文件出於演示目的,我已經注釋掉了我在測試期間使用的 bash 腳本的某些部分。

Bash 腳本


    #!/bin/bash
    
    source /home/grlaer/Desktop/mensam_games/bin/activate
    source /home/grlaer/Desktop/mensam_games/vars.env
    
    cd /home/grlaer/Desktop/mensam_games/cards_refactor
    
    #python3 manage.py shell < tcg_sku/test_bash.py
    ./manage.py shell < tcg_sku/test_bash.py
    
    #cat tcg_sku/test_bash.py | ./manage.py shell
    
    exit 0

Python 腳本

    from datetime import datetime
    
    print(datetime.now())
    
    def do_this():
        print("Its printing datetime")
        print(datetime.now())
        return None
    
    do_this()

錯誤/回溯

    2022-01-16 00:11:02.698550
    Its printing datetime
    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/grlaer/Desktop/mensam_games/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
        utility.execute()
      File "/home/grlaer/Desktop/mensam_games/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/grlaer/Desktop/mensam_games/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/home/grlaer/Desktop/mensam_games/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
        output = self.handle(*args, **options)
      File "/home/grlaer/Desktop/mensam_games/lib/python3.8/site-packages/django/core/management/commands/shell.py", line 93, in handle
        exec(sys.stdin.read())
    
    File "<string>", line 12, in <module>
    File "<string>", line 9, in do_this
    NameError: name 'datetime' is not defined

我從命令行運行 bash test_bash.sh,我得到了上面的錯誤; 但是,如果我將 datetime 設為全局變量,或者將 datetime 設為 function 參數,則它會按預期工作。 Likewise if I tweak the bash script so that instead of trying to run the python file from the django shell it runs with just python it works as intended.

同樣,我可以通過在我的導入下方添加以下內容來修復它,但這似乎不合適。


    globals().update(locals())

這個作品

    from datetime import datetime

    globals().update(locals())

    print(datetime.now())
    
    def do_this():
        print("Its printing datetime")
        print(datetime.now())
        return None
    
    do_this()

這個作品

    from datetime import datetime

    print(datetime.now())
    
    def do_this(datetime):
        print("Its printing datetime")
        print(datetime.now())
        return None
    
    do_this(datetime)

It appears the issue has something to do with managing local vs. global variables when running a Python script via Django Shell via a Bash script. 我的理解是,當我導入日期時間時,它會進入locals()字典,但它永遠不會被復制到globals()字典中。 因此,當運行 function do_this()時,它會在 function do_this() () 的locals()字典中查找日期時間,但它不存在所以它在globals()字典中查找它,它也不存在。 問題出在調用腳本時沒有傳遞globalslocals參數,那么默認情況下,將使用當前 scope 的globals()locals()字典。 所以我可以通過在導入后運行globals().update(locals())來修復它,但這似乎不是解決這個問題的正確方法。

thanks,I also met this issue that ember python django shell into bash script shell,the key point is indeed globals().update(locals())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM