简体   繁体   English

为什么 python 脚本在 pycharm 中运行和在命令提示符下运行时的行为不同?

[英]Why does a python script behaves differently when it is run in pycharm and when it is run from a command prompt?

I'm getting an error when I run a script from the Linux command prompt(bash), but when I run the same script in the Pycharm directly, it works fine.当我从 Linux 命令提示符(bash)运行脚本时出现错误,但是当我直接在 Pycharm 中运行相同的脚本时,它工作正常。

Here is the code:这是代码:

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)

As I already told, in the Pycharm the script works fine without any errors, but here is what I'm getting when the script is run from the command prompt:正如我已经说过的,在 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

Or:或者:

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

Of, course, I didn't find any solutions on such an unusual problem.当然,对于这样一个不寻常的问题,我没有找到任何解决方案。 Any ideas why is that happening ?任何想法为什么会发生这种情况? And how to solve it ?以及如何解决? Thanks !谢谢 !

If you've installed python in the default way, the python command uses Python 2. To interperet with Python 3, use the command python3 :如果您以默认方式安装了 python,则python命令使用 Python 2。要与 Python 3 交互,请使用命令python3

user@machine:~/my/path/to/the/script$ python3 py.py

The program errors in Python 2 because old-style classes, the default in Python 2, do not support __new__ . Python 2 中的程序错误是因为旧样式类(Python 2 中的默认值)不支持__new__ If you need to support Python 2, make your class new-style by extending object :如果您需要支持 Python 2,请通过扩展object使您的类具有新风格:

class EncodeKey(object):

Your code will still work in Python 3 if you do this.如果你这样做,你的代码仍然可以在 Python 3 中工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 2 pdb:在pdb提示符下运行时,语句的行为有所不同 - Python 2 pdb: a statement behaves differently when run at the pdb prompt 为什么这在Python IDLE shell中有效,但在我从命令提示符下作为Python脚本运行时却没有? - Why does this work in the Python IDLE shell but not when I run it as a Python script from the command prompt? 当使用 cron 管理器时,脚本在从 cron 作业和使用 django manage.py 的命令行运行时表现不同 - Script behaves differently when run from cron job and from command line using django manage.py when using a cron supervisor Python 3.2.2在IDLE中运行时与在桌面上运行时的行为不同 - Python 3.2.2 IF behaves differently when run in IDLE vs. run from desktop 从IDLE或命令行运行时,Web.py的行为有所不同 - Web.py behaves differently when run from IDLE or command line 当将简短的Python脚本作为函数隔离时,为什么运行方式不同? - Why does short Python script run differently when it's isolated as a function? 为什么Windows 10 Task Scheduler执行我的Python脚本时会产生不同的运行 - Why does my Python script run differently when executed by Windows 10 Task Scheduler 为什么此python脚本仅在从命令行运行时才会失败? - Why does this python script only fail when run from the command line? IDLE 找不到我的模块,但是当我从命令提示符运行脚本时它确实找到了 - IDLE can't find my modules but when I run script from command prompt it does 使用python在Popen中运行Shell脚本的行为在python命令行和实际程序中有所不同 - Use python to run shell script with Popen behaves differently in python command line and actual program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM