包含pysftp导入时,用cx_Freeze构建的EXE显示错误 [英] Exe built with cx_Freeze shows error when containing pysftp import

查看:127
本文介绍了包含pysftp导入时,用cx_Freeze构建的EXE显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从使用pysftp的python脚本编译可执行文件.我正在使用cx_Freeze来做到这一点.

I am trying to compile an executable from a python script that uses pysftp. I'm using cx_Freeze to do that.

这是我的代码:

Test.py

import datetime
import time
import os
import pysftp

i = 0
while(i<10):
    tm = datetime.datetime.now()
    print (tm.strftime('%H:%M:%S'))
    time.sleep(1)
    i += 1

这是设置:

setup.py

from cx_Freeze import setup, Executable

base = None    

executables = [Executable("Test.py", base=base)]

packages = ["idna", "datetime", "time", "os", "pysftp"]
options = {
    'build_exe': {    
        'packages':packages,
    },    
}

setup(
    name = "<any name>",
    options = options,
    version = "<any number>",
    description = '<any description>',
    executables = executables
)

当我从命令行运行test.py时,它工作正常.但是当我运行运行命令python setup.py build后生成的exe时,test.exe失败并显示以下内容:

When I run test.py from the command line, it works fine. But when I run the exe that is built after running the command python setup.py build, test.exe fails and displays this:

C:\Users\cb\Desktop\Python Scripts\Test cx_Freeze install\build\exe.win-amd64-3.7>Test.exe
Traceback (most recent call last):
  File "C:\Users\cb\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Users\cb\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts\Console.py", line 23, in run
    exec(code, {'__name__': '__main__'})
  File "Test.py", line 4, in <module>
  File "C:\Users\cb\AppData\Local\Programs\Python\Python37\lib\site-packages\pysftp\__init__.py", line 12, in <module>
    import paramiko
  File "C:\Users\cb\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko\__init__.py", line 22, in <module>
    from paramiko.transport import SecurityOptions, Transport
  File "C:\Users\cb\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko\transport.py", line 90, in <module>
    from paramiko.ed25519key import Ed25519Key
  File "C:\Users\cb\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko\ed25519key.py", line 17, in <module>
    import bcrypt
  File "C:\Users\cb\AppData\Local\Programs\Python\Python37\lib\site-packages\bcrypt\__init__.py", line 25, in <module>
    from . import _bcrypt
ModuleNotFoundError: No module named '_cffi_backend'

关于我应该尝试什么的任何建议?

Any suggestions on what I should try?

我已经尝试将"cryptography""paramiko"添加到软件包列表中.我在网上看了一下,发现我可能必须明确声明要用于cx_Freeze的lib,但是我不确定那是什么.

I have already tried adding "cryptography" and "paramiko" to the packages list. I've looked online and found that I may have to explicitly state the lib I am using for cx_Freeze, but I am not sure what that is.

我正在使用python 3.7.3-64bit和Windows 10.

I'm using python 3.7.3-64bit and windows 10.

推荐答案

  1. 尝试将"paramiko""bcrypt"添加到setup.py脚本的packages列表中.如果仍然无法解决问题,请发布您应该获得的新回溯.

  1. Try to add "paramiko" and "bcrypt" to the packages list in your setup.py script. If this still does not work, please post the new traceback you should get.

根据OP,这不能解决问题.

this does not solve the problem according to the OP.

C:\Users\cb\AppData\Local\Programs\Python\Python37\lib\site-packages中搜索名为_cffi_backend*.*的文件,找到了吗?

Search for a file named _cffi_backend*.* in your C:\Users\cb\AppData\Local\Programs\Python\Python37\lib\site-packages, do you find anything?

根据 OP的答案,有文件[Python version]\Lib\site-packages\_cffi_backend.cp37-win_amd64.pyd,并将此文件手动复制到生成的可执行文件旁边的目录即可解决此问题.

according to OP's answer, there is file [Python version]\Lib\site-packages\_cffi_backend.cp37-win_amd64.pyd, and copying this file manually to the lib directory next to the built executable solves the issue.

您应该能够通过修改setup.py脚本,让cx_Freeze自动执行此附加步骤:

You should be able to let cx_Freeze do this additional step automatically by modifying your setup.py script like this:

import _cffi_backend
_cffi_backend_file = _cffi_backend.__file__
include_files = [(_cffi_backend_file, 'lib')]
options = {
    'build_exe': {
       'include_files': include_files,    
       'packages': packages,
    },    
}

第二次

  1. 代替上面的建议,尝试简单地将_cffi_backend添加到setup.py脚本中build_exe选项的includes列表中:

  1. Instead of the above proposals, try to simply add _cffi_backend to the includes list of the build_exe options in your setup.py script:

includes = ['_cffi_backend']
options = {
    'build_exe': {
       'includes': includes,    
       'packages': packages,
    },    
}

这篇关于包含pysftp导入时,用cx_Freeze构建的EXE显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