I am trying to use python 3.6 within the MATLAB interpreter. I have both Python 2.7 and 3.6 installed on my machine. When using the default python 2.7 MATLAB work fine:
>> pyenv('Version', '/usr/bin/python')
ans =
PythonEnvironment with properties:
Version: "2.7"
Executable: "/usr/bin/python"
Library: "libpython2.7.so.1.0"
Home: "/usr"
Status: NotLoaded
ExecutionMode: InProcess
>> py.sys.version
ans =
Python str with no properties.
2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
However when following MATLAB's directions to change versions of python by entering the path of the python 3 executable file, python will not load:
>> pyenv('Version', '/opt/rh/rh-python36/root/usr/bin/python3.6')
ans =
PythonEnvironment with properties:
Version: "3.6"
Executable: "/opt/rh/rh-python36/root/usr/bin/python3.6"
Library: ""
Home: "/opt/rh/rh-python36/root/usr"
Status: NotLoaded
ExecutionMode: InProcess
>> py.sys.version
Unable to resolve the name py.sys.version.
I do notice that the library field in pyenv is not filled in for python 3. So it appears that MATLAB cannot find a python 3 shared object file. Why would MATLAB have problems finding the libpython shared object file?
For me installing libpython3.6 solved this problem, i.e. sudo apt install libpython3.6 on my system. Now the Library field in pyenv is populated and I am able to run python commands.
First of all leave python version installation over to anaconda when it comes to matlab.. and work from within python with your matlab scripts. The next steps to get your python/matlab combo (or visa versa) up and running:
conda install matlab_engine
import matlab.engine
If you hit the bumper:
PackageNotFoundError # (it doesn't install).
Open the Anaconda prompt (be sure you are in desired python version environment (e.g. not base but matpy37):
a) cd matlabroot\extern\engines\python or e.g. cd /Applications/MATLAB_R2019a.app/extern/engines/python on MacOS. (# change to your matlab version if needed).
b.1) and type and run python setup.py install or in some cases bullet b2).
b.2) python setup.py install --prefix "<anaconda installation directory>".
If you don't have for example the proposed conda matpy37 test environment you can create it as followed:
i. conda create -n myenv python=3.6.9 # where e.g. myenv = matpy37. When install done then:
ii. type conda activate matpy37 and run from e.g. Ipython your matlab script by starting it form Jupyter Notebook (Windows: start -> find folder anaconda -> icon Jupyter Notebook (matpy37). Not sure where they hide it in your linux/ubuntu OS.
... in the worse case scenario there are two options (4 and 5):
Don't install the python versions via pip but via anaconda if you want to stay away from breaking package dependencies, fixing callbacks, loggers, diving into subprocess errors and exhausting manual updates and/or down-grades due to package incompatabilities, and more...
Why anaconda:
You can save your specific environment and its package version into a yml-file and recreate it on another PC/Laptop/OS system. The .ylm is in most cases less than 1Mb. So it fits on an usb-stick (some of us call it.. a thumb-drive ;-).
... and possible a last resort (never had to use this myself):
matlab package condalab. Its used for easy switch within matlab.
Related
I'm trying to bazel build a target inside a virtualenv, and I'm coming across the following error:
Use --sandbox_debug to see verbose messages from the sandbox
Error occurred while attempting to use the default Python toolchain (#rules_python//python:autodetecting_toolchain).
According to '/home/vagrant/venv/bin/python -V', version is 'Python 3.7.5', but we need version 2. PATH is:
/home/vagrant/venv/bin:/home/vagrant/.local/bin:/home/vagrant/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/vagrant/bin
Please ensure an interpreter with version 2 is available on this platform as 'python2' or 'python', or else register an appropriate Python toolchain as per the documentation for py_runtime_pair (https://github.com/bazelbuild/rules_python/blob/master/docs/python.md#py_runtime_pair).
Note that prior to Bazel 0.27, there was no check to ensure that the interpreter's version matched the version declared by the target (#4815). If your build worked prior to Bazel 0.27, and you're sure your targets do not require Python 2, you can opt out of this version check by using the non-strict autodetecting toolchain instead of the standard autodetecting toolchain. This can be done by passing the flag `--extra_toolchains=#rules_python//python:autodetecting_toolchain_nonstrict` on the command line or adding it to your bazelrc.
I tried the --extra_toolchains=#rules_python//python:autodetecting_toolchain_nonstrict flag, but the build still failed after, so I thought I'd try building with python2 involved.
Current state of my system:
Inside virtualenv:
python and python3 seems to resolve to version 3.7.5
python2 doesn't resolve to anything
python2.7 resolves to python 2.7.17
Outside virtualenv:
python resolves to 2.7.17
python3 resolves to 3.7.5
python2 doesn't resolve to anything
python2.7 resolves to python 2.7.17
I want either python or python2 in my virtualenv to resolve to 2.7.17 as well. (I think maybe python2 is safer? I worry changing python can have a lot of other side-effects). For changing python I tried the "sudo update-alternatives" command several times, but it seems to already be pointing to the right thing. It seems like virtualenv just isn't picking it up. Any help would be much appreciated.
Thanks!
Problem having two versions of python on my Mac and them interacting. I have the following python locations
python is /anaconda3/envs/fenicsproject/bin/python
python is /usr/bin/python
and when I try to run a script. I get the following error:
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
I have googled some solutions and have found some posts saying I should try
env PYTHON_CONFIGURE_OPTS="--enable-framework" #or
env PYTHON_CONFIGURE_OPTS="--enable-shared"
This is they type of code I try to run
#Import packages
import dolfin as dl
I installed the env fenics by following the directions here
The google search possibly found Homebrew + Python on mac os x 10.8: Fatal Python error: PyThreadState_Get: no current thread importing mapnik however I was unable to find the library that links to a wrong version of python using otool.
I also found https://github.com/enthought/mayavi/issues/552 which suggests pinning to a different version of python.
Based on the install notes for hippy, https://hippylib.readthedocs.io/en/latest/installation.html then where they say conda create -n fenicproject ... you need to substitute the following:
conda create -n fenicsproject python==3.5.1
conda install -n fenicsproject -c conda-forge fenics==2017.2.0 \
mpi4py matplotlib scipy sympy==1.1.1 jupyter
After this, python -c 'import dolfin' no longer fails. It might also be possible to use a later version of python (I only tried 3.5.1).
What OS are you using? That will largely determine the specifics of how to go about correcting this issue, but the key here is which Python version the system path points to and what Python version your IDE is pointing to.
What you largely want to avoid is a situation where you are running Python scripts via your native system Python (2, likely).
Check which version of Python your IDE is using (from: How do I check what version of Python is running my script?):
import sys
print(sys.version)
Is the first number a 2? Did you want to use Python 2?
Next, let's check what version your system currently defaults to. If Ubuntu/Linux, use:
python -V
Is this expected? If not, you may need to change your system environmental variables to point to the correct Python version. The solution to this is OS dependent. If Windows, search "Edit Environmental Variables for Your Account" -> "Environmental Variables" -> "Path" , be sure it either points to Anaconda or the correct Python version; if Ubuntu/Linux, check your .bashrc file:
gedit ~/.bashrc
to see if the system points to the correct Python variable. If using a Mac, I formally apologize.
I'm getting 'module' object has no attribute 'Vector3' error in my simple python code.
This is rightAngle.py file
import meep as mp
import math
cell = mp.Vector3(16, 8, 0)
geometry = [mp.Bloack(mp.Vector3(1e20, 1, 1e20),
center = mp.Vector3(0, 0),
material = mp.Medium(epsilon = 12))]
sources = [mp.Source(mp.ContinuousSource(frequency = 0.15),
component = mp.Ez,
center = mp.Vector3(-7, 0))]
pml_layers = [mp.PML(1.0)]
resolution = 10
Compling using:
python rightAngle.py >& rightAngle.out
And getting this output:
Traceback (most recent call last):
File "rightAngle.py", line 4, in <module>
cell = mp.Vector3(16, 8, 0)
AttributeError: 'module' object has no attribute 'Vector3'
All I'm doing is copy and pasting given code from this fairly reliable source but getting error in Vector3 object.
Python version:
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
I don't know what I'm missing?
Meep Documentation:
https://meep.readthedocs.io/en/latest/Python_Tutorials/Basics/
Got the same problem while trying to use it on Ubuntu 16.04.
What worked for me was installing Anaconda for python 2.7 and create their recommended environment using:
conda create -n mp -c chogan -c defaults -c conda-forge pymeep
No Vector3 problem after that.
My environment script is below but I won't go into great detail here. My plan is to create a git repository based on my current working setup and make that public on Github which will force me to organize the whole setup and make it useful for others.
The following script allows me to run Meep with python3 without Anaconda3. This way you can use the latest packages and git source to build Meep, Open MPI and supporting libraries. My home directory is replaced by
#!/bin/bash
export MPI_PROJ=/home/<USER>/projects/mpi
export MPI_BASE=/usr/local/mpi/openmpi/base
export MPI_411=$MPI_BASE/411
#
# CRITICAL ENVARS - THESE MUST BE SET AS FOLLOWS FOR THE EXISTING MEEP, MPB AND OPENMPI 4.1.1
#
export LD_LIBRARY_PATH="/usr/local/mpi/meep/latest/lib:/usr/local/mpi/openmpi/base/411/bin:/home/fraser/.local/bin:/usr/local/bin:/usr/bin:/opt/bin:/bin:/sbin:/home/fraser/projects/mpi/bin"
export CFLAGS="-I/usr/local/mpi/meep/latest/include -I/usr/local/mpi/mpb/latest/include -I/usr/include/python3.8"
export PKG_CONFIG_PATH="/usr/local/mpi/meep/latest/lib/pkgconfig:/usr/local/mpi/openmpi/base/411/lib/pkgconfig:/usr/lib/x86_64-linux-gnu/pkgconfig:/usr/local/mpi/meep/latest/lib/pkgconfig"
export LDFLAGS="-L/usr/local/mpi/openmpi/base/411/lib -L/usr/local/mpi/meep/latest/lib -lmpi -lmpb"
export PYTHONPATH="/home/fraser/.local/lib/python3.8/site-packages:/usr/lib/python3.8:/usr/local/mpi/meep/latest/lib/python3.8/site-packages:/usr/local/lib/python3.8/dist-packages"
export PATH=/home/fraser/projects/mpi/bin:/usr/local/mpi/meep/latest/bin:/usr/local/mpi/openmpi/base/411/bin:/home/fraser/.local/bin:/usr/local/bin:/usr/bin:/opt/bin:/bin:/sbin
Open MPI 4.1.1 was built many times first to get it right on 3 real machines and 2 virtual machines. In the end all 3 Centos 8 Stream installations ran mpi flawlessly together. Two ubuntu 21 machines (one real, one vm) were able to run mpi together flawlessly. Centos 8 machines would talk to Ubuntu 21 machines. I could not copy the mpi installation between Ubuntu 21 and Centos 8 Stream due to glibc version differences.
Then moved on to Meep which required all the libraries listed in their "Building Meep From Source" documentation page. Some were not available from Mint 20 repos and had to be built from source. Build scripts were created to run configure for each one in order to record the options used.
Meep required specific builds of harminv, fftw3, guile, swig, hdf5. Other libraries were installed from the mint 20 package repositories using apt.
Meep was also built multiple times because my envrionment was to set right to run Meep from a location other than /usr/local. It took some tinkering to get the above environment variables close enough. They may need some tweaking for your purposes.
An intermediate update - I was able to duplicate my success on Mint20 Linux on Centos 8 Stream. Will gather all the working parts into a git repo and document the little details that nobody seems to post on here on StackOverflow or even on the Github issues! For example, export PY3=1 BEFORE RUNNING AUTOGEN in the swig git clone. Otherwise it wants to use python 2!
Another issue is: Save your clone as git-meep for example. Then copy that to meep. Then work on meep. If you use configure/make/make test/make install more than 5 times or any code or environment changes don't get noticed delete the meep tree. Make sure you save any environment-setting or configure-running scripts before deleting.
Copy the git-meep to meep and run autogen.sh again before running any environment-setting scripts. Then run those then run any scripts that run configure and/or make.
My configure scripts strictly run configure. Then I run make/make check/make install by hand, in case something fails to build.
There are more strange things to do that make this work.
Will post back here when the github repository is online.
Problem:
I'd like to install Pmw 2.0.0 (project page here) so that I can use it with tkinter in python3. The setup script from the package detects which version of python you're using and installs the version that is appropriate for your system (Ubuntu 15 in my case). I can't find any references to switches to make it install the 2.0.0 instead of 1.3.3(the Python 2.7 version), nor have I been able to get the script to install to the python3 libraries.
What I've done so far:
I've changed the python version detector in the setup script from
if sys.version_info[0]<3:
version='2.0.0' # really '1.3.3'
packages=['Pmw', 'Pmw.Pmw_1_3_3', 'Pmw.Pmw_1_3_3.lib',]
to
if sys.version_info[0]<2:
version='2.0.0' # really '1.3.3'
packages=['Pmw', 'Pmw.Pmw_1_3_3', 'Pmw.Pmw_1_3_3.lib',]
to attempt to force the installer to default to the python3 version, which it does, but it installs them in the python2.7 libraries (/usr/local/lib/python2.7/distpackages).
What I want to do:
I'm looking for a way to force the installer to put the 3.4-compatible package into the python3 libraries. If that means getting it to install both packages in their respective correct directories, that's fine, too. I'm stumped about what to try next.
Answered by RazZiel on AskUbuntu:
Link here.
Instead of using the command sudo python setup.py build and then sudo python setup.py install, I should have been using python3 to execute the setup script. I've managed to outthink myself pretty badly on this one.
I am trying to install OpenCV on my Ubuntu system. I followed the steps of this page. I am able to run the demos for C/C++ etc. But when I try to run the python demos, I get an ImportError: No module named cv2
Here is more info :
python --version
Python 2.7.8 :: Anaconda 2.1.0 (64-bit)
python -c 'import sys; print sys.path'
['', '/home/radar/anaconda/lib/python2.7/site-packages/pydy-0.2.1-py2.7.egg', '/home/radar', '/home/radar/opencv-2.4.9/modules/python/src2', '/home/radar/anaconda/lib/python27.zip', '/home/radar/anaconda/lib/python2.7', '/home/radar/anaconda/lib/python2.7/plat-linux2', '/home/radar/anaconda/lib/python2.7/lib-tk', '/home/radar/anaconda/lib/python2.7/lib-old', '/home/radar/anaconda/lib/python2.7/lib-dynload', '/home/radar/anaconda/lib/python2.7/site-packages', '/home/radar/anaconda/lib/python2.7/site-packages/PIL', '/home/radar/anaconda/lib/python2.7/site-packages/Sphinx-1.2.3-py2.7.egg', '/home/radar/anaconda/lib/python2.7/site-packages/runipy-0.1.1-py2.7.egg', '/home/radar/anaconda/lib/python2.7/site-packages/setuptools-5.8-py2.7.egg']
You are running Anaconda Python distribution, but you need to use native python to access openCV library. Run Python with specified path and it should work:
/usr/bin/python
Also note, that to use openCV with virtual environmet, you need to manually copy openCV running objects (cv.py, cv2.so) to the environment you are going to use from the dist-packages of your native python, default path is /usr/local/lib/python2.7/dist-packages/.