简体   繁体   English

如何从PyPI导入名称中带有连字符的软件包?

[英]How import package from PyPI with hyphen in name?

There is a package in PyPI called neat-python (yes, with a hyphen). PyPI中有一个名为neat-python的软件包(是的,带有连字符)。 I can install it just fine but can't import it into Python. 我可以很好地安装它,但不能将其导入Python。 I've tried underscores, parentheses, and making the name a string but of course the import statement doesn't allow them. 我已经尝试过使用下划线,括号并将名称设置为字符串,但是import语句当然不允许使用它们。 Does PyPI actually accept packages with illegal Python names or is there a solution I'm overlooking? PyPI是否实际上接受带有非法Python名称的软件包,还是我忽略了一个解决方案?

hyphen is not allowed in import syntax. 导入语法中不允许使用连字符。 In the case of 'neat-python' the package is simply installed as 'neat': 如果是“整洁的python”,则该软件包将简单地安装为“整洁的”:

import neat

you can check this yourself by looking in your site-packages directory (for me, that is /usr/local/lib/python3.7/site-packages ). 您可以通过查看site-packages目录(对我来说就是/usr/local/lib/python3.7/site-packages )自己检查一下。

Edit: and yes, this is allowed for PyPI packages, and it can be annoying. 编辑:是的,PyPI包允许这样做,这可能很烦人。 Usually the actual package name will be some very similar variant of the name used to install from PyPI. 通常,实际的软件包名称将与从PyPI安装的名称非常相似。

Starting in python3.x you can use importlib for some generic module that actually installs with a hyphen in the name. 从python3.x开始,您可以将importlib用于某些实际安装的通用模块,名称中带有连字符。 I will use neat-python as an example even though I have been informed that it actually installs as neat : 我将以neat-python为例,即使我被告知它实际上以neat安装:

--myscript.py--

import importlib
neat = importlib.import_module("neat-python")
# to then call "mymodule" in neat
neat.mymodule(someobject)

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

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