interfacing python 2.7.12 with matlab 2017a [duplicate] - python

I am trying to run Matlab code using Python. I tried to follow the instructions given on this Mathworks page.
When trying to import Matlab though Python, it was installed using pip install matlab.
However, importing matlab.engine gives the error No module named 'matlab.engine'; 'matlab' is not a package. It cannot be installed using pip install engine either.
How can I get the code running? The Python code I'm running is as below:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.triarea(nargout=0)
Python version - 3.5
Matlab version - 8.5.0.197613 (R2015a)

You need to install the Matlab Engine for Python, and it cannot be installed using pip. Try the instructions listed here. I have listed the instructions briefly below:
Make sure you have Python in your PATH.
Find the Matlab root folder. You can use the matlabroot command within Matlab to find it.
Go to the Matlab root folder in the command line.
cd "matlabroot\extern\engines\python" (In Windows)
python setup.py install

I was stuck on this for so long and I cant find a good explanation for it so here y'all go. There's a package for python called matlab here
And it has nothing to do with the matlab engine for python. When you pip install matlab it's installing this. I'm using pycharm and this is the default one it installed. I uninstalled this matlab and, instead, I copied the necessary information to my python project.
To do this, I located the folder named Matlab that is copied somewhere in AppData when you run python setup.py install in matlabroot/extern/engines/python and copied it to the lib folder in the venv of my python project since I'm using the virtual environment interpreter for pycharm.

pip install matlab gives you this, which installs a module with
from numpy import *
from pylab import *
inside. I'm quite sure this is not what you wanted...
I guess you tried importing the Matlab Compiler Runtime for Python. This has to be installed with the respective software from Mathworks though, it doesn't come through Python package index / pip. Check out the instrucions on their site.

You can visit to MATLAB official documentation: https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html
I have copied here as well.
Install Python Engine for Multiple MATLAB Versions
You can specify a MATLAB version to run from a Python script by installing the MATLAB Python packages to version-specific locations. For example, suppose that you want to call either MATLAB R2019a or R2019b from a Python version 3.6 script.
From the Windows system prompt, install the R2019a package in a subfolder named matlab19aPy36:
cd "c:\Program Files\MATLAB\R2019a\extern\engines\python"
python setup.py install --prefix="c:\work\matlab19aPy36"
Install the R2019b package in a matlab19bPy36 subfolder:
cd "c:\Program Files\MATLAB\R2019b\extern\engines\python"
python setup.py install --prefix="c:\work\matlab19bPy36"
From a Linux system prompt:
cd "/usr/local/MATLAB/R2019a/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19aPy36"
cd "/usr/local/MATLAB/R2019b/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19bPy36"
From a Mac Terminal:
cd "/Applications/MATLAB_R2019a.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19aPy36"
cd "/Applications/MATLAB_R2019b.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19bPy36"

I too did the same. Installed matlab using
pip install matlab
and got the same error No module named 'matlab.engine'; 'matlab' is not a package.
Then I checked the official documentation for installing MATLAB Engine API for Python, and followed the installation steps from there.
For me,
pip install matlabengine
solved the issue!

Related

Why do I receive a "no module named scipy" error even after I have installed the module via pip?

I am using Python 3.9 on Windows 10 which I downloaded directly from the Microsoft Store.
I tried running a script in PowerShell: Bash *.sh
This script is supposed to tell my computer to execute a .py script which uses scipy.io and many other modules.
Then I received this error:
ModuleNotFoundError: No module named 'scipy'
My strategy was to make sure pip was up to date, then use it to install the desired packages, then run some commands to see if the packages were installed.
I ran this command to update pip:
python3 -m pip install --upgrade pip
I ran this command to get some modules:
python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
I also tried this command just in case:
pip install scipy
and got the result:
Requirement already satisfied ...
I ran the command pip list to make sure scipy was in the list (and it was there).
Then I ran the command python and my prompt changed to ">>>" and entered import scipy and did not receive any errors.
I am very confused as to how I have scipy installed yet have my script tell me it isn't there. Please help!
From what you have posted it looks like you have more than one python environment path in your system, because of which when you are installing these libraries they are installed at certain location while when you run the bash script it is using some other python location.
Try using these commands in both your terminal (cmd in windows) as well as in you bash script:
import sys
print(sys.path)
This will give you the python environment path (location where your python libraries are present), then compare both the path you get from your terminal as well as bash. Add the path you got from the terminal to your global environment in order to make sure the same python version is used everywhere.
You can also refer to this: Python modules not found over terminal but on python shell, Linux
I had the same issue. You might have multiple versions of python and you only installed scipy on the one you are not using
OR
you are using an IDE which has the option to use packages you install that are not by default in python. Pycharm has that. When you make a new project, it has a tick option saying "Inherit global site-packages" which means it will use additional packages you have installed any.

How to install pymssql to Python 3.4 rather than 2.7 on Ubuntu Linux?

