0

I'm trying to make an api for Pokemon, and I was thinking of packaging it, but no matter what I do, as soon as I try to import from this file, it comes up with this error.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/student/anaconda3/lib/python3.6/site-packages/pokeapi/__init__.py", line 1, in <module>
    from Pokemon import *
ModuleNotFoundError: No module named 'Pokemon'

The directory is like this:

/pokeapi
    /pokeapi
        __init__.py
        Pokemon.py
    setup.py

I install it with pip, and that error comes up.

Code for init.py:

from Pokemon import *

Code for Pokemon.py: https://hastebin.com/qegupucuma.py

I don't know what I'm doing wrong

DePianoman
  • 170
  • 1
  • 1
  • 10

2 Answers2

0

You are trying to import Pokemon without accessing the parent folder. If you do not use relative imports or write the full path for the module, it will think the module you are trying to import is in the project root.

Basically, the root is the folder you are running it. Try creating a pokemon.py file into the first pokeapi folder, nexto to setup.py and see that you can import it normally.

Change your __init__.py either to: from pokeapi.pokemon import * or from . pokemon import * and then use it as pokeapi.<Pokemon module function>

leoschet
  • 1,697
  • 17
  • 33
0

I fixed it by going through the init.py file, and changing it to:

from .pokemon import IDfromPokemon
from .pokemon import PokemonfromID
from .pokemon import PokemonLearnset
from .pokemon import PokemonLocations
from .pokemon import PokemonTypes
from .pokemon import PokemonSprite
from .pokemon import ShinyPokemonSprite
from .pokemon import PokemonAbilities
from .pokemon import Pokemon

From everywhere else, I saw that the dots were a bad thing, but they worked this time, so I'm confused. Eh. It worked, so whatever.

DePianoman
  • 170
  • 1
  • 1
  • 10
  • now you are using relative imports, you can still use the `*` to import all, here some reading about relative imports: [one](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) and [two](https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python) – leoschet Jul 12 '18 at 14:19
  • in the end, it's all about where you are and where your module is – leoschet Jul 12 '18 at 14:29