如何使用我的 python 脚本打包模块
我刚刚编写了一个 python 脚本来修复一些数据库问题,它使用 psycopg2 模块。由于某种原因,需要(在服务器上)运行它的人声称他们无法在他们的服务器计算机上安装 psycopg2...有没有一种方法可以让我将该模块打包到我的脚本中,这样他们就不会必须安装psycopg2吗?类似于在Java中将lib文件夹添加到类路径中吗?
提前谢谢, 安德烈
I just wrote a python script to fix some database issues and it uses the psycopg2 module. For some reason the person that needs to run it (on the server) is claiming that they can't install psycopg2 on their server machine... is there a way that I can package that module within my script such that they don't have to have psycopg2 installed? Something similar to adding a lib folder to the classpath in Java?
Thx in advance,
Andre
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个目录,将脚本放入其中。
cd 进入该目录,然后运行:
这应该会在同一目录中生成 python 模块的副本。
把它全部拉上拉链然后把整个东西寄出去。
Make a directory, put your script into it.
cd into that directory, then run:
That should result in a copy of the python module in the same directory.
Zip it all up and send the whole thing off.
至于无法安装psycopg2,我确信它与有关谁可以在数据库服务器上拥有root访问权限的安全策略有关。如果您找不到具有必要权限的人,有两种方法可以给这只特定的猫换皮:
1)您可以使用 py2exe 构建一个 exe,这样您就知道脚本的所有依赖项都在那里并且可用。此外,这还可以让你避免处理任何 python 不兼容问题。您没有提及您的 python 版本(2.5、2.6、2.7、3.1?),也没有提及服务器上的版本。
2) 或者您可以将 psycopg2 模块放入其自己的目录 (c:\mypymods) 中,并在导入之前将路径添加到 sys.path 中。
As to be unable to install psycopg2, I sure it relates to security policies about who can have root access on database servers. If you can't find someone with the necessary permissions, there are two methods to skin this particular cat:
1) You could build an exe using py2exe, that way you know that all of the dependencies for your scripts are there and available. Also this will allow to you escape dealing with any python incompatibilities. You made no mention of your python version (2.5, 2.6, 2.7, 3.1?) nor the version on the server.
2) Or you could place the psycopg2 module into its own directory (c:\mypymods) and add the path to sys.path before importing.