No module named 'dolphin' mac m1

Hi all,

I’m trying to get fenics to work on my m1 mac using conda.
Following this link from the fenics homepage, I ran:

conda create -n fenicsx-env
conda activate fenicsx-env
conda install -c conda-forge fenics-dolfinx mpich pyvista

Then, I activate using conda activate fenicsx-env
(I’ve also tried using the more outdated source activate to no avail)
Trying to run any of the example fenics code, say python3 demo_poisson.py always leads to the following error:

File "/Users/myname/Downloads/demo_poisson.py", line 37, in <module>
    from dolfin import *
ModuleNotFoundError: No module named 'dolfin'

I’ve wondered if the resolution here could be related to my issue. Although the environment has loaded, os.environ['PATH'] returns:

/Users/myname/opt/anaconda3/envs/fenicsx-env/bin:/Users/myname/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin

Which isn’t the same as what is is in that post,

I’ve looked through the other posts on here with similar issues but the things that have worked for them don’t appear to be working for me. I feel a bit silly as I’ve been using python for many years and am yet to have such an issue installing something… Any insight would be lovely.

Thanks!

Hi,

you are using the command from the legacy fenics. To import dolfinx do

from dolfinx import *

which I don’t really recommend. You can refer to this excellent tutorial on how to use dolfinx from this link

https://jsdokken.com/dolfinx-tutorial/

1 Like

You have installed FEniCSx on conda. The FEniCSx tutorial is available here- The FEniCSx tutorial — FEniCSx tutorial. There are more demos available here- Demos — DOLFINx 0.6.0.0 documentation.
The code:

from dolfin import *

is actually from Legacy FEniCS. You can also install Legacy FEniCS on conda from here- https://fenicsproject.org/download/archive/. Legacy FEniCS demos are available here- https://fenicsproject.org/olddocs/dolfin/1.6.0/python/demo/index.html

Hi,

I followed the same installation procedure (conda install on M1 mac) and it went smoothly.
But I get the same error (for dolfinx though):

  File "/Users/fwaharte/Downloads/demo_poisson.py", line 72, in <module>
    from dolfinx import fem, io, mesh, plot
ModuleNotFoundError: No module named 'dolfinx'

I don’t know how to make it work.
Is it something with the path? Because it is installed somewhere…

Any suggestion would be greatly appreciated :pray:

Could you list the packages in the environment you created? Similar to No module named 'dolfin' on Jupyter notebook - #14 by Elizabeth_Hawkins

Sure!

Here is the output of the commande

