用于导入具有相同名称但版本不同的包的Python实用程序。

pylibimport的Python项目详细描述


用于导入具有相同名称但版本不同的包的Python实用程序。在

版本导入器属性:
  • 下载\u dir-目录以查找导入名称和文件
  • install_dir-按名称和版本保存/安装模块的目录
  • 安装依赖项-如果为True.whl文件将把依赖项安装到install\u目录
  • 重置模块-重置系统模块每次导入后(仍将添加自定义的“0”\u“0”)。在
  • This helps prevent dependencies from being saved to sys.modules
  • modules-导入的模块的名称和版本的字典。在
导入选项:
  • 模块导入-导入normal.py文件。在
  • Zip导入-导入.Zip文件或。焦油gzsdist文件。在
  • 车轮导入-导入.whl文件。这实际上是将它们安装到目标目录,然后导入它。
    • 这需要pip。参见“Pip选项”。在

Pip选项

这个库现在提供了多种使用pip的方法。默认方法是导入pip的main函数和 运行它(pip_main)。如果您在可执行文件中使用此文件,建议使用pip_bin。在

主管道

pip_main选项只使用pip中的main函数。这是此库的默认值。 有时pip的main函数会打开一个单独的进程,这可能会导致可执行文件出现问题。我相信皮普 在尝试安装带有设置.py文件而不是whl文件。在

importpylibimportpylibimport.VersionImporter.pip=pylibimport.pip_main

皮普程序

pip_proc选项使用LightProcess创建一个单独的进程并运行pip_main。在

^{pr2}$

皮普宾

我最初发现运行二进制文件是安装软件包最可靠的方法。不幸的是 可执行文件这似乎在您的计算机上有效,因为pip.exe文件路径匹配。在其他机器上这可能会 不工作。在

importpylibimportpylibimport.VersionImporter.pip=pylibimport.pip_bin

这可以扩展为在shell模式下运行子进程。在

importpylibimportdefpip_shell(*args,**kwargs):returnpylibimport.pip_bin(*args,shell=True,**kwargs)pylibimport.VersionImporter.pip=pip_shell

示例

简单的导入示例。在

importpylibimportimporter=pylibimport.VersionImporter(install_dir='./sub/target_dir')custom=importer.import_module('./sub/custom.py')print(custom.run_custom())# 'hello custom1'# Remove the saved module from the install_dirimporter.delete_installed(custom)# Give a version number to the modulecustom=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)importcustom_1_0_0print(custom_1_0_0.run_custom())

多个文件

此库还可以跨多个文件工作。在

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

准备_模块.py使用pylibimport将版本为的模块导入到系统模块 允许从其他文件导入。在

# multi_modules.pyimportprep_modules# Uses pylibimport for custom_1_0_0 and custom_0_0_0importcustom_1_0_0print(custom_1_0_0.run_custom())importcustom_0_0_0print(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?

子包装

现在您也可以导入子包了。在

importpylibimportimporter=pylibimport.VersionImporter()module=importer.import_module('requests','2.23.0','requests.auth')asserthasattr(module,'HTTPBasicAuth')

问题

大多数导入都没有任何帮助。只需将路径添加到系统路径还有进口。 您还可以通过将zip文件添加到路径并导入它来轻松导入zip文件。 whl文件可以重命名为.zip并像zip文件一样导入。在

最大的问题是C扩展。C扩展要求您在导入之前从.zip中提取.pyd。 最初我只打算自动提取.pyd文件。提取整个zip文件或 安装.whl文件。这还允许您提取/安装一次并将其保留在您的系统中,从而使以后的导入更容易。在

这种方法还允许您通过版本号来分隔内容,这可能很有用。在

纽比

不要尝试对要安装Numpy的Numpy或.whl文件执行此操作。Numpy是针对其他库编译的 把道路弄得一团糟。我没有一个正常的安装成功导入numy。 我用Python3.8-64位在Windows10上尝试了很多不同的方法。 我想我甚至试过在https://www.lfd.uci.edu/~gohlke/pythonlibs/找到的纽比。在

我的最终解决方案是使用pip将库安装到某个位置并指向该位置。在

未来

我想进一步了解python的导入系统。我想研究一下zipimport 与查找器和加载器一起工作。不幸的是,我知道我自己,这可能不会发生。 最后我认为Pythonn将最终添加版本导入支持,否则将由其他pipenv完成 图书馆什么的。{qm}可能与将来的Python(qml)类似 版本是可选的。那只是我的猜测。在

我的长期目标是让它成为虚拟环境的替代品。现在我的电脑上有50个。 我开发的每个库都有一个。有了这个,我还安装了许多相同的库。 我的开发环境充满了重复的库。这个图书馆可以解决这个问题。我没有 开发这个功能需要很多时间,所以需要很长时间。在

列出并下载版本

这个库现在可以从一个简单的pypi索引中找到版本。在

>>> python -m pylibimport.get_versions requests

您也可以用类似的方式下载包

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java这算是人工智能吗?   java@OnDelete(级联)不处理双向映射(hibernate)   java我怎样才能在基元类型(int)上修复这个等式   java如何初始化PrintWriter数组?   arraylist如何使用java添加敌人   java如何处理2019年的背景地理围栏?   java是否有一个XPath表达式可用于在CDATA区域中导航XML?   Kotlin(Android Studio)中的java选项卡式活动   安卓 java。lang.IllegalArgumentException:provider=网络   广播控制流和主流的java排序   java Apache Nifi无法启动Nifi实例   java我正在尝试将内容作为字符串放入ListView   java阻止mozilla会话还原   java在春季选择引用哪个bean?