Reputation: 11
I have python code that is structured like this:
src
--->commoncode.py
--->folder1
--->file1.py
--->folder2
--->file2.py
--->folder3
--->file3.py
I want to use the code that is in commoncode.py in the files fileN.py. I have tried includingfrom . import commoncode
but that does not work (ImportError: cannot import name 'commoncode'
).
I am able to use the code with import commoncode
if I include a symlink in each of the subfolders but that seems hacky and sort of defeats the purpose of having common code.
The only code in commoncode.py right now is class commoncode():
.
Let me know if there is any information that I can further provide that would be useful.
Upvotes: 0
Views: 44
Reputation: 30210
Since you're not using packages, one way is to modify your path:
import sys
sys.path.insert(0, "..")
from commoncode import <whatever>
# Now you can access imported symbols from commoncode.py
Upvotes: 1
Reputation: 44
usually when importing you need to import as follows,
from (Name of the module) import (name of the class within the module).
so in your case, if I understood correctly I believe it would be:
from (Name of the common code module) import commoncode.
at the top of any of your modules you wish to use commoncode in, this is how it would be imported.
Please make sure that you haven't missed any capital or lowercase letters as well, as it is case sensitive when you import. Hope this could be of some help.
Upvotes: 0