conda list -n fenicsx-env
# packages in environment at /Users/fwaharte/mambaforge3/envs/fenicsx-env:
#
# Name                    Version                   Build  Channel
appdirs                   1.4.4              pyhd3eb1b0_0  
blas                      2.116                  openblas    conda-forge
blas-devel                3.9.0           16_osxarm64_openblas    conda-forge
blosc                     1.21.3               h1d6ff8b_0    conda-forge
boost-cpp                 1.74.0               h32e41df_4    conda-forge
brotlipy                  0.7.0           py310h1a28f6b_1002  
bzip2                     1.0.8                h620ffc9_4  
c-ares                    1.18.1               h1a28f6b_0  
ca-certificates           2022.12.7            h4653dfc_0    conda-forge
certifi                   2022.12.7          pyhd8ed1ab_0    conda-forge
cffi                      1.15.1          py310h80987f9_3  
charset-normalizer        2.0.4              pyhd3eb1b0_0  
cryptography              38.0.4          py310h834c97f_0  
curl                      7.87.0               h80987f9_0  
double-conversion         3.1.5                hc377ac9_1  
eigen                     3.3.7                h525c30c_1  
expat                     2.4.9                hc377ac9_0  
fenics-basix              0.5.1           py310h2887b22_1    conda-forge
fenics-dolfinx            0.5.2           py310hd290994_100    conda-forge
fenics-ffcx               0.5.0              pyhb871ab6_1    conda-forge
fenics-libbasix           0.5.1                hf081368_1    conda-forge
fenics-libdolfinx         0.5.2              h4bbba74_100    conda-forge
fenics-ufcx               0.5.0                hb871ab6_1    conda-forge
fenics-ufl                2022.2.0           pyhd8ed1ab_0    conda-forge
ffmpeg                    4.2.2                h04105a8_0  
fftw                      3.3.10          mpi_mpich_h0cb5807_6    conda-forge
flit-core                 3.6.0              pyhd3eb1b0_0  
freetype                  2.12.1               h1192e45_0  
gettext                   0.21.0               h826f4ad_0  
giflib                    5.2.1                h80987f9_3  
gl2ps                     1.4.2                h1192e45_1  
glew                      2.1.0                hc377ac9_3  
gmp                       6.2.1                hc377ac9_3  
gnutls                    3.6.15               h887c41c_0  
hdf4                      4.2.13               h5e329fb_3  
hdf5                      1.12.1          mpi_mpich_h102acf4_4    conda-forge
hypre                     2.25.0          mpi_mpich_h060cf3c_0    conda-forge
icu                       68.1                 hc377ac9_0  
idna                      3.4             py310hca03da5_0  
imageio                   2.19.3          py310hca03da5_0  
jpeg                      9e                   h1a28f6b_0  
jsoncpp                   1.9.4                h525c30c_2  
krb5                      1.19.4               h8380606_0  
lame                      3.100                h1a28f6b_0  
lcms2                     2.12                 hba8e193_0  
lerc                      3.0                  hc377ac9_0  
libadios2                 2.8.3           mpi_mpich_h903a2e2_1    conda-forge
libblas                   3.9.0           16_osxarm64_openblas    conda-forge
libcblas                  3.9.0           16_osxarm64_openblas    conda-forge
libcurl                   7.87.0               h0f1d93c_0  
libcxx                    14.0.6               h848a8c0_0  
libdeflate                1.8                  h1a28f6b_5  
libedit                   3.1.20221030         h80987f9_0  
libev                     4.33                 h1a28f6b_1  
libffi                    3.4.2                hca03da5_6  
libgfortran               5.0.0           11_3_0_hca03da5_28  
libgfortran5              11.3.0              h009349e_28  
libiconv                  1.16                 h1a28f6b_2  
libidn2                   2.3.1                h1a28f6b_0  
liblapack                 3.9.0           16_osxarm64_openblas    conda-forge
liblapacke                3.9.0           16_osxarm64_openblas    conda-forge
libnetcdf                 4.8.1                h1b8e529_2  
libnghttp2                1.46.0               h95c9599_0  
libogg                    1.3.5                h1a28f6b_1  
libopenblas               0.3.21          openmp_hc731615_3    conda-forge
libopus                   1.3                  h1a28f6b_1  
libpng                    1.6.37               hb8d0fd4_0  
libsodium                 1.0.18               h27ca646_1    conda-forge
libsqlite                 3.40.0               h76d750c_0    conda-forge
libssh2                   1.10.0               hf27765b_0  
libtasn1                  4.16.0               h1a28f6b_0  
libtheora                 1.1.1                h1a28f6b_3  
libtiff                   4.5.0                h313beb8_1  
libunistring              0.9.10               h1a28f6b_0  
libvorbis                 1.3.7                h1a28f6b_0  
libvpx                    1.10.0               hc377ac9_0  
libwebp                   1.2.4                ha3663a8_1  
libwebp-base              1.2.4                h80987f9_1  
libxml2                   2.9.14               h8c5e841_0  
libzip                    1.8.0                h0c481fb_0  
libzlib                   1.2.13               h03a7124_4    conda-forge
llvm-openmp               14.0.6               hc6e5704_0  
loguru                    0.5.3           py310hca03da5_4  
lz4-c                     1.9.4                h313beb8_0  
metis                     5.1.0             h9f76cd9_1006    conda-forge
mpfr                      4.1.0                h6d7a090_1    conda-forge
mpi                       1.0                       mpich  
mpi4py                    3.1.4           py310hb6d4c64_0    conda-forge
mpich                     4.0.3              hd4b5bf3_100    conda-forge
mumps-include             5.2.1               hce30654_11    conda-forge
mumps-mpi                 5.2.1               hfd295ca_11    conda-forge
ncurses                   6.4                  h313beb8_0  
nettle                    3.7.3                h84b5d62_1  
numpy                     1.23.5          py310hb93e574_0  
numpy-base                1.23.5          py310haf87e8b_0  
openblas                  0.3.21          openmp_hf78f355_3    conda-forge
openh264                  1.8.0                h98b2900_0  
openssl                   1.1.1t               h03a7124_0    conda-forge
packaging                 22.0            py310hca03da5_0  
parmetis                  4.0.3             hefa2a9d_1005    conda-forge
petsc                     3.17.3          real_hf852a44_100    conda-forge
petsc4py                  3.17.3          real_h09567b2_101    conda-forge
pillow                    9.3.0           py310h313beb8_2  
pip                       22.3.1          py310hca03da5_0  
pooch                     1.4.0              pyhd3eb1b0_0  
proj                      7.2.0                heac154c_1  
ptscotch                  6.0.9                hd02db47_2    conda-forge
pugixml                   1.11.4               hc377ac9_1  
pycparser                 2.21               pyhd3eb1b0_0  
pyopenssl                 22.0.0             pyhd3eb1b0_0  
pysocks                   1.7.1           py310hca03da5_0  
python                    3.10.8          hf452327_0_cpython    conda-forge
python_abi                3.10                    3_cp310    conda-forge
pyvista                   0.38.3             pyhd8ed1ab_0    conda-forge
readline                  8.2                  h1a28f6b_0  
requests                  2.28.1          py310hca03da5_0  
scalapack                 2.2.0                hb170938_1    conda-forge
scooby                    0.7.1              pyhd8ed1ab_0    conda-forge
scotch                    6.0.9                h7537618_2    conda-forge
setuptools                65.6.3          py310hca03da5_0  
slepc                     3.17.1          real_h84e8f0d_102    conda-forge
slepc4py                  3.17.1          real_hc78d41f_103    conda-forge
snappy                    1.1.9                h17c5cce_2    conda-forge
sqlite                    3.40.1               h7a7dc30_0  
suitesparse               5.10.1               h7cd81ec_1    conda-forge
superlu                   5.2.2                hc615359_0    conda-forge
superlu_dist              7.2.0                h7df07b8_0    conda-forge
tbb                       2021.7.0             h48ca7d4_0  
tbb-devel                 2021.7.0             h48ca7d4_0  
tk                        8.6.12               hb8d0fd4_0  
typing_extensions         4.4.0           py310hca03da5_0  
tzdata                    2022g                h04d1e81_0  
urllib3                   1.26.14         py310hca03da5_0  
utfcpp                    3.2.1                hca03da5_0  
vtk                       9.0.3           py310h30f073a_200  
wheel                     0.38.4          py310hca03da5_0  
x264                      1!152.20180806       h1a28f6b_0  
xtensor                   0.24.5               hffc8910_0    conda-forge
xtl                       0.7.5                hffc8910_0    conda-forge
xz                        5.2.10               h80987f9_1  
yaml                      0.2.5                h3422bc3_2    conda-forge
zeromq                    4.3.4                hbdafb3b_1    conda-forge
zfp                       0.5.5                hcfdfaf5_8    conda-forge
zlib                      1.2.13               h03a7124_4    conda-forge
zstd                      1.5.2                h8574219_0  

