简体   繁体   English

关于python导入错误的问题

[英]Problem about python import with error

I have write a small python module with one class and two functions. 我编写了一个带有一个类和两个函数的小型python模块。 The skeleton of the module is as following: 模块的框架如下:

#file name: test_module.py
class TestClass:
  @classmethod
  def method1(cls, param1):
    #to do something
    pass

  def __init__(self, param1):
    #to do something
    ...
def fun1(*params):
  #to do something
  ...

def fun2(*params):
  #to do something
  ...

Another py file is a small script which imports function and class from the module, as following: 另一个py文件是一个小脚本,该脚本从模块中导入函数和类,如下所示:

    import sys
    from test_module import TestClass, fun1, fun2

    def main(sys_argv):
      li = range(5)
      inst1 = TestClass(li)
      fun1(inst1)
      fun2(inst1)
      return 

    if __name__ == "__main__":
      main(sys.argv) 

But when I execute the script, it is broken with following message: 但是当我执行脚本时,它被以下消息破坏:

  • from: can't read /var/mail/test_module 来自:无法读取/ var / mail / test_module
  • ./script.py: line 4: syntax error near unexpected token `(' ./script.py:第4行:意外令牌'('附近的语法错误

  • ./script.py: line 4: `def ./script.py:第4行:`def
    main(sys_argv):' main(sys_argv):'

I am not sure what the problem is. 我不确定是什么问题。 Is it a problem with import? 导入有问题吗? But when I try to import the module in ipython, everything is just ok. 但是,当我尝试在ipython中导入模块时,一切正常。

Add a proper shebang line to your "small script". 在您的“小脚本”中添加适当的shebang行。 It's being interpreted as a shell script. 它被解释为Shell脚本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM