Reputation: 10439
Hi I'm trying to write a program in python using Eclipse and PyDev. I have a project structure like this:
"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:
import ClassA
from FileA import ClassA
from FileA import *
from GeneralClasses.FileA import ClassA
from GeneralClasses.FileA import *
from Root.GeneralClasses.FileA import ClassA
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
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
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