Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How import package from PyPI with hyphen in name?

Tags:

python

pypi

There is a package in PyPI called neat-python (yes, with a hyphen). I can install it just fine but can't import it into Python. I've tried underscores, parentheses, and making the name a string but of course the import statement doesn't allow them. Does PyPI actually accept packages with illegal Python names or is there a solution I'm overlooking?

like image 409
gblauer Avatar asked Jul 08 '19 16:07

gblauer


People also ask

Can Python package name contain hyphen?

Python packages and modules can not use hyphens, only underscores. This section of PEP-8 gives us guidance: Package and Module Names: Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.

How do you handle a hyphen in Python?

You can't have hyphens in variable names in python. That's the subtraction operator. Consider using an underscore instead: education_numType = ...

What is __ import __ in Python?

__import__() . This means all semantics of the function are derived from importlib. __import__() . The most important difference between these two functions is that import_module() returns the specified package or module (e.g. pkg. mod ), while __import__() returns the top-level package or module (e.g. pkg ).

Can a Python package name contain?

So, yes, you can have a number in both a project name and a module name, and the project name can even begin with one!


1 Answers

hyphen is not allowed in import syntax. In the case of 'neat-python' the package is simply installed as 'neat':

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).

Edit: and yes, this is allowed for PyPI packages, and it can be annoying. Usually the actual package name will be some very similar variant of the name used to install from PyPI.

like image 147
Z4-tier Avatar answered Oct 03 '22 13:10

Z4-tier