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

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

Related

How to package a Python 3 console application as a windows binary?

I am doing this practice project to implement a LISP interpreter in Python, using help from here. I wanted to create an exe file for the project, executing which would start a REPL.
I tried using py2exe and pyInstaller but an error is thrown when I execute the output binary, saying that this script cannot run.
Where did I go wrong with my approach and what alternative ways can I use?
Thank you.
It is hard to know for sure but have you checked that all of the required dependencies for your project are either in the same folder as the created executable or (at least) in your path?
The other alternative that I am aware of (and use) is cx_Freeze. This particular exe builder has cross platform support.
cx_Freeze will attempt to automatically find all dependent python modules and include them in the final build. I imagine that the other two options work in the same manner. Packages that cannot be automatically located and binary dependencies (eg dlls, sos) must be explicitly specified in the build configuration scripts.
One method I have for debugging for missing dependencies is to manually copy the suspected missing dependency into the same folder as the .exe to see if it fixes the issue. If it does then I will specify it in the build configuration script.
See https://cx-freeze.readthedocs.io/en/latest/distutils.html for cx_Freeze documentation, in particular section titled build_exe.
Here is a good example of a non-trival setup.py for cx_Freeze: http://www.pythonexample.com/code/cx_freeze-setup/

How can I distribute custom Python scripts, including dependencies and other resources?

My project contains:
My own custom Python files
Unique package-specific generated Python code
Resources (e.g. binaries)
Dependencies on 3rd party modules (e.g. numpy)
The generated Python code makes things tricky, and separates this use case from a typical Python package where everyone gets the same code. I may create several packages to be distributed to different clients. Each package will have different/unique generated Python code, but use identical versions of my custom Python scripts and 3rd party dependencies. For example I may make a "package builder" script, which generates the unique Python code and bundles the dependencies together, depending on the builder arguments.
I want to distribute my Python scripts, including the resources and dependencies. The receiver of this package cannot download the 3rd party dependencies using a requirements.txt and pip; all dependencies and binaries must be included in this package.
The way I envision the client using this package is that they simply unzip the archive I provide, set their PYTHONPATH to the unzipped directory, and invoke my custom Python file to start the process.
If I'm going about this the wrong way I'd appreciate suggestions.

Python compile all modules into a single python file

I am writing a program in python to be sent to other people, who are running the same python version, however these some 3rd party modules that need to be installed to use it.
Is there a way to compile into a .pyc (I only say pyc because its a python compiled file) that has the all the dependant modules inside it as well?
So they can run the programme without needing to install the modules separately?
Edit:
Sorry if it wasnt clear, but I am aware of things such as cx_freeze etc but what im trying to is just a single python file.
So they can just type "python myapp.py" and then it will run. No installation of anything. As if all the module codes are in my .py file.
If you are on python 2.3 or later and your dependencies are pure python:
If you don't want to go the setuptools or distutiles routes, you can provide a zip file with the pycs for your code and all of its dependencies. You will have to do a little work to make any complex pathing inside the zip file available (if the dependencies are just lying around at the root of the zip this is not necessary. Then just add the zip location to your path and it should work just as if the dependencies files has been installed.
If your dependencies include .pyds or other binary dependencies you'll probably have to fall back on distutils.
You can simply include .pyc files for the libraries required, but no - .pyc cannot work as a container for multiple files (unless you will collect all the source into one .py file and then compile it).
It sounds like what you're after is the ability for your end users to run one command, e.g. install my_custom_package_and_all_required_dependencies, and have it assemble everything it needs.
This is a perfect use case for distutils, with which you can make manifests for your own code that link out to external dependencies. If your 3rd party modules are available publicly in a standard format (they should be, and if they're not, it's pretty easy to package them yourself), then this approach has the benefit of allowing you to very easily change what versions of 3rd party libraries your code runs against (see this section of the above linked doc). If you're dead set on packaging others' code with your own, you can always include the required files in the .egg you create with distutils.
Two options:
build a package that will install the dependencies for them (I don't recommend this if the only dependencies are python packages that are installed with pip)
Use virtual environments. You use an existing python on their system but python modules are installed into the virtualenv.
or I suppose you could just punt, and create a shell script that installs them, and tell them to run it once before they run your stuff.

Include python library with program distribution

Is there a way to distribute a python library with an application, so that it can be run with out any installation? The app is primarily going to be used in a computer lab, where users do not have permission to install global libraries. Ideally, users would simply be able to unzip a folder and run the app. The following can be assumed:
The python interpreter is present
Linux operating system
The specific library I need is matplotlib, but I would like to find a generic solution. I've looked at programs like PyInstaller, but they create very large programs that are slow to start. They also include a python interpreter, which is unnecessary.
Firstly, p2exe is Windows only.
In principle you can put all of you libraries into the ZIP file so they get expanded in place with thie application. At most you may need to adjust the PYTHONPATH variable to point at the lib's location.
There is no technical difference between modules installed on the path and in the system's python installation.
Have you looked at cxfreeze?

Create Python EXE without MSVCP90.dll

I'm trying to use Py2Exe to take a python program (using wxPython) to a Windows exe but there is an error regarding MSVCP90.dll . After reading py2exe fails to generate an executable on StackOverflow, it says I need to download this file. However, it also says all target users must download this file. This will be a huge inconvenience for my users (mostly corporate clients who will not know how to download the file), so is there a way to package the with the program?
Thanks!
What is MSVCP90.dll
MSVCP90.dll is Multithreaded, dynamic Visual Studio 2008 C Runtime Library. Generally your application should package MSVCP90.dll unless you are sure that the target machine have the matching CRT. You can use any of the packaging software to package the necessary DLLs and your software and distribute it.
Purpose of MSVCP90.dll
You may be wondering why you need this weird dll? Well CRT is nothing new to python. All application that is based on C heavily relies on C library functions. All the implementations of the standard C Library functions like (malloc, strcpy ..) to name a few is implemented in these libraries. There are different kinds and the specific MSDN website have more details on it.
Distributing MSVCP90.dll
When distributing CRT, you should understand that depending upon what CRT you have used the version number which is suffixed with the name of the CRT varies. For example MSVCP90.DLL is the CRT from Visual Studio 2008. A single machine can contain multiple CRTs either in the system folder on in application installation path.
If you are planning to package your application, you need to re-verify, which CRT version your application uses. Packaging wrong CRT or using one can cause undesired and undefined effect. Generally speaking the CRT your Python Installation uses, should be the same CRT you should package.
Determining the correct MSVCRT
As there are different CRT builds with different versions, it is difficult to ascertain, which CRT should be packaged. If you have a running application (executable), you can use dependencywalker to determine the correct version. Right click on any of the DLLs and click on properties and it will show you the location from which this DLL is being picked.
Packaging your application.
You can try using PyInstaller to package your application. It would be a convenient way to get the DLL into the target machine.
You have to deliver your app with that dll in same folder as your exe file.

Categories

Resources