推荐学习书目
› 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
sakura1
V2EX  ›  Python

Python 如何打包依赖模块到服务器,类似 mvn 的 package+assemble

  •  1
     
  •   sakura1 · Apr 2, 2020 · 3975 views
    This topic created in 2416 days ago, the information mentioned may be changed or developed.

    如题,现在准备用 python 写一个 kafka 的 producer 造测试数据,但是公司服务器没联外网, pip install pykafka 显然是不可能能的。公司服务器是 linux 的,然后我的开发环境是 Mac OS,打包成 exe 八成没戏,仲么办?

    20 replies  •  2020-04-02 20:33:40 +08:00
    bwangel
    bwangel
    Apr 2, 2020
    1
    用 Docker 。

    ø> docker help export

    Usage: docker export [OPTIONS] CONTAINER

    Export a container's filesystem as a tar archive

    Options:
    -o, --output string Write to a file, instead of STDOUT

    ø> docker help import

    Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

    Import the contents from a tarball to create a filesystem image

    Options:
    -c, --change list Apply Dockerfile instruction to the created image
    -m, --message string Set commit message for imported image
    --platform string Set platform if server is multi-platform capable
    ipwx
    ipwx
    Apr 2, 2020
    2
    anaconda
    dapang1221
    dapang1221
    Apr 2, 2020
    3
    venv 啊
    sakura1
    sakura1
    OP
    Apr 2, 2020
    4
    @bwangel 以后等公司服务器里有 docker 再试试把,docker 没有 docker
    sakura1
    sakura1
    OP
    Apr 2, 2020
    5
    @ipwx 兄弟,没网啊,我想向 java 一样,在开发环境上把依赖啥的打包好,然后到服务器直接执行。
    wangyzj
    wangyzj
    Apr 2, 2020
    6
    pyinstaller
    wuwukai007
    wuwukai007
    Apr 2, 2020 via Android
    7
    下载 whl 的包离线安装
    fcmio
    fcmio
    Apr 2, 2020 via iPhone
    8
    😲我前几天问过差不多的问题
    nifury
    nifury
    Apr 2, 2020
    9
    https://stackoverflow.com/questions/11091623/how-to-install-packages-offline
    第二个回复可以只下载不安装,然后再转移到测试机上
    或者直接下载 wheel 文件安装?
    sakura1
    sakura1
    OP
    Apr 2, 2020
    10
    venv 啥的好像是 python3 才有的吧,服务器里就一个 linux 自带的 python2,我继续撸 java 吧
    Trim21
    Trim21
    Apr 2, 2020
    11
    找台有外网的 linux 服务器或者虚拟机包 exe 比较省劲,又要跨平台又要离线安装的需求太罕见了
    deljuven
    deljuven
    Apr 2, 2020
    12
    容器镜像或者虚拟机镜像,venv 也可以,但是如果跨平台的话就有问题
    bwangel
    bwangel
    Apr 2, 2020
    13
    https://github.com/Parsely/pykafka/blob/master/pykafka/rdkafka/_rd_kafkamodule.c

    1. pykafka 中是有 C 代码的,你在 Mac 上创建的 venv 放到 Linux 上是不能使用的,必须本地也在 Linux 中构建。
    2. pykafka 依赖一个 C 库,https://github.com/edenhill/librdkafka,所以你先要在 公司服务器上装好这个库。

    所以 Docker 真是最简单的办法了。
    raysmond
    raysmond
    Apr 2, 2020
    14
    用 wheel 打包所有依赖包
    pip wheel -r requirements.txt -w wheels
    tar czf delphinus-wheels.tar.gz wheels/
    bwangel
    bwangel
    Apr 2, 2020
    15
    另外,如果你的项目不复杂的话,还有一个建议就是用 Go 重写。

    在交叉编译好了以后,直接把可执行文件扔到公司服务器上运行,不需要任何的依赖。
    sakura1
    sakura1
    OP
    Apr 2, 2020
    16
    @bwangel 谢老哥了
    ipwx
    ipwx
    Apr 2, 2020
    17
    @sakura1 anaconda 可以打包环境啊,你搞一个离线 anaconda 安装包到 linux 上装好,然后导入环境不就得了。

    mac 你装个虚拟机,然后把环境弄好给打包呗。
    ClericPy
    ClericPy
    Apr 2, 2020
    18
    用 shiv 打包成 pyz, 但是还是离不开 Python 解释器, 这种问题问的挺多的, V2EX 上就不止回复 2 次了.....
    jingcoco
    jingcoco
    Apr 2, 2020 via Android
    19
    。。。我曾经试过让我的电脑代理服务器上网。。。。。
    lance86
    lance86
    Apr 2, 2020
    20
    回 5 楼,就算是你写 java,你也要在服务器上装 jre 不是? docker 的话有离线安装包,镜像可以导出成文件然后离线还原,就能运行了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1676 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 00:29 · PVG 08:29 · LAX 16:29 · JFK 19:29
    ♥ Do have faith in what you're doing.