Python with anaconda: spyder’s python is not conda’s python

This is a small lesson I learnt in the last days as I’m relatively new to managing my python environments with anaconda and started using it on different machines.

Background

I’m developing a small piece of software which reads data from a table. It could be a csv file, but more conveniently is to read directly form a spreadsheet file, in these cases libreoffice spreadsheets, ods files.

There exists odfpy for that end – even when it has not been updated for some time. After a few small tweaks it does its work as it should on my primary computer. Using anaconda3 to manage my python environments I created on my other computer a supposedly identical environment – but did so by hand w/o exporting the environment information from my primary development computer.

Problem

Yet when running my piece of software it always resulted in the IPython console of spyder with

ImportError: No module named 'odf'

So… why? The package is installed. I started spyder – and get this error. Wondering whether it’s a peculiarity of spyder’s IPython, I started that environment’s normal python in the console – and noticed that my programme worked! Now, that’s something to work on, an indeed the called python interpreter is a different one:

(py36) planetmaker@aeolusmaximus-vortex:~/idltools/BNE$ which python
/home/planetmaker/anaconda3/envs/py36/bin/python

in the „real“ python in my environment vs. anaconda’s default:

In [3]: import sys
In [4]: sys.executable
Out[4]: '/home/planetmaker/anaconda3/bin/python'

Solution

Changing the interpreter in spyder’s setting didn’t have the desired effect – but I tried to install IPython and spyder in that py36 environment – and yes! They happily installed – and the then newly-installed and started spyder from inside that environment did not exhibit the issue with not finding the odf package!

Lessons learnt:

1) proper use of anaconda’s environment export feature.

I could have avoided the whole issue, had I made use of it. Export your environment and import it on another by means of anaconda:

Old machine:
conda list --explicit > spec-file
New machine:
conda create --name myenv --file spec-file.txt

2) Always install spyder specifically in an environment.

Anaconda’s default spyder does not have access to all the same paths as a spyder installed in an environment

Bitte loggen Sie sich ein, damit Sie einen Kommentar hinterlassen können.