How to import python package with sdk

조회 수: 2 (최근 30일)
Mark Higger
Mark Higger 2018년 8월 8일
편집: Joseph Ziminski 2019년 12월 9일
I followed the instructions here (https://www.mathworks.com/help/compiler_sdk/ml_code/create-a-python-application-with-matlab-code.html) and successfully created a python package. While I can import the package, when I try to initialize it I get an error saying Pkg.initialize() does not exist. I'm also not sure it installed correctly. When I ran sudo python setup.py install (w/o sudo i get a permissions error) I get the following output.
"running install
running build
running build_py
running install_lib
running install_egg_info
Removing /Library/Python/2.7/site-packages/matlabruntimeforpython-R2017b-py2.7.egg-info
Writing /Library/Python/2.7/site-packages/matlabruntimeforpython-R2017b-py2.7.egg-info
removing 'build/lib' (and everything under it)
'build/bdist.macosx-10.12-intel' does not exist -- can't clean it
'build/scripts-2.7' does not exist -- can't clean it
removing 'build'"
  댓글 수: 4
Kojiro Saito
Kojiro Saito 2018년 8월 9일
What's you Python codes for calling compiled package? I have tried the document's steps and works without an error.
Mark Higger
Mark Higger 2018년 8월 9일
" import ParResPkg
ParResPkg.initialize() "
and I am getting the error
File "/Users/markhigger/Documents/MATLAB/TestPkg.py", line 10, in <module>
ParResPkg.initialize()
AttributeError: module 'ParResPkg' has no attribute 'initialize'
Is it possible there is an error in the install? At the end of the install I get message:
'build/bdist.macosx-10.12-intel' does not exist -- can't clean it
'build/scripts-2.7' does not exist -- can't clean it

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

답변 (2개)

Kojiro Saito
Kojiro Saito 2018년 8월 9일
I don't have Mac OS now, so I can't test module installation, but I have tested in Linux OS.
python ./setup.py install
displayed the following output.
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/MagicSquarePkg
copying MagicSquarePkg/__init__.py -> build/lib.linux-x86_64-2.7/MagicSquarePkg
copying MagicSquarePkg/MagicSquarePkg.ctf -> build/lib.linux-x86_64-2.7/MagicSquarePkg
running install_lib
running install_egg_info
Removing /usr/local/lib/python2.7/dist-packages/matlabruntimeforpython-R2018a.egg-info
Writing /usr/local/lib/python2.7/dist-packages/matlabruntimeforpython-R2018a.egg-info
removing 'build/lib.linux-x86_64-2.7' (and everything under it)
'build/bdist.linux-x86_64' does not exist -- can't clean it
'build/scripts-2.7' does not exist -- can't clean it
removing 'build'
I have a concern that in your case, there's no "creating" and "copying" descriptions, so I guess your module was not installed properly.
Could you clean-up the module first,
python ./setup.py clean
and reinstall it again?
python ./setup.py install
  댓글 수: 3
Mark Higger
Mark Higger 2018년 8월 10일
I attached a few screenshots of the package creation, the python code/error, the folder containing everything and the output of python setup install
Kojiro Saito
Kojiro Saito 2018년 8월 11일
편집: Kojiro Saito 2018년 8월 13일
Two things I want to confirm.
(1) Is there any MagicSqrPkg.py file in your current direcotry? It would cause module conflict.
(2) Does this 'no attribute initialize' error occur in Python command line (call 'python TestPkg.py' in Terminal, not via iPython)? As this document says, rather than python commands, we need to use mwpython command, so, matlabroot/bin/mwpython ./TestPkg.py is a correct command.

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


Joseph Ziminski
Joseph Ziminski 2019년 12월 9일
편집: Joseph Ziminski 2019년 12월 9일
I had the same problem, not sure if this solution would work for you but what (at least has initially) seemed to fix it:
Finding package at 'C:\Users\Owner\Anaconda3\envs\RealptoMatlab\Lib\site-packages\magSq' there is a 'magic_square' folder (my mat file was 'magic_square.m', I set the namespace to 'magSq' when compiling), '_pycache_' and '__init__.py'. However the files contained in other working modules are all found only in the 'magic_square' folder, including a .ctf file. As such in the ..\magSq folder I deleted '_pycache_' and '__init__.py'. Then I copied the contents of 'magic_square' (which also includes a _pycache _ and the init file) into the magSq folder and deleted the empty 'magic_square' folder. Then it seems to find .initilise() etc.
Not sure why this happened, the only difference between this and other working modules is that I compiled it with a namespace, but feels unlikely that is the cause.
UPDATE: to finally get it working I had to change the file name of 'magic_square.ctf' to 'magSq.ctf' and it worked.

카테고리

Help CenterFile Exchange에서 Python Client Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by