How to install external libraries with Portable Py

2019-01-15 20:15发布

问题:

I can't install Python on my machine due to administrator privileges, but I did download/open Portable Python successfully. I am on a Windows 7 64-bit machine. How would I be able to use the external libraries from before, such as Numpy or Gmpy?

回答1:

easy_install is trying to install from source. gmpy and gmpy2 are C extensions and require the presence of a compatible C compiler and other libraries (GMP; and MPFR and MPC for gmpy2). Installing from source is frequently difficult on Windows. The installers include a precompiled version of the extension.

One option is to extract the compiled binary from the installer. 7-Zip is able to open the installer file and you can extract the binary. In a standard Python installation, the extracted binary just needs to be placed in the site-packages directory. If necessary, you can do the extraction on another system and copy the file.

You can also use the zipfile module to extract the compiled extension. Here is an example. You will need to modify the exact file locations to reflect your system.

>>> import zipfile
>>> f=zipfile.ZipFile('gmpy2-2.0.0.win-amd64-py3.3.exe','r')
>>> f.namelist()
['PLATLIB/gmpy2-2.0.0-py3.3.egg-info', 'PLATLIB/gmpy2.pyd']
>>> f.extract('PLATLIB/gmpy2.pyd')
'C:\\Python33\\PLATLIB\\gmpy2.pyd'


回答2:

Do the following:

  • Find an already compiled version of the desired package - Christoph Gohlke mantains an excelent collection here - download it and put it in a folder (say C:\temp).
  • In your Portable Python installation find the Scripts folder (usually under the Apps folder) and open a command prompt there.
  • From the Scripts folder type the command easy_install C:\temp\numpy-MKL-1.8.0.win32-py2.7.exe (change the exe file name for whatever is the name of your installer).

Test if the installation succeeded:

>>> import numpy
>>> print numpy.__version__
1.8.0


回答3:

please refer to https://groups.google.com/forum/?fromgroups#!topic/portablepython/BVQOHFNXilU

According to that, for most packages, you should be able to conduct an easy install into your Portable Python root folder and then import it as normal in your python script.



回答4:

For most external packages, I have been able to import them as follows:

  1. Extract the package source from the appropriate .whl file on PyPI into a custom 'include' folder on the disk/stick with Portable Python installed.
    1. In my case, this is F:\py\include -- whatever you choose, be sure the path to this folder has no spaces or special characters in it, or else Portable Python won't parse it correctly when added to PYTHONPATH.
    2. Wheels are just ZIP files; rename the .whl to .zip and Windows Explorer will open it right up.
    3. Usually you want to retrieve the subfolder within the .whl whose name is just the package name you're interested in: numpy, sympy, etc.
  2. Add the path to this include folder to the PYTHONPATH environment variable:
    1. In Windows Explorer, right-click on 'Computer' and select 'Properties'
    2. This is a bit version-dependent, but select something like 'Advanced system settings' (Win7) or the 'Performance' or 'Advanced' tab (WinXP).
    3. Click 'Environment variables'
    4. Under 'System variables', look for PYTHONPATH. If it's there, select it and click 'Edit.' If not, click 'New'.
    5. If you're adding it new, type PYTHONPATH as the 'Variable name.' Either way, Add the path to your custom include folder into 'Variable value.' If other paths are already in there, separate your path from any prior one with a semicolon. DO NOT put a space between the semicolon and your new path! Portable Python apparently interprets entries with a leading space as being relative paths, with the reference folder being the Portable Python installation folder.
    6. Click 'OK' as needed to save the settings and clear the dialogs.

In some cases where a "formal" installation process is required, this hasn't always worked. I think sympy was one case where I had to compile it before transferring it to F:\py\include. Also, I had a particularly rough time with h5py, but eventually got it to work by installing it into a 'normal' version of Python 2.7 and copying the resultant h5py folder over to F:\py\include.