ImportError: No module named when module is there

后端 未结 4 651
名媛妹妹
名媛妹妹 2021-01-15 01:52

I usually run python2 but I am playing with python3. Right now I am confused as to why I am getting this error.

When I run the command ./test_web_events.py

相关标签:
4条回答
  • 2021-01-15 02:16

    Make sure you have __init__.py file in all your package folders so that you tructure looks like

    /python_lib
       Makefile
       /qe
          /tests
             test_web_events.py
          /util
             __init__.py                <------------ create this file
             scratchstore.py
          /trinity
          __init__.py
    

    and then the you cd to python_lib folder and run ``export PYTHONPATH=`pwd```

    0 讨论(0)
  • 2021-01-15 02:18

    This is most likely because you haven't added /python_lib/qe to your PYTHONPATH.

    When you try to import a module, the interpreter will look for it only in a certain number of places, you cannot arbitrarily try to import a module from anywhere.

    The most common ways are to have a package installed via pip, to have the module sitting in the same directory as the .py file, or to have added the path to that module to the PYTHONPATH.

    See: https://docs.python.org/2/tutorial/modules.html#the-module-search-path

    It seems like the latter case is most likely what you want to do. This is going to be dependent on your OS, but googling it should be straightforward.

    0 讨论(0)
  • 2021-01-15 02:25

    The issue is that /python_lib is not in Python path. The behavior is the same on both Python 2 and 3.

    In general, do not run scripts from within (inside) a Python package, run them from the top-level directory instead:

    /python_lib$ python -m qe.tests.test_web_events
    

    Thus /python_lib is in Python path and /python_lib/qe/tests is not. It assumes that there is tests/__init__.py file.

    Do not modify sys.path manually. It may lead to subtle bugs related to importing modules. There are better alternatives e.g., if you don't want to run the scripts from /python_lib, just install the development version:

    (your_virtualenv)/python_lib$ pip install -e .
    
    0 讨论(0)
  • 2021-01-15 02:37

    Just #!/usr/bin/env python add this to your all scripts. Make sure it's on the top.Like;

    #!/usr/bin/env python
    import qe
    

    I assume you added Python 3 to path, if you did, the only problem is this. You do not need init.py or sys.path or anything. This line already finding Python path automatically if you added Python 3 to path, if Python 2 still on the path then it's normal you got error.

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