當我從 Linux 命令提示符(bash)運行腳本時出現錯誤,但是當我直接在 Pycharm 中運行相同的腳本時,它作業正常。
這是代碼:
class EncodeKey:
def __new__(cls, *args):
if len(args) == 1:
return cls.generate_encode_key(args[0].upper())
return cls.get_encode_key(args)
...
...
if __name__ == '__main__':
encode_key = EncodeKey(*["something"])
print(encode_key)
正如我已經說過的,在 Pycharm 中,腳本運行良好,沒有任何錯誤,但這是從命令提示符運行腳本時得到的結果:
user@machine:~/my/path/to/the/script$ python py.py
Traceback (most recent call last):
File "py.py", line 93, in <module>
encode_key = EncodeKey(*["something"])
TypeError: this constructor takes no arguments
或者:
user@machine:~/my/path/to/the/script$ ls -lh
...
-rwxrwxr-x 1 user user 3.2K Jun 20 19:12 py.py
...
user@machine:~/my/path/to/the/script$ ./py.py
Traceback (most recent call last):
File "py.py", line 93, in <module>
encode_key = EncodeKey(*["something"])
TypeError: this constructor takes no arguments
當然,對于這樣一個不尋常的問題,我沒有找到任何解決方案。任何想法為什么會發生這種情況?以及如何解決?謝謝 !
uj5u.com熱心網友回復:
如果您以默認方式安裝了 python,則該python命令使用 Python 2。要與 Python 3 互動,請使用以下命令python3:
user@machine:~/my/path/to/the/script$ python3 py.py
Python 2 中的程式錯誤是因為 Python 2 中默認的舊樣式類不支持__new__. 如果您需要支持 Python 2,請通過以下方式使您的類具有新風格object:
class EncodeKey(object):
如果你這樣做,你的代碼仍然可以在 Python 3 中作業。
uj5u.com熱心網友回復:
也許您需要在終端中輸入您的虛擬環境。在您的檔案夾中打開終端。
型別:
.\venv\Scripts\activate
然后從虛擬環境中運行您的應用程式。
如果我是正確的,全域 Python 無法在你的 venv 中找到你的本地依賴項。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/493696.html
