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

2020-04-02 14:28:07 +08:00
 sakura1

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

2605 次点击
所在节点    Python
20 条回复
bwangel
2020-04-02 14:32:41 +08:00
用 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
2020-04-02 14:32:44 +08:00
anaconda
dapang1221
2020-04-02 14:34:50 +08:00
venv 啊
sakura1
2020-04-02 14:36:15 +08:00
@bwangel 以后等公司服务器里有 docker 再试试把,docker 没有 docker
sakura1
2020-04-02 14:37:24 +08:00
@ipwx 兄弟,没网啊,我想向 java 一样,在开发环境上把依赖啥的打包好,然后到服务器直接执行。
wangyzj
2020-04-02 14:37:55 +08:00
pyinstaller
wuwukai007
2020-04-02 14:40:22 +08:00
下载 whl 的包离线安装
ShuoHui
2020-04-02 14:40:22 +08:00
😲我前几天问过差不多的问题
nifury
2020-04-02 14:40:54 +08:00
https://stackoverflow.com/questions/11091623/how-to-install-packages-offline
第二个回复可以只下载不安装,然后再转移到测试机上
或者直接下载 wheel 文件安装?
sakura1
2020-04-02 14:41:02 +08:00
venv 啥的好像是 python3 才有的吧,服务器里就一个 linux 自带的 python2,我继续撸 java 吧
Trim21
2020-04-02 14:41:05 +08:00
找台有外网的 linux 服务器或者虚拟机包 exe 比较省劲,又要跨平台又要离线安装的需求太罕见了
deljuven
2020-04-02 14:44:12 +08:00
容器镜像或者虚拟机镜像,venv 也可以,但是如果跨平台的话就有问题
bwangel
2020-04-02 14:45:16 +08:00
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
2020-04-02 14:48:12 +08:00
用 wheel 打包所有依赖包
pip wheel -r requirements.txt -w wheels
tar czf delphinus-wheels.tar.gz wheels/
bwangel
2020-04-02 14:48:44 +08:00
另外,如果你的项目不复杂的话,还有一个建议就是用 Go 重写。

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

mac 你装个虚拟机,然后把环境弄好给打包呗。
ClericPy
2020-04-02 15:14:43 +08:00
用 shiv 打包成 pyz, 但是还是离不开 Python 解释器, 这种问题问的挺多的, V2EX 上就不止回复 2 次了.....
jingcoco
2020-04-02 19:54:47 +08:00
。。。我曾经试过让我的电脑代理服务器上网。。。。。
lance86
2020-04-02 20:33:40 +08:00
回 5 楼,就算是你写 java,你也要在服务器上装 jre 不是? docker 的话有离线安装包,镜像可以导出成文件然后离线还原,就能运行了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/658650

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX