简体   繁体   中英

How do I include 3rd party modules with my python scripts?

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?

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.

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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM