Allow importing

Issue #170 invalid
Franco Milicchio created an issue

Dear all,

Following the discussion on Google Groups, I am submitting a request to allow import in UFL files.

As of now, using an import results in an exception:

% ffc -l dolfin general.ufl

An exception occured during evaluation of .ufl file.

If you need to debug it as a python script, rename it to .py

and add the lines



    from ufl import *

    set_level(DEBUG)



at the top then run with python.



Traceback (most recent call last):

  File "/Users/sensei/anaconda3/bin/ffc", line 11, in <module>

    load_entry_point('FFC==2017.1.0', 'console_scripts', 'ffc')()

  File "/Users/sensei/anaconda3/lib/python3.6/site-packages/ffc/main.py", line 217, in main

    resultcode = _compile_files(args, parameters, enable_profile)

  File "/Users/sensei/anaconda3/lib/python3.6/site-packages/ffc/main.py", line 250, in _compile_files

    ufd = load_ufl_file(filename)

  File "/Users/sensei/anaconda3/lib/python3.6/site-packages/ufl/algorithms/formfiles.py", line 232, in load_ufl_file

    namespace = execute_ufl_code(uflcode, filename)

  File "/Users/sensei/anaconda3/lib/python3.6/site-packages/ufl/algorithms/formfiles.py", line 133, in execute_ufl_code

    raise e

  File "/Users/sensei/anaconda3/lib/python3.6/site-packages/ufl/algorithms/formfiles.py", line 130, in execute_ufl_code

    exec(uflcode, namespace)

  File "<string>", line 6, in <module>

ModuleNotFoundError: No module named 'definitions'

The culprit files are:

definitions.py

testvar = "DG"

general.ufl

from definitions import *

cell     = tetrahedron
elementS = FiniteElement(testvar, cell, 0)
elementV = VectorElement("Lagrange", cell, 2)
elementT = TensorElement("Discontinuous Lagrange", cell, 1)

# trial functions
us = TrialFunction(elementV)
uv = TrialFunction(elementV)
ut = TrialFunction(elementV)

# test functions
vs = TestFunction(elementV)
vv = TestFunction(elementV)
vt = TestFunction(elementV)

# all forms
scalar_form = inner(us, vs) * dx
vector_form = inner(uv, vv) * dx
tensor_form = inner(ut, vt) * dx

# export forms to access function spaces
forms = [scalar_form, vector_form, tensor_form]

Comments (6)

  1. Jan Blechta

    This is invalid. definitions.py is not a proper module. Run

    mkdir definitions
    mv definitions.py definitions
    echo "from definitions.definitions import *" > definitions/__init__.py
    PYTHONPATH="$PWD:$PYTHONPATH" ffc-3 general.ufl
    

    or install module definitions to avoid modifying PYTHONPATH (not recommendable due to its generic name).

  2. Jan Blechta

    As a side note, we can't really automatically look for foo.py in current dir or form file dir (when you have import foo in a form file). That would be a security risk, or at least a source of hard-to-debug bugs. Imagine you drop a file logging.py in the current dir.

  3. Franco Milicchio reporter

    I will try to make a fully fledged module, however, it is really handy for developing prototypes to just import a file, as it works perfectly both from python's console and from PyCharm (which I use with fenics) without any modifications to PYTHONPATH (macOS 10.12.6).

  4. Jan Blechta

    I was actually a bit wrong. Single .py file works too. But it must be on PYTHONPATH (or other sites where Python looks at). So PYTHONPATH=$PWD:$PYTHONPATH ffc-3 general.ufl works even with your file layout.

    Roughly said there are two modes how to run Python: 1. python3 script.py and 2. python3 -mpackage args. You're used that in the case 1. script directory is in sys.path so you can import "packages" in that directory.

    Running FFC script is equivalent to case 2. and FFC/UFL has no authority to fiddle with PYTHONPATH. Than would be non-standard behaviour and a potential security risk. It is user's responsibility to take care of installation of his own packages, e.g., by doing simply export PYTHONPATH=$PWD:$PYTHONPATH.

  5. Log in to comment