Using FEniCS with python venv

Hi all,

I’m new to FEniCS, and I’m trying to run some sample files I’ve found on the web. I’m using Ubuntu 20.04.1.

The sample program works fine if I do not use a Python virtual environment, but, as soon as I create a venv and I activate it, the terminal cannot recognize fenics as a module (ModuleNotFoundError: No module named ‘fenics’).

What am I doing wrong?

Thank you very much,

Michele

Could you elaborate a bit more on what commands works, how you have installed dolfin (and what actually works), and how you are creating a virtual environment and what modules you expect that should be installed in this environment (A virtual environment doesn’t carry over locally installed packages, as far as I am aware).

Hi Jorgen,

Thank you very much for your answer.

I followed these steps:

  1. I created a Python virtual environment in /home/user/Documents/Python called fenics-venv, and I activated it:

    python3 -m venv fenics-venv
    source fenics-venv/bin/activate

  2. I copied the sample file poisson.py that I’ve found at this git repository in the following path /home/user/Documents/Python/fenics-venv/my-fenics-project

  3. FEniCS was installed by using the commands for ubuntu found at this link (with the venv activated):

    sudo apt-get install software-properties-common
    sudo add-apt-repository ppa:fenics-packages/fenics
    sudo apt-get update
    sudo apt-get install --no-install-recommends fenics

  4. After opening the terminal directly from the folder /home/user/Documents/Python/fenics-venv/my-fenics-project (where the sample file is), I run the following command:

    python poisson.py

    By doing this, I get the following error:

    ModuleNotFoundError: No module named ‘fenics’

While, if I do not create an environment, everything works fine (the sample file run without any problem). It seems to me that FEniCS is not seen by the Python venv, but I was not able to understand why. This could have a trivial answer, since I’m a newbie of Ubuntu and FEniCS, so pardon me in that case.

If you need further details, please tell me.

Thank you very much again,

Michele

You cannot use sudo apt-get install to install packages in a venv, see: Install packages in a virtual environment using pip and venv - Python Packaging User Guide
You can see this by calling which pip3, which python and which apt-get in your venv to see what they point to.

You can also do pip3 list on your system and pip3 list in your venv to see the different packages that are installed.

As you have already installed fenics on your system, I see no need to create a venv.

Thank you again Jorgen. I will follow your advice, and I will use FEniCS as it is.

Michele