[英]Why does this python code hang on import/compile but work in the shell?
I'm trying to use python to sftp a file, and the code works great in the interactive shell -- even pasting it in all at once. 我正在尝试使用python sftp文件,并且该代码在交互式shell中非常有效-甚至可以一次全部粘贴。
When I try to import the file (just to compile it), the code hangs with no exceptions or obvious errors. 当我尝试导入文件(只是对其进行编译)时,代码将挂起,没有异常或明显的错误。
How do I get the code to compile, or does someone have working code that accomplishes sftp by some other method? 如何获取代码进行编译,或者有人拥有可以通过其他方法完成sftp的有效代码?
This code hangs right at the ssh.connect() statement: 这段代码挂在ssh.connect()语句上:
""" ProblemDemo.py Chopped down from the paramiko demo file. This code works in the shell but hangs when I try to import it! """ from time import sleep import os import paramiko sOutputFilename = "redacted.htm" #-- The payload file hostname = "redacted.com" ####-- WARNING! Embedded passwords! Remove ASAP. sUsername = "redacted" sPassword = "redacted" sTargetDir = "redacted" #-- Get host key, if we know one. hostkeytype = None hostkey = None host_keys = {} try: host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) except IOError: try: # try ~/ssh/ too, because windows can't have a folder named ~/.ssh/ host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) except IOError: print '*** Unable to open host keys file' host_keys = {} if host_keys.has_key(hostname): hostkeytype = host_keys[hostname].keys()[0] hostkey = host_keys[hostname][hostkeytype] print 'Using host key of type %s' % hostkeytype ssh = paramiko.Transport((hostname, 22)) ssh.connect(username=sUsername, password=sPassword, hostkey=hostkey) sftp = paramiko.SFTPClient.from_transport(ssh) sftp.chdir (sTargetDir) sftp.put (sOutputFilename, sOutputFilename) ssh.close()
That's indeed a bad idea to execute this kind of code at import time, although I am not sure why it hangs - it may be that import mechanism does something strange which interacts badly with paramiko (thread related issues maybe ?). 在导入时执行这种代码确实是一个坏主意,尽管我不确定为什么它会挂起-可能是导入机制做了一些奇怪的事情,与paramiko交互不良(可能是与线程有关的问题?)。 Anyway, the usual solution is to implement the functionality into a function:
无论如何,通常的解决方案是将功能实现为功能:
def my_expensive_function(args):
pass
if __name__ == '__main__':
import sys
my_expensive_functions(sys.args)
This way, just importing the module will not do anything, but running the script will execute the function with the given arguments at command line. 这样,仅导入模块将不会执行任何操作,但是运行脚本将在命令行中使用给定参数执行函数。
This may not be a direct reason why, but rarely do you ever want to have "functionality" executed upon import. 这可能不是直接的原因,但是您很少希望在导入时执行“功能”。 Normally you should define a class or function that you then call like this:
通常,您应该定义一个类或函数 ,然后像这样调用:
import mymodule
mymodule.run()
The "global" code that you run in an import typically should be limited to imports, variable definitions, function and class definitions, and the like... 您在导入中运行的“全局”代码通常应限于导入,变量定义,函数和类定义等。
Weirdness aside, I was just using import to compile the code. 除了怪异,我只是使用import来编译代码。 Turning the script into a function seems like an unnecessary complication for this kind of application.
对于这种应用程序,将脚本转换为函数似乎是不必要的复杂性。
Searched for alternate means to compile and found: 搜索替代的编译方法,发现:
import py_compile py_compile.compile("ProblemDemo.py")
This generated a pyc file that works as intended. 这生成了一个可以正常工作的pyc文件。 So the lesson learned is that import is not a robust way to compile python scripts.
因此,经验教训是导入不是编译python脚本的可靠方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.