Skip to content

Solve the yade.config import issue when enabling `NOSUFFIX` option

Sacha Duverger requested to merge correct_nosuffix into master

The problem this MR solves

When compiling with the option NOSUFFIX (see compilation logs cmake_out.log and make_out.log), YADE fails on launch with the error ModuleNotFoundError: No module named 'yade.config'; 'yade' is not a package.

Origin of the problem

The reason this happens is because the first directory in the Python path is the directory containing YADE's executable, which is named simply yade because of the NOSUFFIX option. As a consequence, when Python tries to import yade.config, it confuses YADE's executable ${INSTALL_PREFIX}/bin/yade with the directory ${INSTALL_PREFIX}/lib/x86_64-linux-gnu/yade/py/yade and raises the ModuleNotFoundError. Here is the output of a Python session illustrating the issue:

$ python3 -i ~/packages/yadeperso/install/bin/yade.py 
Welcome to Yade 2023-05-15.git-a46ea55 
Traceback (most recent call last):
  File "/home/sacha/packages/yadeperso/install/bin/yade.py", line 151, in <module>
    import yade
  File "/home/sacha/packages/yadeperso/install/bin/yade.py", line 153, in <module>
    import yade.config
ModuleNotFoundError: No module named 'yade.config'; 'yade' is not a package
>>> for dir in sys.path: print(dir)
... 
/home/sacha/packages/yadeperso/install/bin
/home/sacha/packages/yadeperso/install/lib/x86_64-linux-gnu/yade/py
/home/sacha/packages/yadeperso/install/lib/x86_64-linux-gnu/yade/py
/usr/lib/python38.zip
/usr/lib/python3.8
/usr/lib/python3.8/lib-dynload
/home/sacha/.local/lib/python3.8/site-packages
/home/sacha/packages/s-tui
/home/sacha/packages/pycbg/src
/usr/local/lib/python3.8/dist-packages
/usr/lib/python3/dist-packages
/home/sacha/packages/yadeperso/install/lib/x86_64-linux-gnu/yade/py
/home/sacha/packages/yadeperso/install/lib/x86_64-linux-gnu/yade/py
>>> os.listdir(sys.path[0]), os.listdir(sys.path[1])
(['yade', '__pycache__', 'yade.py', 'yade-batch'], ['gts', 'yade', 'mtTkinter.py'])

The solution proposed in this MR

Python's developers anticipated this kind of issues and propose a couple solutions: launching the script with the -P option, or setting the environment variable PYTHONSAFEPATH to anything (see Python's documentation). However, these solutions exist only since Python 3.11 and complexifies the workflow of the user if he chose to use the NOSUFFIX option (I was this user recently). The solution I propose is to insert YADE's librairy as the very first element of the Python path (instead of the second), only if the NOSUFFIX option is activated. This way, any scripts that assumes YADE's executable directory to be the first in the Python path will still work without the NOSUFFIX option.

Edited by Sacha Duverger

Merge request reports