Running a Python Script in Matlab wouldn't execute it

6 vues (au cours des 30 derniers jours)
Maen Saassouh
Maen Saassouh le 11 Août 2020
Modifié(e) : Maen Saassouh le 17 Août 2020
The main issue I am facing is not being able to run a python script outside of Matlab via
system('python pythonFile.py')
If I use the latter method, no errors are shown, but the python code is simply not being executed (I have put "print" functions here and there to check), and I get
ans = -1.0737e+09
as a result, with no output whatsoever.
Whereas, if I run the same script from the command prompt (Anaconda), it works fine. Keep in mind that I have opened Matlab from the same prompt by typing 'matlab' so that I get specific environment variables. The odd thing is, if I remove specific imports such as Keras, Tensorflow, and Numpy libraries from my python code, Matlab is able to run the script fine, i.e., the print functions' output in this case is shown, but of course, the training of my ML model is no longer there. Again, none of this happens if I run the python script from the Anaconda prompt.
I have also tried to use pyenv and py. Once I verified that the pyenv properties such as Version, Executable, etc. are setup, I tried the following via the Matlab Command Window:
py.pythonFile.main
. Although it seems to start to run properly (the very first print statements show in the Command Window) but then Matlab either crashes immediately, just before the import statements of the libraries afore mentioned, or prompts the error:
Unable to resolve the name py.pythonFile.main
(depending on whether I import tensorflow.keras or keras in my python file, respectively).
I have tried echo %path% in my Anaconda prompt, in which the python code runs properly, and used
setenv(path, theEchoedPath)
but I got the same issue.
I need to run the python file from Matlab. Any suggestions?
Libraries used: Tensorflow 1.4.0, Keras 2.2.4. Python version: 3.6.6.
  1 commentaire
Maen Saassouh
Maen Saassouh le 16 Août 2020
Modifié(e) : Maen Saassouh le 17 Août 2020
Update on the issue:
  • Using Octave instead of Matlab eliminated this issue, however I could not proceed with Octave as it doesn't support many Matlab functions in my case.
  • I tried making a batch file in which I select my environment and call my python file. After calling it through Matlab, it simply exits after reaching the import statements in Python. Of course, I did not face this issue after calling the batch file outside of Matlab (again, I need to do so via Matlab of course).
  • I tried starting Matlab by double clicking on the Matlab file this time. Initially, I used to start Matlab through Anaconda prompt because otherwise, it wouldn't recognize the necessary libraries, and I suspect that this is a Path issue. However, this time, because I am using the mentioned batch file, I did not face the latter issue and the Python file was executed just fine!
Even though the problem is solved now, I would appreciate any explanation from the community regarding this issue, or an alternative solution, as this could be useful for similar cases.
Thank you.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by