如何在打包时为 python 模块设置别名? [英] How do I alias a python module at packaging time?

查看:60
本文介绍了如何在打包时为 python 模块设置别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于历史原因和向后兼容性,我想以这组导入的方式打包一个模块(我们称之为 myapi):

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:

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

假设软件包是通过 pip install myapipip install --user myapi 以常规方式安装的.

Assuming the package was installed the regular way with pip install myapi or pip install --user myapi.

即,我想将myapi的完整模块结构别名为另一个名称oldname at Packaging,以便用户可以导入任何模块使用 oldname 或新名称(此处为 myapi)而不复制代码.在 Python 或 setuptools 中有什么东西可以做到这一点吗?

I.e., 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. Is there something in Python or setuptools that can accomplish that?

替代方案,对我不起作用:

我知道用户可以这样做:

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.

在某些系统上,最简单的方法是在运行 setup.py install 时创建一个符号链接:

On some systems, the easiest way would be to just create a symbolic link upon running setup.py install:

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

但那是hacky并且肯定会混淆Python的导入系统并导致其他问题(oldname.__name__pip show oldname等).它也不适用于轮子.我更喜欢内置方式,但对 Python 包装的了解并不像我所了解的那样深入.也许我可以在我的 setup.py(顺便说一句,使用 setuptools)中放入一些东西.有什么建议么?有没有更好的办法?

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). 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. Maybe there is a something I could put in my setup.py (which btw. uses 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.

这篇关于如何在打包时为 python 模块设置别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