1

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?

Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
  • Spyder (and basically all IDEs for Python) use PYTHONPATH to find libraries. Your likely issue is that you don't have access to these packages in the default search path that Spyder uses to find modules and packages. You need to modify your PYTHONPATH somehow to include the directory that you run your 'main.py' from. This directory includes the package 'generation' which your IDE cannot find. Otherwise you will have to use the regular python interpreter from the same directory as your 'main.py' to run your code properly. – Shashank Sep 04 '13 at 07:47
  • @ShashankGupta My current PYTHONPATH is just the directory of my current project. Do I have to do anything about that? – Sibbs Gambling Sep 04 '13 at 07:53
  • Hmm if that is the case, then I'm sorry to say that I don't really know why your Python can't find 'generation' module. pythonpath is how most Python IDEs work with finding modules. I don't personally use Spyder though so there could be some different path settings I'm not aware of. – Shashank Sep 04 '13 at 08:04
  • @ShashankGupta Thank you for your kindness anyway! – Sibbs Gambling Sep 04 '13 at 08:27

3 Answers3

2

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

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
0

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.

Community
  • 1
  • 1
Jblasco
  • 3,827
  • 22
  • 25
  • Could you please just tell me what I should do with the "pythonpath" manager? – Sibbs Gambling Sep 04 '13 at 07:54
  • In my version, the pythonpath manager is in Tools. When I open it, an empty screen appears. If I hit add path another screen opens. I suggest you choose the folder "clustering" and accept. That will add it to your pythonpath. I stress again, this will only work for spyder, nothing else. – Jblasco Sep 04 '13 at 07:57
  • Yeah, exactly. I have done the same thing, but the error remains. – Sibbs Gambling Sep 04 '13 at 07:59
0

It seems is caused by typo.

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

OGURA Daiki
  • 177
  • 5