Search code examples
pythonwindowspython-module

python import works interactively but not from script


Disclaimer: I'm not a Windows expert, so this may be something idiosyncratic to Windows.

I'm observing a situation where I can import a module in interactive mode but not when requested from a file. Here is foo.py in its entirety:

#!/usr/bin/env python
import pyftdi

When I try to run this in Python 3.6.4, it fails:

E:\>py foo.py
Traceback (most recent call last):
  File "foo.py", line 2, in <module>
    import pyftdi
ImportError: No module named pyftdi

...but when I import the module interactively, it succeeds:

E:\>py
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyftdi
>>>

(FWIW, I don't get an error when repeating this under Mac OS X.)

Any thoughts about what's going on?


Solution

  • The problem is most likely that the version of python is different and has different modules installed. To fix this you can simply do py -3.6 foo.py instead of py foo.py.