So it seems like you have installed it in the fenicsx-env. Have you activated it? what is the output of export $PYTHONPATH?

Yes it is activated using:

conda activate fenicsx-env

Here is the output of the export function:

declare -x CONDA_DEFAULT_ENV="fenicsx-env"
declare -x CONDA_EXE="/Users/fwaharte/mambaforge3/bin/conda"
declare -x CONDA_PREFIX="/Users/fwaharte/mambaforge3/envs/fenicsx-env"
declare -x CONDA_PREFIX_1="/Users/fwaharte/mambaforge3"
declare -x CONDA_PROMPT_MODIFIER="(fenicsx-env) "
declare -x CONDA_PYTHON_EXE="/Users/fwaharte/mambaforge3/bin/python"
declare -x CONDA_SHLVL="2"
declare -x GSETTINGS_SCHEMA_DIR="/Users/fwaharte/mambaforge3/envs/fenicsx-env/share/glib-2.0/schemas"
declare -x GSETTINGS_SCHEMA_DIR_CONDA_BACKUP=""
declare -x HOME="/Users/fwaharte"
declare -x HOMEBREW_CELLAR="/opt/homebrew/Cellar"
declare -x HOMEBREW_PREFIX="/opt/homebrew"
declare -x HOMEBREW_REPOSITORY="/opt/homebrew"
declare -x INFOPATH="/opt/homebrew/share/info:"
declare -x JAVA_HOME="/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents\$"
declare -x LANG="en_GB.UTF-8"
declare -x LOGNAME="fwaharte"
declare -x LaunchInstanceID="F80F7945-79DC-450A-B27A-8C64B29887E2"
declare -x MANPATH="/opt/homebrew/share/man:"
declare -x OLDPWD="/Users/fwaharte"
declare -x PATH="/Applications/FreeFem++.app/Contents/ff-4.11/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/fwaharte/mambaforge3/envs/fenicsx-env/bin:/Users/fwaharte/mambaforge3/condabin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin"
declare -x PROJ_DATA="/Users/fwaharte/mambaforge3/envs/fenicsx-env/share/proj"
declare -x PROJ_LIB="/Users/fwaharte/mambaforge3/envs/fenicsx-env/share/proj"
declare -x PROJ_NETWORK="ON"
declare -x PWD="/Users/fwaharte/Downloads"
declare -x SECURITYSESSIONID="186a6"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_AUTH_SOCK="/private/tmp/com.apple.launchd.ObYr6R9CqO/Listeners"
declare -x TERM="xterm-256color"
declare -x TERM_PROGRAM="Apple_Terminal"
declare -x TERM_PROGRAM_VERSION="447"
declare -x TERM_SESSION_ID="DAD287FD-774A-47B0-B247-0F3D208874F5"
declare -x TMPDIR="/var/folders/nn/3jwzjtl56z91x1sp9yz_fd9r023mdn/T/"
declare -x USER="fwaharte"
declare -x XPC_FLAGS="0x0"
declare -x XPC_SERVICE_NAME="0"
declare -x _CE_CONDA=""
declare -x _CE_M=""
declare -x __CFBundleIdentifier="com.apple.Terminal"

