简体   繁体   English

如何在打包时为别名设置python模块?

[英]How do I alias a python module at packaging time?

For historic reasons and backwards compatibility I want to package a module (let's call it myapi ) in a way that this set of imports: 出于历史原因和向后兼容性,我想以这种导入方式打包模块(让我们称之为myapi ):

from myapi.utils import helpers
from myapi.api.rest import MyRest
import myapi

Import the same code (without duplication) as these below: 导入相同的代码(不重复)如下所示:

from oldname.utils import helpers
from oldname.api.rest import MyRest
import oldname

Assuming the package was installed the regular way with pip install myapi or pip install --user myapi . 假设使用pip install myapipip install --user myapi以常规方式pip install --user myapi

Ie, I want to alias the complete module structure of myapi under another name oldname at packaging , so the user can import any module using oldname or the new name (here myapi ) without duplicating the code. 也就是说,我想别名的完整模块结构myapi另一个名称oldname 在包装 ,所以使用用户可以导入任何模块oldname或新名称(这里myapi )不重复的代码。 Is there something in Python or setuptools that can accomplish that? Python或setuptools中有什么东西可以实现吗?

Alternatives, that don't work for me: 替代方案,对我不起作用:

I'm aware that the user could just do: 我知道用户可以这样做:

import myapi as oldname

My goal is to avoid confusion, because the package name changed at some point. 我的目标是避免混淆,因为包名称在某些时候发生了变化。 The users should be able to use both the old and the new name interchangeably, without having to know the name changed at all. 用户应该能够交替使用旧名称和新名称,而不必知道名称已更改。

On some systems, the easiest way would be to just create a symbolic link upon running setup.py install : 在某些系统上,最简单的方法是在运行setup.py install时创建一个符号链接:

ln -s /usr/local/lib/python2.7/dist-packages/myapi \
      /usr/local/lib/python2.7/dist-packages/oldname

But that is hacky and will most certainly confuse Python's import system and lead to other problems ( oldname.__name__ , pip show oldname and such). 但这很麻烦,并且肯定会混淆Python的导入系统并导致其他问题( oldname.__name__pip show oldname等)。 Also it won't work with wheels. 它也不适用于车轮。 I prefer a built-in way but am not that deep into Python packaging that I would know one. 我更喜欢内置的方式,但我不熟悉Python包装。 Maybe there is a something I could put in my setup.py (which btw. uses setuptools ). 也许有一些东西我可以放在我的setup.py (顺便说一句。使用setuptools )。 Any suggestions? 有什么建议么? Is there a better way? 有没有更好的办法?

What I ended up using is the following package strucutre: 我最终使用的是以下包结构:

$ tree
./
├── myapi/
│   ├── utils/
│   │   ├── ...
│   │   └── __init__.py
│   └── api/
│       ├── rest.py
│       ├── ...
│       └── __init__.py
├── oldname/
│   └── __init__.py
└── setup.py

Where these are the relevent parts of the files: 这些是文件的相关部分:

# ./setup.py
from setuptools import setup, find_packages

setup(
    # ...
    packages=find_packages('./'),
    package_dir={'': './'},
)
# ./oldname/__init__.py

import sys, myapi
from myapi import *

sys.modules['oldname'] = myapi

Seems to work, but no idea if there are any edge-cases or whether there is a better solution and/or more canonical solution. 似乎工作,但不知道是否有任何边缘情况或是否有更好的解决方案和/或更多的规范解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM