简体   繁体   中英

How to move OS X Python modules (site-packages) onto server location?

On my Mac OS X 10.9 Mavericks machine, I have installed Python 2.7.6, PySide 1.2.1 and mysql-python 1.2.3 (all of them installed using Homebrew and Pip). Since I edited the /etc/paths to begin with /usr/local/bin before installning anything, all of the modules have been installed in /usr/local/lib/python2.7/site-packages and 'which python' returns /usr/local/bin/python. Everything runs smoothly.

Is it possible to somehow copy these modules onto a server location, for other OS X machines to import (rather than install this locally on all other OS X machines)?

I strongly suggest using Virtualenv to manage your Pip packages.

It allows you to:

  • separate the site-packages used by various projects;
  • disregard the site-packages provided by the OS, so that you don't have a conflict when moving from an OS X workstation to a Linux server;
  • generate a requirements.txt file with the exact names and versions of all libraries installed in the Virtualenv of any given project (this file will be committed to your source repository)

and finally:

  • reinstall the entire Virtualenv to a different machine (even a different OS) with a simple command.

Granted, some of these features are provided by Pip, but Virtualenv puts them all into a failsafe environment.

So i have a couple of modules installed in a virtualenv, but how do I put them in a server location, and can they be read/imported without running a virtualenv on the other machines?

You mentioned some modules, such as mysql-python, which need to be compiled into a binary module. They cannot be ported between different machines.

Other modules, which are of Python files only, can indeed be copied over to the other machine's site-packages . But even for those, it's best to use a virtualenv, instead of putting random junk into a server's site-packages directory.

In the server location you should install a clean virtualenv (using the right version of Python, of course) and then install all the required modules from source, using a requirements.txt file:

$ mkdir ~/virtualenv
$ python2.x virtualenv.py ~/virtualenv/your_app
$ ~/virtualenv/your_app/bin/pip install -r requirements.txt

The virtualenv path is just a suggestion. You can produce the requirements file from your current machine:

$ pip freeze > requirements.txt

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