INTERVIEW
WITH THE
ULTRABEAST

I, UltraBeast Blog!

Monday, January 16, 2012

How to Package the ENTIRE Python interpreter, (of a specific bit-version) and ALL its site-packages???

MONDAY 16/1

This use case,
  • when the intended users of your app already have some Python installed on their PC already (and are using Windows ;) ) and you don't want to screw with that
  • maybe they already have Python27, 64 bit, or Python24, 32bit... and you want to insist them to use Python26 32bit... due to some dependencies that you want to use for your app...
  • Your users don't have a C compiler on their PC, and you don't expect them to install one... so they can't do pip install by themselves...
  • The binary distribution of the site-package that you want to use ONLY works on a specific bit and version of Python interpreter...
  • You want your users to just 'copy the entire folder', run activate.bat, and then run your scripts... no need any fancy and tedious installation steps, unpackaging steps, whatever...
  • Basically you don't want your users to have to set up anything to run your tool, you just want them to copy in the whole folder with python.exe, activate.bat, all dependencies in site-packages, and your tool's scripts, and then they just run your tool and all imports work fine, this is what you want.

For completeness, there's also some other steps where you prepared this virtual-env by installing stuff from both .msi and sources. Detailed below...



--HOW TO USE a specific Python.exe (python2.6, 32bit, and a certain set of site-packages) -- virtualenv
first copy the whole virtual env folder to say c:\python26_virtual_env
next if you run this specific command line:
  • c:\python26_virtual_env\Scripts\python.exe
i.e. using the direct path to this specific interpreter, you will get the print sys.path to be beginning from this path...
Now, how do you install PyCrypto into the Lib\site-packages of this path? (you must activate the virtualenv first!)
And it's installing the compiled binaries from .msi (because you dun want to install a C compiler, and this is Windows.)


1
installing pyCrypto from .msi from command line into virtualenv
installing from .msi:     just MODIFY activate.bat, (modify the first lines, path to the specific python.exe to be appended to the PATH system env variable)
and use the MODIFIED activate.bat.
Then go into the command prompt, and run the .msi from the virtualenv! command line.



2
installing paramiko from sources (setup.py install)
CANNOT DO THIS --> running "setup.py install" from another folder i.e. c:\python26_virtual_env>Scripts\python.exe paramiko\paramiko\setup.py install
doing it this way will give u a lot of junk in the current folder, and it won't install correctly in the site-packages -__-"

Instead, you must navigate the command line, CD\ into the SAME FOLDER where the setup.py file is,
then only run python.exe with THE SPECIFIC pathto\python.exe setup.py install that you want to use!



3
contents of activate.bat (modified)

@echo off
REM set VIRTUAL_ENV=c:\latest-system-tests\python26_virtual_env
set VIRTUAL_ENV=c:\python26_virtual_env
REM set VIRTUAL_ENV=%CD%

if not defined PROMPT (
    set PROMPT=$P$G
)

if defined _OLD_VIRTUAL_PROMPT (
    set PROMPT=%_OLD_VIRTUAL_PROMPT%
)

if defined _OLD_VIRTUAL_PYTHONHOME (
     set PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME%
)

set _OLD_VIRTUAL_PROMPT=%PROMPT%
set PROMPT=(python26_virtual_env) %PROMPT%

if defined PYTHONHOME (
     set _OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%
     set PYTHONHOME=
)

if defined _OLD_VIRTUAL_PATH set PATH=%_OLD_VIRTUAL_PATH%; goto SKIPPATH

set _OLD_VIRTUAL_PATH=%PATH%

:SKIPPATH
set PATH=%VIRTUAL_ENV%\Scripts;%PATH%

:END



Now that you've modified the activate.bat, and installed all the dependencies you wanted into the subfolders, you can just copy this whole folder to other PCs ... so your 'packaging' task is done ;)



4
to confirm:
>> Scripts\python.exe
import Crypto
import paramiko

both should work well.



5 to further confirm:
  • copied the whole interpreter package from Winxp into Win7.
  • run activate bat.
  • able to import Crypto and import paramiko!
  • refer to the below for the proven screen output.

C:\python26_virtual_env>Scripts\activate.bat
(python26_virtual_env) C:\python26_virtual_env>python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Crypto
>>> import paramiko
>>>

No comments:

Post a Comment

Yes... milord?

Interview With The UltraBeast katakana

Blurb ©

Placeholder for some future cool feature :D