Issue with two versions of python - python

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.

Related

Change 'python' to point to python2.7 inside virtualenv

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!

Getting MATLAB R2019b to use Python3.6

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.

Setting python for RStudio under Anaconda

Can't change python being used when install_keras run from RStudio within Anaconda
I have several versions of Python installed on my Windows 10 box: Python 2.7, Python 3.6, plus the pythons that appear to have been installed with Anaconda. I have RStudio installed via Anaconda.
I am trying to use the keras library in my R code. I run 'install_keras()' and receive the following error:
Error: Error 1 occurred while checking for python architecture
In addition: Warning message:
running command '"C:\Python27\/python.exe" -c "import sys; import platform;
sys.stdout.write(platform.architecture()[0])"' had status 1
I have tried running install_keras using the method and conda settings:
install_keras(method="conda", conda="C:/Development/Anaconda35/Scripts")
But I still get the same error. I have also tried resetting the PATH and RETICULATE_PYTHON environmental variables, using Sys.setenv and use_python() and it doesn't seem to help.
Sys.which('PYTHON') does show the correct executable that I want to use.
Yes, my environment variables did have that Python set first in the path. But changing that didn't seem to help.
I have tried uninstalling and reinstalling keras, both the R library and the 'conda' (pip) python library. That didn't help.
I'm assuming at this point that there's some configuration or setting file with this set, but I can't seem to find it.
Suggestions?

IPython sys.path different from python sys.path

I generally use IPython and only recently noticed that the the search path for imports is wrong in the regular python shell. From what I understand, sys.path inherits from PYTHONPATH (although I don't know where PYTHONPATH lives), is this different in IPython? I'm worried that this effecting installations. For instance I just tried
pip install --upgrade gensim
which failed because it couldn't resolve the scipy dependency, which I already have installed.
So I dove a little bit deeper and found in Ipython
import gensim
gensim.__version__
returns .9.1
while in python
import gensim
gensim.__version__
returns .8.9
Here is what the Ipython version of sys.path looks like:
['',
'/Users/change/anaconda/bin',
'/Users/change/anaconda/lib/python2.7/site-packages/pybing-0.12-py2.7.egg',
'/Users/change/anaconda/lib/python2.7/site-packages/httplib2-0.8-py2.7.egg',
'/Users/change/anaconda/python.app/Contents/lib/python27.zip',
'/Users/change/anaconda/python.app/Contents/lib/python2.7',
'/Users/change/anaconda/python.app/Contents/lib/python2.7/plat-darwin',
'/Users/change/anaconda/python.app/Contents/lib/python2.7/plat-mac',
'/Users/change/anaconda/python.app/Contents/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/change/anaconda/python.app/Contents/lib/python2.7/lib-tk',
'/Users/change/anaconda/python.app/Contents/lib/python2.7/lib-old',
'/Users/change/anaconda/python.app/Contents/lib/python2.7/lib-dynload',
'/Users/change/anaconda/lib/python2.7/site-packages',
'/Users/change/anaconda/lib/python2.7/site-packages/PIL',
'/Users/change/anaconda/lib/python2.7/site-packages/pygoogle',
'/Users/change/anaconda/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',
'/Users/change/anaconda/lib/python2.7/site-packages/IPython/extensions']
and the regular python script sys.path:
['',
'/Users/change/anaconda/lib/python2.7/site-packages/pybing-0.12-py2.7.egg',
'/Users/change/anaconda/lib/python2.7/site-packages/httplib2-0.8-py2.7.egg',
'/Users/change/anaconda/lib/python27.zip',
'/Users/change/anaconda/lib/python2.7',
'/Users/change/anaconda/lib/python2.7/plat-darwin',
'/Users/change/anaconda/lib/python2.7/plat-mac',
'/Users/change/anaconda/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/change/anaconda/lib/python2.7/lib-tk',
'/Users/change/anaconda/lib/python2.7/lib-old',
'/Users/change/anaconda/lib/python2.7/lib-dynload',
'/Users/change/anaconda/lib/python2.7/site-packages',
'/Users/change/anaconda/lib/python2.7/site-packages/PIL',
'/Users/change/anaconda/lib/python2.7/site-packages/pygoogle',
'/Users/change/anaconda/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Probably too late for you now, but the answer here may help you too: ipython reads wrong python version . Basically the ipython script can directly reference a specific python binary rather than the one that you'd get if you just ran python directly.
I had a brew installed version of Python on my mac. For some reason that couldn't use the system libraries. After brew uninstall python it worked again, because the default python was switched back to /usr/bin/python.
Also playing around with which python, which ipython and opening up /usr/bin/python, /usr/local/bin/python, /usr/bin/ipython and /usr/local/bin/ipython and doing the imports there might help you find out where it is exactly going wrong.
You may have your virtual environment activated which is ignoring the system path.
It can be fixed by running this in terminal (After closing notebook etc):
virtualenv --system-site-packages ~

How can I update Python on Mac OS and have RubyGems use the new version?

I am having serious issues trying to download the "therubyracer" gem. I think the problem is that I do not have a working libv8 library to create the gem.
I think I do not have a working libv8 because my RubyGems keeps trying to use "Python 2.5.6", which is too old to gem install libv8. I tried countless ways to update the Python that RubyGems uses, however, I cannot figure it out.
Here is what I have done thus far. I went to http://www.python.org/download/releases/3.3.2/ and downloaded "(Mac OS X 64-bit/32-bit Installer (3.3.2) for Mac OS X 10.6 and later 2.)".
Upon completion of the download, I ran:
gem install libv8 --version 3.11.8.17
Yet, I still received the same error. I noticed the output still said:
"Using Python 2.5.4"
Despite downloading a newer Python version, it is not being used. So how do I change the version of Python that the "gem" command uses? I searched and discovered "virtualenv," which appears to be a good Python manager, however I am a bit at a dead end now. Any help or suggestion?
If libv8 uses the python command from the command line, this should apply:
If you do not care what python2 is installed, you can do this in the command line:
type python # this gives you the path of the python command
python is /usr/bin/python
type python2.7
python is /usr/bin/python2.7
Then you can copy one over the other
cp /usr/bin/python2.7 /usr/bin/python
You can safely copy python2.7 there because it is compatible with older versions.
Suggestions?

Categories