简体   繁体   English

如何更改python模块的名称或别名

[英]How to change the name or alias a python module

I am using a python app, via django, that requires Pillow, and I have installed it via pip.我正在通过 django 使用需要 Pillow 的 python 应用程序,并且我已经通过 pip 安装了它。 However, when the framework loads it complains that Pillow is not installed.但是,当框架加载时,它会抱怨没有安装 Pillow。 I can verify this when trying import pillow fails.我可以在尝试import pillow失败时验证这一点。

If I look at the directory that contains the python modules I see there is no "pillow", but there is "PIL".如果我查看包含 python 模块的目录,我会看到没有“枕头”,但有“PIL”。 I understand that pillow is a fork of PIL, and I can import PIL (PIL appears in a list of modules within python >>> help() >>> modules).我知道枕头是 PIL 的一个分支,我可以import PIL (PIL 出现在 python >>> help() >>> modules 中的模块列表中)。

Without touching the framework, is there a way to convince the app/django that PIL == pillow.在不触及框架的情况下,有没有办法说服应用程序/django PIL ==枕头。 I tried import PIL as pillow in a settings file, but that didn't solve the problem.我尝试在设置文件中将import PIL as pillow ,但这并没有解决问题。

Thanks谢谢

The answer is in your question:答案在你的问题中:

>>> import PIL as pillow
>>> pillow
<module 'PIL' from '/opt/anaconda3/lib/python3.7/site-packages/PIL/__init__.py'>

Note that you need to add the import line in the module you plan to use it.请注意,您需要在您计划使用它的模块中添加导入行。

Maybe if you put this in your startup code it would work.也许如果你把它放在你的启动代码中它会起作用。

import sys
import PIL
sys.modules["pillow"] = PIL

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

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