python 2.7 - Import statement continues to fail -
i need figuring out import error.
here directory structure python project.
├── files │ ├── dictionary_files │ │ │ └── transcripts ├── src │ ├── package1 │ │ ├── adapt_dictionary.py │ │ ├── adapt_dictionary.pyc │ │ └── __init__.py │ ├── package2 │ └── subtitle.py └── test ├── logs │ └── error_log_dict.txt ├── test1.py └── test2.py
here's problem. file test1.py
testing suite wrote adapt_dictionary.py
. in adapt_dictionary.py
have class called d_bot
.
class d_bot: def __init__(self):
i'm trying import class test1.py
file.
import sys import import sys sys.path.append("/home/andy/documents/projects/ai_subs/src/package1") adapt_dictionary import d_bot
the console yields cannot import name d_bot
. not sure what's going on. have tried few things.
- ensure no circular dependencies (good on this)
- changing
pythonpath
point corresponding directory class lies - messing
sys.path
my python path given follows in .bashrc
file.
export pythonpath=${pythonpath}:/home/andy/documents/projects/ai_subs/src/package1
still no luck. i'm running python 2.7.6 , i'm out of ideas.
first of all, sys.path.append
line wrote wrong. if want import file in package should add directory the package contained , import file package:
import sys sys.path.append("/home/andy/documents/projects/ai_subs/src") package1.adapt_dictionary import d_bot
you may thing adding src/package1
, using import adapt_dictionary
equivalent above: it not!
- if have different
adapt_dictionary
files inpythonpath
may imported instead of 1 package - moreover in cases does matter whether module imported package or not (e.g. if use
pickle
module must consistent in imports otherwise code breaks).
moreover trying fix wrong error. interpreter does import adapt_dictionary
module, cannot find d_bot
class. see:
$mkdir package1 $touch package1/__init__.py $echo 'class x: pass' > package1/a.py $echo 'from package1.a import x > package1.a import y' > test1.py $python test1.py traceback (most recent call last): file "test1.py", line 2, in <module> package1.a import y importerror: cannot import name y $echo 'from package1.b import x' > test2.py $python test2.py traceback (most recent call last): file "test2.py", line 1, in <module> package1.b import x importerror: no module named b
note how error resembles first 1 , not second one? , first 1 occurs because a.py
exists not contain y
class.
you may have adapt_dictionary.py
module somewhere , interpreter importing 1 instead or there may obsolete .pyc
around.
also, adapt_dictionary
actual name of module? i've seen many times people posting code made names when actual files called same name built-in file, in case import
s prefer built-in 1 yours.
try doing:
import sys sys.path.append("/home/andy/documents/projects/ai_subs/src/package1") import adapt_dictionary print(adapt_dictionary.__file__)
to check module got imported.
Comments
Post a Comment