Zeinab Abbasimazar
Zeinab Abbasimazar

Reputation: 10439

Strange Import Error in PyDev Eclipse for Custom Classes

Hi I'm trying to write a program in python using Eclipse and PyDev. I have a project structure like this:

enter image description here

"Root", "GeneralClasses" and "UserClasses" are three directories containing python modules named "FileA", "FileB" and "FileC" and as it's clear there are three classes named "ClassA", "ClassB" and "ClassC".

I was trying to import "ClassA" in "FileC" and I got import error. I tried multiple ways:

  1. import ClassA
  2. from FileA import ClassA
  3. from FileA import *
  4. from GeneralClasses.FileA import ClassA
  5. from GeneralClasses.FileA import *
  6. from Root.GeneralClasses.FileA import ClassA
  7. from Root.GeneralClasses.FileA import *

All of them gave me the same error. I don't know how to solve the problem. I'll appreciate any help.

Upvotes: 0

Views: 1149

Answers (2)

Brian J Murray
Brian J Murray

Reputation: 167

Are you using the PyDev view in Eclipse?

If you're editing Python code in a different view, the context menu has an option to create a new "folder" instead of a "python package". For folders, Eclipse doese not autogenerate an init.py file. As a result, the python interpreter doesn't see the folder as a subpackage.

Upvotes: 0

Robin Newhouse
Robin Newhouse

Reputation: 2408

I recreated your scenario and have the same problem. Perhaps you did the same thing I did and put your own Root folder in place? (I suspect this because PyDev calls it src.) You'll need to have the src folder be added to the python path, which PyDev conveniently does for you. All you need to do is: File > new > other > PyDev > source folder

more information here http://pydev.org/manual_101_project_conf2.html

Place those files in the src directory and the importing should work fine.

Good luck!

Upvotes: 1

Related Questions