0

我想使用一个外部模块(psutil),它有一些 C 代码和 python 代码。在我的本地机器上,我使用 Mingw 安装了这个模块。现在,我想在其他机器上使用这个库(而不是在每台机器上安装它)。那么我可以将我编译的代码复制到一个公共位置,然后从其他机器上,我将它包含在其中sys.path并且应该能够使用它吗?

我一直在寻找一种正确的方法来使用库而不必在每台机器上安装它,到目前为止我发现使用bdist选项,但这会创建一个 zip 文件,我认为它需要在每台机器上解压。

有人可以帮助我了解应该遵循的最佳方法。我知道,我可以使用 Pyinstaller、Py2exe,但我的其他机器有 python,我只想使用这个模块,而不必到处安装它。

4

2 回答 2

1

I had success with this using eggs. You can create eggs of all your additional modules, put them in some directory, and then simply add that directory to your sys.path and then import the egg. Since you have a C extension there, you probably need to use the pkg_resources module.

This is neat in that it creates good separation and encapsulation of the other code.

A bit more info in this answer.

于 2013-09-05T10:14:23.503 回答
0

Use bdist with the correct options: From the docs:

The available formats for built distributions are:

Format      Description                  Notes
gztar       gzipped tar file (.tar.gz)   (1),(3)
ztar        compressed tar file (.tar.Z) (3)
tar         tar file (.tar)              (3)
zip         zip file (.zip)              (2),(4)
rpm         RPM                          (5)
pkgtool     Solaris pkgtool  
sdux        HP-UX swinstall  
wininst     self-extracting ZIP file for Windows (4)
msi         Microsoft Installer.

Notes:
1. default on Unix
2. default on Windows
3. requires external utilities: tar and possibly one of gzip, bzip2, or compress
4. requires either external zip utility or zipfile module (part of the standard Python library since Python 1.6)
5. requires external rpm utility, version 3.0.4 or better (use rpm --version to find out which version you have)
于 2013-09-05T10:20:02.183 回答