Python 从存档导入模块 (xlwt)
比方说,Tight Ars & Co. 是一家拥有极其严格的安全政策的公司,假设我在这家公司工作。假设他们有一项任务需要使用 python 脚本写入 Excel 文件,我发现了一个名为 xlwt 的令人难以置信的精彩库。现在我的脚本能够写入 Excel 文件,一切都很棒,阳光明媚,我发布了代码,突然有人问我这个 thingamajig setup.py 是什么,为什么我们应该运行它?等等,我们甚至不会运行它,我们希望环境从第三方代码等中清除,因为我不知道有任何魔法或呜呜有什么方法可以打包依赖库并将它们导入我的脚本?
Let's say Tight Ars & Co. is a company with incredibly tight security policies, and lets assume I work for this company. Assume they've one task that requires a python script to write to excel files, and I find this incredibly wonderful library called xlwt. Now my script is able to write to excel files, everything is wonderful and the sun is shining, I release the code, and suddenly I'm asked what is this thingamajig setup.py, why should we run it? wait, we'll not even run it, we want the environment to be clean from third party code etc etc, since I'm unaware of any wizardry or voo doo is there any way I can package the dependent libraries and import them in my script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
setup.py
通常对任何纯 Python 包执行的操作都是将文件复制到标准位置并将.py
文件编译为.pyc
。我无法想象为什么你的雇主会认为这是(讨厌的)第三方软件,但是包的来源是好的,你的IDE是好的,Python本身是好的,等等...选项:
(1)复制从源代码分发版的
xlwt
目录到sys.path
中列出的某个位置(2) 创建一个 ZIP 文件
xlwt.zip
,其中包含xlwt
目录并将其复制到同上。(3) 与(2)相同,但首先将.py文件编译为.pyc。
如果有人指出上述涉及容易出错的手动步骤,您可以:
(a) 编写一个脚本来执行此操作
或
(b) 复制
setup.py
,更改其名称,假装您编写了自己用,自己用,...All
setup.py
typically does with any pure-Python package is copy files into a standard place and compile the.py
files to.pyc
. I can't imagine why your employer would regard that as (nasty) third-party software, but the source of the package is OK, your IDE is OK, Python itself is OK, etc ...Options:
(1) Copy the
xlwt
directory from a source distribution to somewhere that's listed insys.path
(2) Make a ZIP file
xlwt.zip
containing the contents of thexlwt
directory and copy it to ditto.(3) As (2) but compile the .py files to .pyc first.
If somebody points out that the above involves error-prone manual steps, you can:
(a) write a script to do that
or
(b) copy
setup.py
, change its name, pretend that you wrote it yourself, use it, ...除非我误解了这个问题,否则您应该能够获取源存档并将“xlwt”目录复制到与脚本相同的目录,并且应该可以从本地目录导入。
Unless I am misunderstanding the question you should be able to obtain the source archive and simply copy the "xlwt" directory to the same directory as your script and it should be importable from the local directory.