Error when importing Python modules that include "import" statements

조회 수: 18 (최근 30일)
Jonas GROS
Jonas GROS 2018년 4월 27일
편집: Jonas Gros 2018년 5월 18일
I am observing the same behavior as reported here: https://stackoverflow.com/questions/45848273/matlab-cant-call-python-module-with-imports (i.e. I can import a simple example python module defined in Matlab documentation (mymod.py, see: https://ch.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html ), but like this person when I add "import numpy as np" in that mymod.py file, then matlab gets an error when I try to import it.)
I.e., I can import fine the file mymod.py having content:
def search(words):
"""Return list of words containing 'son'"""
newlist = [w for w in words if 'son' in w]
return newlist
But, I cannot import the file mymod2.py having content:
def search(words):
"""Return list of words containing 'son'"""
import numpy as np
newlist = [w for w in words if 'son' in w]
return newlist
Here is what I was doing in Matlab, together with the error messages:
>> py.importlib.import_module('mymod2')
ans =
Python module with properties:
search: [1×1 py.function]
<module 'mymod2' from 'mymod2.py'>
>> N = py.list({'Jones','Johnson','James'})
N =
Python list with no properties.
['Jones', 'Johnson', 'James']
>> py.mymod2.search(N)
Error using _init_><module> (line 26) Python Error: ImportError: Importing the multiarray numpy extension module failed. Most likely you are trying to import a failed build of numpy. If you're working with a numpy git repo, try `git clean -xdf` (removes all files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: The specified module could not be found.
Error in type_check><module> (line 11) import numpy.core.numeric as _nx
Error in _init_><module> (line 8) from .type_check import *
Error in add_newdocs><module> (line 13) from numpy.lib import add_newdoc
Error in _init_><module> (line 153) from . import add_newdocs
Error in mymod2>search (line 3) import numpy as np
I cannot figure out why. More details below on what I tried.
This works: system('C:\Users\BobbyWatson\AppData\Local\Enthought\Canopy\edm\envs\User\python D:\MyPath\mymod2.py')
When I run [v, e, i] = pyversion I get:
v =
2.7
e =
C:\Users\BobbyWatson\AppData\Local\Enthought\Canopy\edm\envs\User\python.exe
i =
logical
(Which I did set up myself.)
  댓글 수: 3
Jonas GROS
Jonas GROS 2018년 4월 27일
And, finally, note that my Python is working just fine within Canopy, which is how I am using Python.
Jonas Gros
Jonas Gros 2018년 5월 2일
But 'py.importlib.import_module('sys')' works fine. It seems to be some packages that fails, including numpy, and scipy (failing when importing numpy). Everything works just fine within canopy or command prompt.

댓글을 달려면 로그인하십시오.

답변 (1개)

Padmanie Maulkhan
Padmanie Maulkhan 2018년 5월 18일
Try pip installing your packages from the command line. Your packages might only be installed in Canopy, which is why your code runs there. However, you need to also install them through command line for MATLAB to be able to access it.
  댓글 수: 1
Jonas Gros
Jonas Gros 2018년 5월 18일
편집: Jonas Gros 2018년 5월 18일
Thanks. Finally the issue has been solved.
One solution: start Matlab from the Canopy command prompt. This made sure the path was ok and Matlab was looking for Python at the right place. (Alternative that also works: make sure to add the path to Canopy Python using: setenv('PATH', [ 'C:\Users\name_of_the_user\AppData\Local\Enthought\Canopy\edm\envs\User\Scripts;' getenv('PATH')]) (This was the path on my computer)) That worked. (2) I also had some problems with netCDF4. It worked if first I do: netcdf.inqLibVers; (Loads Matlab's netCDF.)
(Note about your reply: Matlab does not contain Python, I believe. One needs his own Python install. For that, one can either use Canopy, Anaconda; or a the basic "empty" Python distribution and then install all the packages as you propose. This is not important for solving the issue, the problem was to make sure Matlab looks for Python at the right place, plus some unclear issue probably related to dependencies for netCDF, which could be solved by loading the Matlab netCDF.) Thanks.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by