I'm working on a support library for a large Python project which heavily uses relative imports by appending various project directories to sys.path
.
Using The Hitchhiker's Guide to Packaging as a template I attempted to create a package structure which will allow me to do a local install, but can easily be changed to a global install later if desired.
One of the dependencies of my package is the pyasn1
package for the encoding and decoding of ASN.1 annotated objects. I have to include the pyasn1
library separately as the version supported by the CentOS 6.3 default repositories is one major version back and has known bugs that will break my custom package.
The top-level of the library structure is as follows:
MyLibrary/
setup.py
setup.cfg
LICENSE.txt
README.txt
MyCustomPackage/
pyasn1-0.1.6/
In my setup configuration file I define the install directory for my library to be a local directory called .lib
. This is desirable as it allows me to do absolute imports by running the command import site; site.addsitedir("MyLibrary/.lib")
in the project's main application without requiring our engineers to pass command line arguments to the 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 into with doing the installation this way is that when my package tries to import pyasn1
it imports the global version and ignores the locally installed version.
As a possible workaround I have tried installing the pyasn1
package under a different name than the global package (eg pyasn1_0_1_6
) by doing package_dir = {'pyasn1_0_1_6':'pyasn1-0.1.6/pyasn1'}
. However, this fails since the imports used internally to the pyasn1
package do not use the pyasn1_0_1_6
name.
Is there some way to either a) force Python to import a locally installed package over a globally installed one or b) force a package to install under a different name?