Sorry, i meant echo $PYTHONPATH

Oh, ok!

Then the output is empty. Not good I guess…

After activating the environment, there should be a path to the environment in $PYTHONPATH

That’s odd then…
Should I add it manually? Maybe adding the path to the installed modules?

I suspect this is a case of python3 not being the Python executable you expect it to be. You might start by checking which python3. You can also try being explicit about using the Python in your environment:

/Users/fwaharte/mambaforge3/envs/fenicsx-env/bin/python3 demo_poisson.py

No, $PYTHONPATH should almost always be undefined. It’s only really used when standard installation is unavailable.

Perhaps you mean the actual import path (to which $PYTHONPATH is one contributor), which would be the output of python3 -m site. At runtime, you can check print(sys.path).

This line:

suggests that the environment isn’t first on your $PATH, which in turn makes me thing that which python3 is going to return /opt/homebrew/bin/python3.

I don’t quite understand why conda activate -n fenicsx-env is not adding the env to the front of your PATH, but that seems to be the root cause. Does a fresh shell help at all?

Does it help to run:

. /Users/myname/opt/anaconda3/etc/profile.d/conda.sh

before running conda activate again?

2 Likes

I tried with a fresh shell: not better.

I ran the command

. /Users/myname/opt/anaconda3/etc/profile.d/conda.sh

But when I want to activate the env, I get this error message:

EnvironmentNameNotFound: Could not find conda environment: fenicsx-env

So I recreated a new env, installed fenicsx using conda.
And it works!
Thanks a lot :pray: