如何在我的 python 脚本中包含第 3 方模块?
我开始使用 Python 来自动执行工作中的重复性任务,并且经常需要将对第三方模块的引用集成到我的脚本中。如何将这些文件直接包含在我的脚本中?我知道有一些方法可以将库集中安装在我的 python 安装文件夹 (C:\Python27) 中。许多第三方模块提供了一个 EXE 安装程序,可以自动执行此操作。但是,我担心这将如何影响我的脚本的可移植性。例如,如果我将脚本发送给其他人,我不想也向他们发送他们需要下载和安装的所有单独模块的列表。
更新
我对 Visual Studio 中的 C# 项目有更多的经验。在 Visual Studio 项目中,如果我想使用第 3 方 DLL,我只需将该 DLL 包含在我的解决方案中的 Lib 文件夹中,并从我的项目中引用该 DLL。我不费心将该库加载到 GAC 中,这对我来说似乎相当于安装 python 包的 .NET。
是否有某种方法可以将第三方库包含在我的项目文件夹中并使用相对路径引用它们?假设我有以下文件结构。
\My Script.py
\lib\3rdPartyLib\3rdPartyLib.py
我可以从 MyScript.py 导入 3rdPartyLib 吗?
import 3rdPartyLib from \lib\3rdPartyLib\ ??????
为什么我不想这样做?
I've started using Python to automate repetitive tasks I have at work, and often need to integrate references to third party modules into my scripts. How can I include these files with my scripts directly? I know there are ways to install the libraries centrally in my python installation folder (C:\Python27). Many third party modules provide an EXE installer that will do this automatically. However, I'm concerned how that will affect the portability of my scripts. For example if I send my script to someone else I don't want to also have to send them a list of all the separate modules they need to go download and install.
Update
I have a lot more experience with C# project in Visual Studio. In a visual studio project if I want to use a 3rd party DLL I just include that DLL in my solution in a Lib folder and reference that DLL from my project. I don't bother loading that library into the GAC, which to me seems like the .NET equivalent of installing a python package.
Isn't there some way I can just include 3rd party libraries in my project folder and reference them using a relative path? Say I had the following file structure.
\My Script.py
\lib\3rdPartyLib\3rdPartyLib.py
Can I import 3rdPartyLib from MyScript.py?
import 3rdPartyLib from \lib\3rdPartyLib\ ??????
Why would I not want to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:
是的,您可以将第三方模块复制到您的应用程序文件夹中。虽然使用站点包文件夹可以集中进行每台计算机的更新,但如果您不经常更改库,那么将它们包含在您所描述的应用程序文件夹中可能会更容易。
如果可能,将应用程序文件夹放在网络驱动器上将简化网络范围内的更新。
您需要使用 py2exe 或 pyinstaller。
它将构建一个充满依赖项和 Windows .exe 的文件夹。您还可以包含图标和其他元数据。将其封装在免费安装程序中,例如 innoSetup,您将拥有一个外观专业的应用程序。
Update:
Yes, you can copy third party modules to your application folder. While using the site-packages folder allows for centralized per-machine updates, if you change the libraries infrequently it might be easier for you to just include them in your application folder as you describe.
If possible, putting your application folders on a network drive will simplify updates network wide.
You'll want to use py2exe or pyinstaller.
It will build a folder full of dependencies and a Windows .exe. You can include icons and other metadata as well. Wrap it in a free installer such as innoSetup and you'll have a profesional looking application on your hands.
那么,您可以使用 py2exe 或 pyinstaller,或者您可以在脚本开始时引用您的模块,将它们打包到存档中,并使您的程序可以在不同的操作系统上移植!
但我发现你只使用 Windows !是什么让便携性的理念在您的脑海中挥之不去......
Well you can use a py2exe or pyinstaller or you can reference your modules on start of scripts pack them into a archive and have your program portable over different os-es !
But well as much heh I see you only use windows ! What make idea of portability last in your back thought ...