0

I can not run python scripts on Command Prompt which has to import any module whereas I can run that script without any error on python IDLE. I think, this is happening after I install Anaconda on my machine.

Even if I run "Python" command it starts the shell and then there I can import any module that I want without any error.

C:\Users\USER>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt4
>>>

But when I try to run a script which has to import any module directly It rises a "ModuleNotFoundError" error.

E:\Python\PyQt4\Apps\test>main.py
Traceback (most recent call last):
File "E:\Python\PyQt4\Apps\test\main.py", line 9, in <module>
from PyQt4 import QtCore, QtGui
ModuleNotFoundError: No module named 'PyQt4'

E:\Python\PyQt4\Apps\test>

I don't know why it is happening

  • In CMD, running `python` searches the directories in the `PATH` environment variable for "python.*" and checks for each file extension in the `PATHEXT` environment variable (e.g. "python.com", "python.exe", etc). Normally it finds "python.exe" and executes it via `CreateProcess`. – Eryk Sun Jan 05 '18 at 08:39
  • Running `main.py` instead searches for "main.py" and also "main.py.*" with the `PATHEXT` file extensions. When a match is found it tries to execute it via `CreateProcess` and fails (given it's not a PE executable) and then tries `ShellExecuteEx`, which executes the default action (usually "open") for the registered file association for a ".PY" file. The registered template command should use `"%1"` for the file (script) path and `%*` for the command-line arguments. – Eryk Sun Jan 05 '18 at 08:40
  • So, What I should do now? – Muhammad Naimul Islam Jan 05 '18 at 13:23
  • You can either find "main.py" using some other means and run it explicitly as `python "path\to\main.py"`, or change the .PY file association to use another version of Python. It should be one of the listed Python applications in the "open with => choose another app" context menu of a .PY file, or use the control panel's Default Programs app. For the "open with" approach, lock a choice in by selecting to "always use this app". – Eryk Sun Jan 05 '18 at 13:39

0 Answers0