I'm not overly familiar with Linux and am trying to run a Python script that is dependent upon Python 3.4 as well as pymssql. Both Python 2.7 and 3.4 are installed (usr/local/lib/[PYTHON_VERSION_HERE]). pymssql is also installed, except it's installed in the Python 2.7 directory, not the 3.4 directory. When I run my Python script (python3 myscript.py), I get the following error:
File "myscript.py", line 2, in
import pymssql
ImportError: No module named 'pymssql'
My belief is that I need to install pymssql to the Python 3.4 folder, but that's my uneducated opinion. So my question is this:
How can I get my script to run using Python 3.4 as well as use the pymssql package (sorry, probably wrong term there)?
I've tried many different approaches, broken my Ubuntu install (and subsequently reimaged), and at this point don't know what to do. I am a relative novice, so some of the replies I've seen on the web say to use ENV and separate the versions are really far beyond the scope of my understanding. If I have to go that route, then I will, but if there is another (i.e. easier) way to go here, I'd really appreciate it, as this was supposed to just be a tiny thing I need to take care of but it's tied up 12 hours of my life thus far! Thank you in advance.
It is better if when you run python3.4 you can have modules for that version.
Another way to get the desire modules running is install pip for python 3.4
sudo apt-get install python3-pip
Then install the module you want
python3.4 -m pip install pymssql
The easiest way is to use virtual environments instead of system paths or environment scripts. See official Python package installation guide.
All you need to do is to
# Create fresh Python environemnt
virtualenv -p python3.4 my-venv
# Activate it in current shell
source my-venv/bin/activate
# Install packages
pip install mysqlclent
Note that mysqlclient is Python 3.x compatible version.

How to install the fitsio package in python

I am trying to install the fitsio package to python. I have tried so using pip, but that resulted in the following error: "ValueError: could not configure cfitsio 3370". I have also tried to install it without pip, but it gave me the same error. Any advise on how to install it properly? I don't have a lot of python experience, so please keep it simple. Thanks!
According to the setup.py source code of the python fitsio package, the package is configured and compiled using os.system('sh ./configure') (line 40) and os.system('make') (line 47) respectively. This is expected since the python fitsio package is basically a python wrapper of the C version of the fitsio library.
These are Linux commands, which you are trying to run natively at the Windows Command Prompt. This can't work and that's why you get the errors you're getting.
To get the python fitsio package installed on Windows you need to use something like Cygwin. Be sure to include the make and gcc in the packages you install in Cygwin (they are usually installed by default), as the fitsio package you want requires a GNU Toolchain for compiling the cfitsio. Another option would be to switch to Linux or use a Linux virtual machine but that's going a long way just to get a library to work :)
After installing the package you can find some examples for the fitsio module in python at the github repository landing page of the package.

Install sympy for python34 in windows 8

I download sympy-0.7.6. Now it is in download directory. I install python in C drive (c:\Python34).
Now how to install the sympy. There is no .exe file in sympy directory. But have a setup.py.
My OS is windows 8.
How I install or use sympy for python 3.4 in windows 8 64 bit system.???
I downloaded the sympy for perform some integration and other mathematical operations.
Please help...
thanks.
I successfully installed sympy in window 7 machine from source code.
download the zip from https://github.com/sympy/sympy
or
git clone https://github.com/sympy/sympy.git
cd sympy
python setup.py install
You have to run python setup.py install in the sympy's folder that you have download (in your download directory). Installation instructions are at https://github.com/sympy/sympy#installation
This will work with python3 as well.
But I think that you are facing problem in running python commands. You must be able to run python to install and use Sympy.
Another option is to install Anaconda, which comes with SymPy (as well as tons of other useful Python libraries and the IPython notebook). The latest version of Anaconda comes with an older version, so you'll want to open a command prompt after you install it and run
conda update sympy
to get the latest version.

How to install pymorph on windows?

I am very new to python so sorry if this is a trivial question. I am developing on windows and I want to install the following library pymorph
I know it can be installed on linux but how to install it on windows enviroment ?
Thanks
Download the package. Extract the package, cd to the folder (make sure setup.py is in the folder) and run python setup.py install from your cmd while in said folder.
There are 2 main ways to install a python dependency.
First method
You can install pymorph (and almost all python dependencies) with the easy_install software. You can retrieve it here.
Example :
Run the setuptools-0.6c11.win32-py2.7.exe if you are using python version 2.7.
After that, you have the easy_install executable.
To install pymorph (in a shell, using cmd.exe) :
easy_install-2.7 pymorph
Don't forget to adapt the name of the easy_install executable with the good python version.
The advantage of this method is that it uses the Python Package Index :
According to Wikipedia :
The Python Package Index or PyPI is the official third-party software repository for the Python programming language. Python developers intend of it to be a comprehensive catalog of all open source Python packages.1 It is analogous to CPAN, the repository for Perl. Package managers such as EasyInstall, pip and PyPM use PyPI as the default source for packages and their dependencies.
Second method
In a shell, using the cmd.exe, go to the pymorph folder. After that, always in your shell :
python setup.py install
In a same way, python executable name can depend of the version of python. Example :
python2.7 for python version 2.7

Categories