20

I need to run a Python script on a machine that doesn't have Python installed. I use Python as a part of a software package, and Python runs behind the curtain without the user's notice of it.

What I did was as follows.

  1. Copy python.exe, python26.dll, msvcr90.dll and Microsoft.VC90.CRT.manifest
  2. Zip all the directory in LIBs directory as the python26.zip
  3. Copy all the necessary dll/pyd files inside the DLL directory.

It seems to work, but when I change the python26.zip to the other name such as pythonlib.zip, it cannot find the Python library any more.

  • Question 1: What's the magic behind the python26.zip name? Python automatically finds a library inside a python26.zip, but not with different name?
  • Question 2: If I have python26.zip at the same directory where python.exe/python26.dll is, I don't need to add path sys.path.append (THE PATH TO python26.zip). Is it correct?

Python has built-in libraries, and sys is one of them. I thought that I could use sys.path to point to whatever Python library in the ZIP file I needed. But, surprisingly, if I use the library name as Python26.zip, it just worked. Why is this so?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • 2
    Beware of manually installing Python. You will probably get it to run just fine but installing any additional libraries is a major pain in the posterior. – ktdrv Apr 20 '10 at 21:26
  • 1
    can you just package your app using py2exe? (it will include a copy of the interpreter inside an exe) – Corey Goldberg Apr 20 '10 at 22:22
  • How would Python find the zip if the name were not hardcoded? But more importantly, why do you need to change it? – Peter Hansen Apr 20 '10 at 23:04
  • 1
    I'm trying to do the same, but I'm stuck on step 1. How do you copy Microsoft.VC90.CRT.manifest? – Don Apr 06 '11 at 16:57

6 Answers6

26

I have been using PortablePython for a year now, and I find it great as it is working on my locked-down work-notebook.

There is a Python 2.5.4, 2.6.1 and a 3.0.1 version.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
das_weezul
  • 6,082
  • 2
  • 28
  • 33
19

From Sylvain Pointeau's blog:

The procedure is actually very simple, just download the msi installer from http://www.python.org/getit/ and type the command:

C:\development\apps>msiexec /a python-3.3.2.msi /qb TARGETDIR=C:\development\apps\python33

His example uses msiexec (aka MSI Administrative Installer for you UniExtract people) to force an extract to TARGETDIR. You'll notice that there is an internal installer which you delete.

EDIT: Also you can make it silent as well, but doing this every time you want to use python seems dumb. Just extract to a tempdir and then cleanup when they uninstall it.

PS: I didn't see how old this was! :D

  • 1
    I had to do similar recently with 2.7.11. Also, you will have to install pip manually (using the `get-pip.py` script). – Erik Apr 27 '16 at 02:37
11

I looked into the Python interpreter source code, and I did some experiments. And I found that the Python interpreter prepend the "THE DIRECTORY OF PYTHONXXX.DLL + pythonXXX.zip" no matter what. XXX is the version of the Python interpreter.

As a result, if there is a python26.zip in the same directory as the python26.dll. I could use all of the Python library automatically.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
prosseek
  • 182,215
  • 215
  • 566
  • 871
6

Another option is installing WinPython. It uses an installer, but it doesn't require admin rights (tested on Windows 7). Unlike Portable Python, it even has a Python 3.3.5 version.

Zenadix
  • 15,291
  • 4
  • 26
  • 41
3

py2exe will allow you to compile your Python script into a Windows executable. It may or may not work better than PortablePython, but perhaps it could be a little cleaner with regard to the number of files you need to distribute for your "behind the curtain" program.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jay Taylor
  • 13,185
  • 11
  • 60
  • 85
3

Another option might be to consider PyInstaller which will create stand-alone Python applications cross-platform. From the home page:

PyInstaller is a program that converts (packages) Python programs into stand-alone executables, under Windows, Linux, and Mac OS X. [...] The main goal of PyInstaller is to be compatible with 3rd-party packages out-of-the-box. This means that, with PyInstaller, all the required tricks to make external packages work are already integrated within PyInstaller itself so that there is no user intervention required. You'll never be required to look for tricks in wikis and apply custom modification to your files or your setup scripts. As an example, libraries like PyQt and Matplotlib are fully supported, without having to handle plugins or external data files manually. Check our compatibility list of SupportedPackages.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ars
  • 120,335
  • 23
  • 147
  • 134
  • PyInstaller doesn't support Windows with Python2.6. It's a blocker for me and may be for others too. http://www.pyinstaller.org/wiki/Python26Win – blokeley Apr 21 '10 at 08:19