3

I'm trying to run a Python program, which uses the pygame modules, from MATLAB. I know I can use either

system('python program.py')

or just

! python program.py

However, I keep getting the error:

Traceback (most recent call last):
  File "program.py", line 1, in <module>
    import pygame
ImportError: No module named pygame

What is strange is that if I run the program from the command line, it works just fine. Does anyone know why if run from within MATLAB, Python can't find pygame?

3 Answers 3

2

The problem may be that MATLAB is not seeing your PYTHONPATH, which normally stores Python libraries and modules. For custom modules, PYTHONPATH should also include the path to your custom folders.

You can try setting the value of PYTHONPATH from within a MATLAB running session:

PATH_PYTHON = '<python_lib_folder>' 
setenv('PYTHONPATH', PATH_PYTHON); % set env path (PYTHONPATH) for this session
system('python program.py'); 

See also the possibly relevant SO answer here: How can I call a Qtproject from matlab?

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

Comments

1

As I haven't used matlab too often and don't have the program available now I cannot say for sure, but matlab may be creating a custom environment with custom paths (this happens a lot, so the user has a very consistent experience in their software). When matlab installs it may not export paths to its own modules to your default environment. So when calling for pygame.py outside of matlab, python cannot find pygame.py under its usual lookup paths.

Solutions could be:

  • find the pygame.py, and map the path to it directly in your code, though this could cause you headaches later on during deployment

  • Try just copying the pygame.py file to your working directory, could have dependences that need to addressed.

  • Install pygame directly from its developer at http://www.pygame.org. Version differences could be a problem but pygame gets put under the usual lookup paths for python. (This would be my preferred solution personally.)

  • Or just export the location of path to pygame in matlab's library to your default enivronment. This could be a problem during deployment too.

Comments

0

For posterity, first try everything that Stewie noted here ("Undefined variable "py" or class" when trying to load Python from MATLAB R2014b?). If it doesnt work then it's possible that you have multiple pythons. You can try and check which python works (with all the related installed modules) on your bash/terminal. And then use

pyversion PYTHONPATH

to let matlab know the right path. Also use py.importlib.import_module('yourmodule') to import the module after that. That should get you started.

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.