Python ImportError,无法解决

I'm working on an open source Python project and added a new module, measure.py to it and was trying to import and use it in another module I had added measure_long.py. However, I am running into a ImportError error.

Here is the structure of my project, named opencensus-python:

├── opencensus-python
│   ├── opencensus
│   │   ├── trace
│   │   │    │__  __init__.py
│   │   │    │__  measure.py 
│   │   │    │__  measure_long.py
│   │   

Here is the code in measure.py. This runs fine, and I have an import statement in there that I don't use just to prove to myself that importing from that folder did work (so perhaps my __init__.py file is fine?) enter image description here

这是“ measure.long.py”中的代码。

enter image description here

当我运行它时,这是我得到的错误:

Traceback (most recent call last):
     File "measure_long.py", line 2, in <module>
        from opencensus.trace import measure
ImportError: cannot import name 'measure' from 'opencensus.trace' (/Users/anamnavied/Documents/CSC492/opencensus-python/venv/lib/python3.8/site-packages/opencensus/trace/__init__.py)

我遇到了一些有关导入错误的堆栈溢出问题,并进行了以下操作:

  1. changed the absolute import statements to relative one: from .measure import Measure but get this error instead ImportError: attempted relative import with no known parent package. I debugged this according to another stack overflow post by adding a shebang to both Python files and running them as an executable script but still get the same ImportError.
  2. I modified the __init__.py file, since that was what referenced in the error message. This was written by the original developers and contained the code marked #original and I added mine with comments: enter image description here

任何建议表示赞赏!

评论