V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
PEax
V2EX  ›  Python

Python 打包 exe 无法使用 socks5 代理,疑似功能包打包失败

  •  
  •   PEax · 2022-10-19 14:21:52 +08:00 · 2006 次点击
    这是一个创建于 554 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1.已经安装了 requests[socks] 包,代码运行没问题
    2.pyinstaller 打包 exe 后打开,使用 socks5 代理提示: [ Missing dependencies for SOCKS support.]
    3.怀疑是 sock 包没打包进去,手动引入 import socks, 打包后提示 [ No module named 'socks']

    google 查阅,没有相关的资料,有大佬遇到过吗
    12 条回复    2022-10-20 15:30:18 +08:00
    masker
        1
    masker  
       2022-10-19 14:26:11 +08:00 via Android
    pyinstaller -p
    指向你的 site-packages 路径
    fbichijing
        2
    fbichijing  
       2022-10-19 14:41:10 +08:00   ❤️ 1
    因为没有代码,场景无法复现。

    不过最近的 pyinstaller 似乎有些问题,我用最新版本的 pyinstaller 打包的时候也出现了一些莫名其妙的错误。由此我一般都将 pyinstaller 回退到以前的版本,4.9 还是 5.1 ,不知道最新版本有没有修好了。个人之见。
    PEax
        3
    PEax  
    OP
       2022-10-19 18:29:38 +08:00
    @fbichijing
    import requests
    res = requests.get('https://www.baidu.com', proxies={"http": "socks5://127.0.0.1:5500", "https": "socks5://127.0.0.1:5500"})
    print(res.text)

    pyinstaller -F xxx.py

    就是最简单的访问,就这几行代码
    fbichijing
        4
    fbichijing  
       2022-10-19 18:37:01 +08:00
    @PEax 我记得要使用 socks5 代理还要加个 h 。

    python 3.8
    pyinstaller 4.9


    ```python
    import os
    import requests
    from lxml import etree

    url = 'https://google.com'
    proxies = {"http": "socks5h://127.0.0.1:1080",
    "https": "socks5h://127.0.0.1:1080"}
    res = requests.get(url, proxies=proxies)

    response = etree.HTML(res.text)
    print(response.xpath('//title/text()'))
    os.system('pause')
    ```
    fbichijing
        5
    fbichijing  
       2022-10-19 18:40:52 +08:00
    @PEax

    打包一切正常,上面的回复点得太快,话还没说完就发出去了......
    PEax
        6
    PEax  
    OP
       2022-10-19 19:03:10 +08:00
    @fbichijing 我发现是我 mac 用虚拟机打包的问题,我换成 win 就不会了,并且加上 h 就好了!,谢谢大佬
    ysc3839
        7
    ysc3839  
       2022-10-19 20:19:36 +08:00 via Android
    @fbichijing socks5h 是 curl 的写法,其他库不一定支持
    https://curl.se/libcurl/c/CURLOPT_PROXY.html
    fbichijing
        8
    fbichijing  
       2022-10-19 20:31:18 +08:00
    @ysc3839 requests 需要。https://stackoverflow.com/questions/12601316/how-to-make-python-requests-work-via-socks-proxy#answer-15661226 评论里也有讨论。我仅记得当时我也遇到了什么报错,查找了很多网页,有看到过一个网页写了很详细的解释。时间过于久远,当时我也没有记笔记或者保存脱机页面的习惯。后来就只留下这里加一个 h 就不会报错的印象在脑海里......^_^
    laqow
        9
    laqow  
       2022-10-19 21:47:36 +08:00
    pyinstaller 打包在 import 时可能要具体到函数,用 form requests import get, res = get('https://www.baidu.com', proxies={"http": "socks5://127.0.0.1:5500", "https": "socks5://127.0.0.1:5500"}) 可能就行了
    gablic
        10
    gablic  
       2022-10-20 09:35:16 +08:00
    怀疑环境问题,试试指定 pyinstaller 和 site-packages 的目录
    python "pyinstaller.exe 路径" --onefile --path "site-packages 路径" xxx.py
    ungrown
        11
    ungrown  
       2022-10-20 11:32:11 +08:00
    @fbichijing #8 也就是说其实不是“漏库”的原因,而是 scheme 相关的 trick
    rev1si0n
        12
    rev1si0n  
       2022-10-20 15:30:18 +08:00
    直接在入口脚本先

    import socks
    import sockshandler

    然后再打包
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1821 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:33 · PVG 00:33 · LAX 09:33 · JFK 12:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.