# Using FEniCS with python venv

**URL:** <https://fenicsproject.discourse.group/t/using-fenics-with-python-venv/4887>\
**Category:** Uncategorized\
**Created:** [January 12, 2021, 4:12pm UTC](https://fenicsproject.discourse.group/t/using-fenics-with-python-venv/4887 "2021-01-12T16:12:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CastriMik](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/castrimik/32/2069_2.png) [@CastriMik](https://fenicsproject.discourse.group/u/CastriMik)\
**Post date:** [January 12, 2021, 4:12pm UTC](https://fenicsproject.discourse.group/t/using-fenics-with-python-venv/4887/1 "2021-01-12T16:12:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 12, 2021, 4:22pm UTC](https://fenicsproject.discourse.group/t/using-fenics-with-python-venv/4887/2 "2021-01-12T16:22:28Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![CastriMik](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/castrimik/32/2069_2.png) [@CastriMik](https://fenicsproject.discourse.group/u/CastriMik)\
**Post date:** [January 13, 2021, 9:36am UTC](https://fenicsproject.discourse.group/t/using-fenics-with-python-venv/4887/3 "2021-01-13T09:36:13Z")

</div>

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:

2. I copied the sample file poisson.py that I’ve found at [this git repository](https://github.com/hplgit/fenics-tutorial/blob/master/src/vol1/python/poisson.py) 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](https://fenicsproject.org/download/) (with the venv activated):

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:

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

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 13, 2021, 9:51am UTC](https://fenicsproject.discourse.group/t/using-fenics-with-python-venv/4887/4 "2021-01-13T09:51:03Z")

</div>

> [@CastriMik](#):
>
> FEniCS was installed by using the commands for ubuntu found at [this link](https://fenicsproject.org/download/) (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`

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](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/)  
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.

---

<div class="post-metadata">

**Author:** ![CastriMik](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/castrimik/32/2069_2.png) [@CastriMik](https://fenicsproject.discourse.group/u/CastriMik)\
**Post date:** [January 13, 2021, 10:06am UTC](https://fenicsproject.discourse.group/t/using-fenics-with-python-venv/4887/5 "2021-01-13T10:06:53Z")

</div>

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

Michele
