python 2.6 - import local package over global package -


i'm working on support library large python project heavily uses relative imports appending various project directories sys.path.

using the hitchhiker's guide packaging template attempted create package structure allow me local install, can changed global install later if desired.

one of dependencies of package pyasn1 package encoding , decoding of asn.1 annotated objects. have include pyasn1 library separately version supported centos 6.3 default repositories 1 major version , has known bugs break custom package.

the top-level of library structure follows:

mylibrary/   setup.py   setup.cfg   license.txt   readme.txt   mycustompackage/   pyasn1-0.1.6/ 

in setup configuration file define install directory library local directory called .lib. desirable allows me absolute imports running command import site; site.addsitedir("mylibrary/.lib") in project's main application without requiring our engineers pass command line arguments setup script.

setup.cfg

[install] install-lib=.lib 

setup.py

setup(                                                                              name='mylibrary',    version='0.1a',    package_dir = {'pyasn1': 'pyasn1-0.1.6/pyasn1'},       packages=[              'mycustompackage',                       'pyasn1',              'pyasn1.codec',              'pyasn1.compat','               pyasn1.codec.ber',              'pyasn1.codec.cer',              'pyasn1.codec.der',              'pyasn1.type'             ],    license='',    long_description=open('readme.txt').read(),    data_files = [] ) 

the problem i've run doing installation way when package tries import pyasn1 imports global version , ignores locally installed version.

as possible workaround have tried installing pyasn1 package under different name global package (eg pyasn1_0_1_6) doing package_dir = {'pyasn1_0_1_6':'pyasn1-0.1.6/pyasn1'}. however, fails since imports used internally pyasn1 package not use pyasn1_0_1_6 name.

is there way either a) force python import locally installed package on globally installed 1 or b) force package install under different name?

use virtualenv ensure application runs in known configuration independent os version of libraries.

edit: quick (unix) solution setting pythonpath environment variable, works path python modules (module loaded first path in found, append directory @ beginning of pythonpath). anwyay, i recommend proceed virtualenv, since engineered handling situations 1 facing.

rationale

the process automatable if write setuptools script specifying dependencies install_requires. complete example, refer this 1 wrote

setup

note can insert steps below in setup.sh shell script.

first create virtualenv , enter it:

$ virtualenv $name $ cd $name 

activate it:

$ source bin/activate 

now cd project directory , run installer script:

$ cd $my_project_dir $ python ./setup.py --prefix $path_to_virtualenv 

note --prefix $path_to_virtualenv, used tell script install in virtualenv instead of system-wide. call after activating virtualenv. note depencies automatically downloaded , installed in virtualenv.

then done. when want leave virtualenv, issue:

$ deactivate 

on subsequent calls, need activate virtualenv (step 2), maybe using runawesomeproject.sh if want.

as noted on virtualenv website, should use virtualenv >= 1.9, previous versions did not download dependencies via https. if consider plain http sufficient, version should do.

you might try relocatable virtualenvs: setup , copy folder host. anyway, note this feature still experimental.


Comments