Skip to main content

Python utility for importing packages with the same name, but different version.

Project description

Python utility for importing packages with the same name, but different version.

VersionImporter Attributes:
  • import_dir - Directory to find import names and files

  • target_dir - Directory to save/install modules by names and version

  • install_dependencies - If True .whl files will install dependencies to the target_dir

  • reset_modules - Reset sys.modules after every import (Will still add custom_0_0_0).

  • This helps prevent dependencies from being saved to sys.modules

  • modules - dictionary of name and version modules that have been imported.

Import options:
  • Module import - import normal .py files.

  • Zip import - import .zip files or .tar.gz sdist files.

  • Wheel import - import .whl files. This really installs them to a target dir then imports it.

Example

Simple import example.

import pylibimport

importer = pylibimport.VersionImporter(target_dir='./sub/target_dir')

custom = importer.import_module('./sub/custom.py')
print(custom.run_custom())
# 'hello custom1'

# Remove the saved module from the target_dir
importer.delete_module(custom)

# Give a version number to the module
custom = importer.import_module('./sub/custom.py', '1.0.0')
print(custom.run_custom())
# 'hello custom1'

# pylibimport always adds an import_name to sys.modules (custom 1.0.0 becomes custom_1_0_0)
import custom_1_0_0
print(custom_1_0_0.run_custom())

Multiple Files

This library also works across multiple files.

# prep_modules.py
import pylibimport

importer = pylibimport.VersionImporter(target_dir='./sub/target_dir')
importer.import_module('./sub/custom.py', '1.0.0')  # Give a version number to the module
importer.import_module('./sub/import_dir/custom.py', '0.0.0')

The prep_modules.py uses pylibimport to import modules with version into sys.modules allowing imports from other files.

# multi_modules.py
import prep_modules  # Uses pylibimport for custom_1_0_0 and custom_0_0_0

import custom_1_0_0
print(custom_1_0_0.run_custom())

import custom_0_0_0
print(custom_0_0_0.run_custom())

# This actually works! ... code completion is not going to happen.
# Python has a bunch of import hooks (ZipImporter) which could make this better?

Subpackages

Now you can import sub packages as well.

import pylibimport

importer = pylibimport.VersionImporter()

module = importer.import_module('requests', '2.23.0', 'auth')
assert hasattr(module, 'HTTPBasicAuth')

Problems

Most importing works without any help. You just add the path to sys.path and import. You can also easily import a zip file by adding the zip file to the path and importing it. A .whl file could just be renamed .zip and import like the zip file.

The biggest problem is C extensions. C extensions require you to extract the .pyd from the .zip before importing. Originally I was going to automate only extracting the .pyd files. It is much easier to extract the entire zip file or install the .whl file. This also lets you extract/install once and leave it on your system, making imports easier later.

This approach also lets you separate things by version number which may be useful.

Numpy

Don’t try this with Numpy or .whl files that want to install Numpy. Numpy is compiled against other libraries and the pathing gets messed up. I have not had any success importing numpy without a regular install. I tried a lot of different ways on Windows 10 with Python 3.8 - 64 Bit. I think I even tried Numpy found at https://www.lfd.uci.edu/~gohlke/pythonlibs/.

My ultimate solution is to use pip to install the library to a location and point to that location.

Future

I would like to learn more about python’s import system. I would like to research how zipimport works with the finder and loader. Unfortunately, I know myself, and it’s probably not going to happen. In the end I think Python will eventually add version import support anyway or this will be done by other pipenv library or something. Future Python (4.0) syntax will probably be like qml import custom 1.0.0 where the version is optional. That is just my guess.

My very long term goal is to make this a virtual environment replacemnt. Right now I have 50 venv’s on my computer. I have one for every library that I develop. With this I also have a bunch of the same libraries installed. My development environment is filled with duplicate libraries. This library can solve this problem. I do not have a lot of time to develop this functionality, so it will take me a long time.

List and Download Versions

This library can now find versions from a simple pypi index.

>>> python -m pylibimport.get_versions requests

You can also download a package in a similar way

>>> python -m pylibimport.download requests -v 2.23.0
requests-2.23.0-py2.py3-none-any.whl saved!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylibimport-1.0.2.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pylibimport-1.0.2-py3-none-any.whl (46.7 kB view details)

Uploaded Python 3

File details

Details for the file pylibimport-1.0.2.tar.gz.

File metadata

  • Download URL: pylibimport-1.0.2.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.8

File hashes

Hashes for pylibimport-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f1159da79449f89a705b36c41b5e5b6db084c4f6498fb5430a0018d433ba307a
MD5 befb7c22ad5f2ea1be0340ffe3df16ba
BLAKE2b-256 368ab00a0d7d2f8a789a23a0d14e0c7c98e5b57c72a6d8a0ffe014cc60b8bcfa

See more details on using hashes here.

File details

Details for the file pylibimport-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pylibimport-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 46.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.8

File hashes

Hashes for pylibimport-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d3ed8b0ceef5130537894a2586222f590b2e3ab1a4a79d9f0e6d34c731f3851f
MD5 c9545929894380045edcfae6537724c4
BLAKE2b-256 714dfba8d95304426d3443129c11eaf43f199c36d7f2ca3b2b4393ff19b4b294

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page