import error: 'No module named' *does* exist

前端 未结 11 1554
星月不相逢
星月不相逢 2020-11-29 04:30

I am getting this stack trace when I start pyramid pserve:

% python $(which pserve) ../etc/development.ini
Traceback         


        
相关标签:
11条回答
  • 2020-11-29 04:56

    I got this when I didn't type things right. I had

    __init.py__ 
    

    instead of

    __init__.py
    
    0 讨论(0)
  • 2020-11-29 05:02

    My usual trick is to simply print sys.path in the actual context where the import problem happens. In your case it'd seem that the place for the print is in /home/hughdbrown/.local/bin/pserve . Then check dirs & files in the places that path shows..

    You do that by first having:

    import sys
    

    and in python 2 with print expression:

    print sys.path
    

    or in python 3 with the print function:

    print(sys.path)
    
    0 讨论(0)
  • 2020-11-29 05:08

    The PYTHONPATH is not set properly. Export it using export PYTHONPATH=$PYTHONPATH:/path/to/your/modules .

    0 讨论(0)
  • 2020-11-29 05:08

    I met the same problem, and I try the pdb.set_trace() before the error line.

    My problem is the package name duplicate with the module name, like:

    test
    ├── __init__.py
    ├── a
    │   ├── __init__.py
    │   └── test.py
    └── b
        └── __init__.py
    

    and at file a/__init__.py, using from test.b import xxx will cause ImportError: No module named b.

    0 讨论(0)
  • 2020-11-29 05:09

    I had the same problem, and I solved it by adding the following code to the top of the python file:

    import sys
    import os
    
    sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
    

    Number of repetitions of os.path.dirname depends on where is the file located your project hierarchy. For instance, in my case the project root is three levels up.

    0 讨论(0)
提交回复
热议问题