【问题标题】:PyPac PyInstaller can't access the internetPyPac PyInstaller 无法访问互联网
【发布时间】:2018-02-08 08:25:02
【问题描述】:

我有一个通过代理服务器访问 Internet 的程序,我正在使用 PyPac 的 PACSession 进行会话。当我用 python 调用程序时,这很好用。但是,用 pyinstaller 编译它会给我一个错误。

测试程序:

import sys
import os
from pypac import PACSession
from requests.auth import HTTPProxyAuth
import getpass
import keyring
url = r'https://leilookup.gleif.org/api/v1/leirecords?lei='
lei_list = ['I9O8MELCUVOTLJABOX92', '5493004NRE4TCTRC2D29', 'VBHFXSYT7OG62HNT8T76', '529900M2YBAPOUTTM178', '549300KJFL0KHN20KH82',
            '0NTV4GLMGXKW55GJC835']

def main():
    lei_numbers = ",".join(lei_list)
    internet = 'gleif.org'
    password = keyring.get_password(internet, getpass.getuser())
    if not password:
        keyring.set_password(internet, getpass.getuser(), getpass.getpass('Please enter your Login password: '))
    print('Before PACSession')
    session = PACSession(proxy_auth=HTTPProxyAuth(getpass.getuser(), keyring.get_password(internet, getpass.getuser())))
    page = session.get(url=url + lei_numbers)
    json = page.json()
    for ix in range(len(json)):
        print(json[ix]['LEI']['$'] + ': ' + json[ix]['Entity']['LegalName']['$'])

    return 0

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

版本:

Windows 7
python 3.6.3 
pypack 0.5.0
pyinstaller 3.3
requests 2.18.4
tld 0.7.9; 

错误:

Traceback (most recent call last):
  File "site-packages\tld\utils.py", line 117, in get_tld_names
  File "c:\python36\lib\codecs.py", line 895, in open
    file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: '...\\AppData\\Local\\Temp\\_MEI299882\\tld\\res\\effective_t
ld_names.dat.txt'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "urllib\request.py", line 1318, in do_open
  File "http\client.py", line 1239, in request
  File "http\client.py", line 1285, in _send_request
  File "http\client.py", line 1234, in endheaders
  File "http\client.py", line 1026, in _send_output
  File "http\client.py", line 964, in send
  File "http\client.py", line 936, in connect
  File "socket.py", line 724, in create_connection
  File "socket.py", line 713, in create_connection
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of ti
me, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages\tld\utils.py", line 74, in update_tld_names
  File "urllib\request.py", line 223, in urlopen
  File "urllib\request.py", line 526, in open
  File "urllib\request.py", line 544, in _open
  File "urllib\request.py", line 504, in _call_chain
  File "urllib\request.py", line 1346, in http_open
  File "urllib\request.py", line 1320, in do_open
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly resp
ond after a period of time, or established connection failed because connected host has failed to respond>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ScreenScraping.py", line 51, in <module>
  File "ScreenScraping.py", line 41, in main
  File "site-packages\requests\sessions.py", line 521, in get
  File "site-packages\pypac\api.py", line 179, in request
  File "site-packages\pypac\api.py", line 244, in get_pac
  File "site-packages\pypac\api.py", line 48, in get_pac
  File "site-packages\pypac\api.py", line 72, in collect_pac_urls
  File "site-packages\pypac\wpad.py", line 31, in proxy_urls_from_dns
  File "site-packages\tld\utils.py", line 169, in get_tld
  File "site-packages\tld\utils.py", line 124, in get_tld_names
  File "site-packages\tld\utils.py", line 84, in update_tld_names
tld.exceptions.TldIOError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because connected host has failed to respond>
[29744] Failed to execute script ScreenScraping

如何使编译后的程序与 pyinstaller 一起工作?

【问题讨论】:

    标签: python python-3.x httprequest pyinstaller proxy-server


    【解决方案1】:

    您的实际问题是:

    FileNotFoundError: [Errno 2] No such file or directory: '...\\AppData\\Local\\Temp\\_MEI299882\\tld\\res\\effective_tld_names.dat.txt'
    

    基本上,pypac 使用的 tld 包会加载该文本文件,并且一旦使用 PyInstaller 打包,该文件就不再可用。您可以使用规范文件中的 --add-data 标志或 datas 字段将其包含在 PyInstaller 二进制文件中。

    datas: [('C:\Python\Lib\site-packages\tld\res\effective_tld_names.dat.txt', 'tld\res')]
    

    【讨论】:

    • 字符串也应该有 r"some string" 前缀
    【解决方案2】:

    如果您尝试从上面的答案中复制粘贴解决方案,则尝试此操作并确保适当更改您的 python 路径

    datas= [('C:\\Python\\Lib\\site-packages\\tld\\res\\effective_tld_names.dat.txt', 'tld\\res')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2014-11-16
      • 2016-02-20
      • 2019-08-23
      • 2021-01-26
      • 2020-02-07
      相关资源
      最近更新 更多