Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import python module in script, but can in command prompt

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!

like image 846
jeffgiff Avatar asked Apr 13 '26 07:04

jeffgiff


1 Answers

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.

like image 50
Artsiom Praneuski Avatar answered Apr 15 '26 10:04

Artsiom Praneuski