Python - MATLAB not working: MATLAB cant find python environment, numpy not working, fooof not working
13 Comments
Hi @Vyte Jan ,
Sorry for late response since busy working with clients all day long. I reviewed your comments Your query revolves around two main issues:
1. An error related to the importlib.import_module function in Python, specifically a TypeError indicating that a required argument is missing.
2. An error encountered while trying to install the importlib package via pip in PyCharm, highlighting issues with the build environment, specifically related to setuptools.
Let me address these issues
TypeError with importlib.import_module
The error message you received:
TypeError: import_module() missing 1 required positional argument: 'name'
indicates that you are calling importlib.import_module without providing the necessary argument. The import_module function requires a string argument that specifies the name of the module you want to import. Here's how you can correctly use it:
import importlib
# Correctly importing a module named 'math'
math_module = importlib.import_module('math')
# Now you can use math functions print(math_module.sqrt(16)) # Output: 4.0
Make sure you replace math with the actual module name you intend to import. The string should be the exact name of the module as it would be referenced in an import statement.
Error Installing importlib
Regarding the error when attempting to install importlib, it is important to note that importlib is part of the Python standard library starting from Python 3.1, so there is generally no need to install it separately via pip. The error message indicates that you're trying to install a package that should already be included with your Python installation. However, if you're encountering issues with setuptools, it suggests that your Python environment might be misconfigured. Here’s how to address this:
Fixing the Setuptools Issue
1. Make sure you have Setuptools Installed
Run the following command in your terminal or PyCharm terminal:
pip install --upgrade setuptools
2. Check Your Python Environment
Make sure that your virtual environment is activated properly. If you're using a virtual environment, activate it before running pip commands:
.\venv\Scripts\activate
3. Reinstall Pip
If problems persist, consider reinstalling pip:
python -m ensurepip --upgrade
4. Creating a New Virtual Environment
If none of the above steps resolve your issue, creating a new virtual environment might help:
python -m venv new_env_name source new_env_name/bin/activate
Direct Conversion vs Numpy
If you're considering avoiding numpy for conversions, ensure that your data types are compatible and use native Python functions where applicable. For example, converting lists to tuples can simply be done using:
my_list = [1, 2, 3] my_tuple = tuple(my_list) print(my_tuple) # Output: (1, 2, 3)
When working with imports, always handle exceptions to manage potential errors gracefully:
try:
my_module = importlib.import_module('some_nonexistent_module')
except ModuleNotFoundError as e:
print(f"Error importing module: {e}")
By addressing these points, you should be able to resolve both your TypeError and installation issues effectively. If you still have further questions or need additional clarification on any point, feel free to ask!
Hi @Vyte Jan,
The error message you are receiving indicates that you are trying to import numpy from its source directory. This typically occurs when the current working directory is set to the location where numpy is installed as source code, rather than where it is installed as a package. Ensure that your current working directory in MATLAB is not pointing to the numpy source directory. You can check this by using:
pwd
If it shows a path within the numpy source folder, change it to a different directory where you usually run your scripts.
Setting Up the Correct Python Environment
Using pyenv: To specify which Python environment MATLAB should use, execute:
pyenv('Version', 'path_to_your_python_executable');Make sure that this path points to a valid installation of Python that has numpy properly installed. Please click the following link for more information regarding Python compatibility.
https://www.mathworks.com/support/requirements/python-compatibility.html
Creating a Virtual Environment: If you haven’t already, consider creating a virtual environment specifically for your MATLAB project. Here’s how you can do this
python -m venv myenv
source myenv/bin/activate # On Windows use: myenv\Scripts\activate
pip install numpy matplotlib fooofAfter activating this environment, ensure MATLAB is using it with the pyenv command.
Verifying Your Installation: Open a terminal or command prompt and activate your virtual environment. Run Python and check if you can import numpywithout issues:
import numpy as np
print(np.__version__)If this works fine outside of MATLAB, then your Python installation is correct.
Environment Variables: Check if the PYTHONHOME and PATH environment variables are correctly set. In MATLAB, you can check these with:
getenv('PYTHONHOME')
getenv('PATH')If these variables are pointing to incorrect locations (like a previous installation), it could lead to conflicts.
Restarting MATLAB: After making changes, ensure you restart MATLAB to apply any changes made with pyenv.
Hope this helps.
Please let me know if you have any further questions.
Hi @Vyte Jan ,
First, let me make that your Python environment is correctly set up and recognized by MATLAB. Make sure that the version of Python in your virtual environment (fooof_venv,Python 3.9) is compatible with MATLAB 2021b. According to MathWorks documentation, Python 3.9 is indeed supported. Also, specify the path to your virtual environment's Python executable directly in MATLAB using:
python_env_path = '/Users/c1034944/ANALYSIS/fooof_venv/bin/python';
pyenv('Version', python_env_path);After executing this, verify if the pyenv command confirms that the environment has been loaded correctly.
Your current output for getenv('PYTHONHOME), getenv(PATH, and getenv(PYTHONPATH) indicates that they are either empty or not pointing to the correct locations when running MATLAB from the terminal.
PYTHONHOME: This variable should not typically be set for virtual environments; it might cause conflicts. It is best to leave it unset.
PYTHONPATH: If you have custom modules that need to be recognized by Python, you can set this variable; however, it should include paths where your modules are located, not the path to the Python executable.
PATH: Make sure that your PATH variable includes the directory of your virtual environment’s binaries. Here’s how you can modify your .bash_profile.
# Add virtual environment to PATH
export PATH="/Users/c1034944/ANALYSIS/fooof_venv/bin:$PATH"After making changes, remember to source your .bash_profileor restart your terminal:
source ~/.bash_profile
Starting MATLAB Correctly
When starting MATLAB from the terminal, ensure you're doing so after activating your virtual environment:
source /Users/c1034944/ANALYSIS/fooof_venv/bin/activate matlab
This will ensure that MATLAB inherits the activated environment's settings.
Resolving Import Errors
If you continue facing issues with importing NumPy in MATLAB:Make sure you can run a simple script in Python outside of MATLAB to confirm that NumPy is functioning properly.Use:
import numpy as np
print(np.__version__)If this works without issues, but fails in MATLAB, try running a simple command directly in MATLAB:
py.importlib.import_module('numpy');
By following these steps systematically, you should be able to resolve the issues related to configuring and using your Python virtual environment within MATLAB effectively.
Hi @Vyte Jan,
Integrating Python with MATLAB can sometimes be a nuanced process, especially when dealing with virtual environments. Let's break down the situation and address your concerns step by step.
Understanding the pyenv Command
The pyenv command in MATLAB is crucial for managing the Python environment. When you first set the Python executable using:
python_env_path = '/Users/c1034944/ANALYSIS/fooof_venv/bin/python';
pyenv('Version', python_env_path);
You are instructing MATLAB to use the specified Python interpreter. However, the initial output indicates that the environment is not loaded:
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Users/c1034944/ANALYSIS/fooof_venv/bin/python"
Library:
"/Library/Developer/CommandLineTools/Library/Frameworks/
Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/Users/c1034944/ANALYSIS/fooof_venv"
Status: NotLoaded
This means that while you have specified the Python executable, MATLAB has not yet initialized the environment.
Why Must You Run a Python Command?
The reason you need to execute a Python command (like py.list({'This','is a','list'})) to load the environment is that MATLAB only initializes the Python environment upon the first call to a Python function. This behavior is by design, as it allows MATLAB to ensure that the Python environment is correctly set up before executing any commands. After running a Python command, the environment status changes to "Loaded," confirming that MATLAB has successfully initialized the Python interpreter.
Environment Variables: PYTHONHOME and PYTHONPATH
You mentioned that both getenv('PYTHONHOME') and getenv('PYTHONPATH') return empty values. This is expected behavior when using a virtual environment, as these variables are typically set to point to specific installations of Python and its libraries. In a virtual environment, the paths are managed internally, and you generally do not need to set these variables manually.
- PYTHONHOME: This variable is used to specify the location of the standard Python libraries. In a virtual environment, it is usually not set, as the environment itself contains the necessary libraries.
- PYTHONPATH: This variable is used to specify additional directories that Python should add to the module search path. Since you have removed it from your bash_profile, it should not interfere with your virtual environment.
PATH Configuration
Your PATH variable appears to be correctly set up to include the virtual environment's bin directory:
export PATH="/Users/c1034944/ANALYSIS/fooof_venv/bin:$PATH"
This ensures that when you run MATLAB from the terminal, it can access the Python executable within your virtual environment.
Testing NumPy Functionality
You have successfully tested NumPy outside of MATLAB, confirming that it is installed and functioning correctly:
import numpy as np print(np.__version__)
The output indicates that NumPy is indeed available in your virtual environment. This is a positive sign that the environment is set up correctly.
My recommendations are listed below.
Loading the Environment: Continue to use a Python command to load the environment in MATLAB. This is standard behavior and not indicative of a problem.
Environment Variables: Do not worry about PYTHONHOME and PYTHONPATH being empty; this is typical for virtual environments.
MATLAB Execution: Ensure that you always start MATLAB from the terminal where the virtual environment is activated to maintain the correct context.
If you continue to experience issues, consider checking for any specific compatibility problems between MATLAB and the version of Python or NumPy you are using. Additionally, ensure that your MATLAB version supports the Python version you are working with.
If you have further questions or need additional assistance, feel free to ask. Your setup seems well on its way to functioning correctly, and with a few adjustments, you should be able to run your analyses seamlessly.
Answers (1)
0 votes
Hi @Vyte Jan,
After going through your comments and reviewing the documentations provided at the links below,
https://www.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for-python.html
https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html
https://www.mathworks.com/matlabcentral/answers/427187-problem-with-python-numpy?s_tid=mwa_osa_a
https://www.mathworks.com/help/matlab/matlab_external/passing-data-to-python.html
Please see suggested solutions listed below.
Verify NumPy Installation
- First, ensure that NumPy is indeed installed in your specified Python environment. You can do this by activating your environment and running:
python -m pip show numpy
- If it's not installed, you can install it using:
python -m pip install numpy
Check Python Environment Path
- Make sure that MATLAB is pointing to the correct Python executable. You’ve already set this using:
pyenv("Version", python_env_path);- Confirm that the environment is loaded properly by checking `pyenv` output after this command.
Using py.importlib.import_module
- Before calling any NumPy functions, explicitly import NumPy in MATLAB:
np = py.importlib.import_module('numpy');Then, try calling NumPy functions like so:
freq = [0 0.9766 1.9531 2.9297];
py_array = np.array(freq);Direct Conversion Alternatives
- If you continue to face issues with `py.numpy.array()`, consider converting your data into a compatible format without direct reliance on NumPy. For instance, you can convert MATLAB arrays into Python lists:
freq_list = py.list(num2cell(freq)); % Convert MATLAB array to cell array
then to Python listPlease see attached.

Use of pyrun or pyrunfile Functions
- If you have a more complex script or if you need to execute multiple lines of Python code, consider using `pyrun` or `pyrunfile` functions which allow you to run entire scripts and handle outputs effectively.
Also, please review the documentations provided in the links above as well.
Hope following these suggestions should help resolve your problems. Please let me know if you have any further questions.
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!