7

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?

gblauer
  • 147
  • 2
  • 6
  • Does this answer your question? [Is it ok to use dashes in Python files when trying to import them?](https://stackoverflow.com/questions/761519/is-it-ok-to-use-dashes-in-python-files-when-trying-to-import-them) – Georgy Aug 12 '20 at 14:40
  • I am wondering why the question does not contain the link to the discussed project. It is here: https://pypi.org/project/neat-python/ ; https://github.com/CodeReclaimers/neat-python ; https://neat-python.readthedocs.io/en/latest/ --- I have to say that even the documentation is missing the information how to use (import) the module in your Python code. – pabouk - Ukraine stay strong Apr 26 '21 at 14:51

2 Answers2

7

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.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42
  • Thanks. Very helpful. What's even crazier is that there is another package in PyPI called "neat" which is unrelated. I suppose it would import as "neat" also. – gblauer Jul 08 '19 at 16:44
  • 1
    In this particular case the importable name for both packages is `neat`; that means the packages cannot be installed into one virtual env. But it's not always the case — [the name of the distribution and its importable name could be completely different](https://stackoverflow.com/a/54599368/7976758). – phd Jul 08 '19 at 17:26
0

Starting in python3.x you can use importlib for some generic module that actually installs with a hyphen in the name. I will use neat-python as an example even though I have been informed that it actually installs as neat:

--myscript.py--

import importlib
neat = importlib.import_module("neat-python")
# to then call "mymodule" in neat
neat.mymodule(someobject)
d_kennetz
  • 5,219
  • 5
  • 21
  • 44
  • 1
    This doesn't work. Problem here is that 'neat-python' installs using package name 'neat' – Z4-tier Jul 08 '19 at 16:42
  • This would work in the case of a package actually installing with a hyphenated name. – d_kennetz Jul 08 '19 at 16:43
  • true, it works to the extent that it is valid syntax... but it won't help OP in this case. – Z4-tier Jul 08 '19 at 16:45
  • But it does address the title of the question for future visitors. His question was partially, "Does PyPI actually accept packages with illegal Python names or is there a solution I'm overlooking?" which this addresses perfectly. – d_kennetz Jul 08 '19 at 16:46
  • will PyPI actually let packages be installed with names that would require this? Obviously it's OK for the "PyPI name" to be named all sorts of things, but is it a requirement that the actual importable python package have a valid name? – Z4-tier Jul 08 '19 at 16:50