3

I have install scipy to read data from a .mat file. When I am in Python in the command prompt I am able to type the following and get the value I desire:

Command Prompt

>>> import scipy
>>> import scipy.io
>>> from scipy.io import loadmat
>>> x=loadmat('C:\My websites\Rooftop PV.mat')
>>> size = x['component']['Size'][0][0][0][0]
>>> print(size)
150
>>> import sys
>>> sys.executable
'C:\\...\\Documents\\anaconda3\\python.exe'

However, when I run the .py script...

.py script

import sys
print(sys.executable)
import scipy
import scipy.io
from scipy.io import loadmat

x = loadmat('C:\My websites\Rooftop PV.mat')
size = x['component']['Size'][0][0][0][0]
print(size)

it is unable to find the scipy.io module, producing this error:

Error:

 C:\Python\pythonw.exe
 File "C:\Python\testmatfile.py", line 4, in <module>
     import scipy.io
 ModuleNotFoundError: No module named 'scipy.io'

Any ideas on why this might be? Thanks!

4
  • Can you import sys and share sys.executable for both of these examples? Commented Jun 26, 2018 at 21:41
  • I updated the outputs for each example with sys.executable results. Commented Jun 26, 2018 at 21:47
  • I think you might be using different python environments between your interpreter and script examples. It looks like one is anaconda and the other might be a different installation. Can you share how you are running the script? Commented Jun 26, 2018 at 21:56
  • I am running the script from IDLE. Commented Jun 26, 2018 at 22:02

1 Answer 1

4

As you can see there are two different python executables used. C:\\...\\Documents\\anaconda3\\python.exe is used in the first case and C:\Python\pythonw.exe is used in the second case. You have your lib installed for the first python executable (into the corresponding path), so you just need to run

C:\Python\pythonw.exe -m pip install <lib_name>

command to install the lib into the path related to the second executable. You can also use virtualenv or docker to run your script into isolated env with all required dependencies.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.