Exe built with cx_Freeze shows error when containing pysftp import

主宰稳场 提交于 2020-06-28 09:38:11

问题


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

Here is my code:

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

Here is the setup:

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
)

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?

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.

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


回答1:


  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.

    EDIT: this does not solve the problem according to the OP.

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

    EDIT: 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.

    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,
        },    
    }
    

2nd EDIT:

  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,
        },    
    }
    



回答2:


Thank you to jpeg for providing the great suggestions that led to help fixing this issue. What I had to do was to copy _cffi_backend.cp37-win_amd64.pyd to the lib directory next to the built executable.

You can find this file under your python install ([Python version]\Lib\site-packages\_cffi_backend.cp37-win_amd64.pyd)



来源:https://stackoverflow.com/questions/57210744/exe-built-with-cx-freeze-shows-error-when-containing-pysftp-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!