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

前端 未结 11 1553
星月不相逢
星月不相逢 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:46

    They are several ways to run python script:

    • run by double click on file.py (it opens the python command line)
    • run your file.py from the cmd prompt (cmd) (drag/drop your file on it for instance)
    • run your file.py in your IDE (eg. pyscripter or Pycharm)

    Each of these ways can run a different version of python (¤)


    Check which python version is run by cmd: Type in cmd:

    python --version 
    

    Check which python version is run when clicking on .py:

    option 1:

    create a test.py containing this:

    import sys print (sys.version)
    input("exit")
    

    Option 2:

    type in cmd:

    assoc .py
    ftype Python.File
    

    Check the path and if the module (ex: win32clipboard) is recognized in the cmd:

    create a test.py containing this:

    python
    import sys
    sys.executable
    sys.path
    import win32clipboard
    win32clipboard.__file__
    

    Check the path and if module is recognized in the .py

    create a test.py containing this:

    import sys
    print(sys.executable)
    print(sys.path)
    import win32clipboard
    print(win32clipboard.__file__)
    

    If the version in cmd is ok but not in .py it's because the default program associated with .py isn't the right one. Change python version for .py

    To change the python version associated with cmd:

    Control Panel\All Control Panel Items\System\Advanced system setting\Environnement variable In SYSTEM variable set the path variable to you python version (the path are separated by ;: cmd use the FIRST path eg: C:\path\to\Python27;C:\path\to\Python35 → cmd will use python27)

    To change the python version associated with .py extension:

    Run cmd as admin:

    Write: ftype Python.File="C:\Python35\python.exe" "%1" %* It will set the last python version (eg. python3.6). If your last version is 3.6 but you want 3.5 just add some xxx in your folder (xxxpython36) so it will take the last recognized version which is python3.5 (after the cmd remove the xxx).

    Other:

    "No modul error" could also come from a syntax error btw python et 3 (eg. missing parenthesis for print function...)

    ¤ Thus each of them has it's own pip version

    0 讨论(0)
  • 2020-11-29 04:48

    If you have a script with the same name as your module in another directory, it will use that instead. For example:

    module.py
    
    module
    |
    |--module
    |  |
    |  |--__init__.py
    |  |--module.py
    

    This will make it so that the first module.py is being used, not the second one.

    0 讨论(0)
  • 2020-11-29 04:49

    In case this is of interest to anyone, I had the same problem when I was running Python in Cygwin, in my case it was complaning that pandas wasn't installed even though it was. The problem was that I had 2 installations of python - one in windows and another one in cygwin (using the cygwin installer) and although both were the same versions of Python, the Cygwin installation was confused about where Pandas was installed. When i uninstalled cygwin's Python and pointed Cygwin at the windows installation everything was fine

    0 讨论(0)
  • 2020-11-29 04:51

    I've had this problem too, I had just forgotten to type workon myproject in the terminal before executing my program.

    0 讨论(0)
  • 2020-11-29 04:54

    I set the PYTHONPATH to '.' and that solved it for me.

    export PYTHONPATH='.'
    

    For a one-liner you could as easily do:

    PYTHONPATH='.' your_python_script
    

    These commands are expected to be run in a terminal

    0 讨论(0)
  • 2020-11-29 04:56

    I had the same issue. I solved it by running the command in a different python version. I tried python3 filename.py. Earlier i was using Python 2.7.

    Another possibility is that the file from which something is imported may contain BOM (Byte Order Mark). It can be solved by opening the file in some editor which supports multiple encoding like VSCode (Notepad++) and saving in a different encoding statndard like ANSI, UTF-8(without BOM).

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