Trouble with Different Python Envrionments, Anaconda and FEniCS

I’m working on MacOs, and I am trying to run finite elements on Python using the FEniCS library.

I use Jupyter notebooks, and in the past, I’ve had to source my anaconda installation as follows, to run things well:

source activate ~/../my_user_name/anaconda3

I am not sure, but I think this activates a separate environment for Python installations managed by Anaconda.

To install FEniCS, I ran the following commands:

conda create -n fenicsproject -c conda-forge fenics
source activate fenicsproject

Now when I try to run a Python script or a Jupyter notebook, I no longer have access to the standard libraries such as matplotlib.
When I try to install matplotlib in this environment using pip3, I get the following message:

pip3 install matplotlib                                                                    
Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/site-packages (2.1.0)

Any ideas on how to resolve this issue, and could you help me manage these multiple environments/installations of Python?

When we call source activate ... it starts a Python virtual environment. The purpose of a virtual environment is to created an isolated Python set-up. So it is by intention that the Python packages you have installed at other places outside that virtual environment are not accessible from within it. I would except fenics to install matplotlib as a dependency but in any case you try to explicitly install it.

Your error message says that matplotlib is already installed in /usr/local/lib/python3.6/site-packages which means you are calling pip3 install matplotlib from a terminal where fenicsproject environment is not yet activated.

If you are trying to install matplotlib in the same environment as fenics try

conda install -n fenicsproject matplotlib
1 Like

A combination of installing the packages with pip in the environment and executing
pip install ipykernel --upgrade
python3 -m ipykernel install --user
worked for me. Thank you!

I had the same problem too. I solved it just by launching the jupyter notebook from a terminal instead of the anaconda navigator GUI which is bugged apparently. see also my post of Oct 18

Take care to note that manual pip installations can sometimes cause problems, introducing version inconsistencies where the pip version is used instead instead of the environment version of a module. It can confuse other dependent modules which then fail complaining about missing features.

Generally it’s more robust to use the environment installation tools to install modules. In the case of anaconda, that’s conda install, as @amit112amit suggested.