Pregunta

I am using Spyder.

I have just started building a project. Its architecture is currently as shown.

enter image description here

As shown, I have 2 packages, one of which has a module called trajectorygeneration now. Both the __init__.py files are automatically generated. I am trying to import the module into the main.py, but I ended up with such error messages:

>>> from generation import trajectorygeneration
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named generation

How may I fix this problem?

¿Fue útil?

Solución

(Spyder dev here) This is a bug in Spyder. We are not adding the project's path to PYTHONPATH and that's why you can't import your project in the console after you open it. I created an issue for you and we'll fix it in our next major release (i.e 2.3).

A workaround for now (as it was mentioned by @Jblasco) is to use our PYTHONPATH manager to add the project's path by yourself. After doing it, you need to open a new console so that changes can take effect.

Otros consejos

Might it be that generation is not in your path and python can not find the package?

See: Expand Python Search Path to Other Source

Also, Spyder has a "pythonpath" manager (at least mine has) where you can include any folder in it. Problem is, that if you do it this way, then the python console, for example, will most likely not include those, so you could not import in the console.

It seems is caused by typo.

In your picture, target file name is trajectorygeneration.py. But main.py would import module named trajectorygeneration's'.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top