Python 3.12 - No module named 'imp'

I tried installing the latest ArduCopter on Ubuntu 24.04 for simulation with

git clone -b Copter-4.5.6 https://github.com/ardupilot/ardupilot

but encountered the following compilation error when I ran sim_vehicle.py:

On further review, it appears the imp module has been deprecated and removed in python 3.12, the default version of python in Ubuntu 24.04. The module imp has been replaced by importlib. Does this mean an update is needed to the ardupilot repo? Should I try installing an older version of python to get it to work?

If you don’t have an ArduPilot virtual environment configured in Ubuntu 24.04, you will likely run into issues. I don’t think module deprecation under Python 3.12 is the root cause.

Have you run install-prereqs-ubuntu.sh?

For example, using a very similar environment on a recent Arch Linux install with Python 3.12.5, if I do not activate the venv, I get the following output:

$ sim_vehicle.py -v ArduCopter --no-mavproxy
Traceback (most recent call last):
  File "/home/myusername/ardupilot/Tools/autotest/sim_vehicle.py", line 31, in <module>
    from pysim import util
  File "/home/myusername/ardupilot/Tools/autotest/pysim/util.py", line 19, in <module>
    import pexpect
ModuleNotFoundError: No module named 'pexpect'

But as soon as I activate the venv, all is well.

I have also recently used Ubuntu 24.04 without any problems, though I’ve largely abandoned Ubuntu for reasons that have nothing to do with this particular issue (even the most lightweight server edition installations are just too bloated, IMHO).

Thank you Yuri. I ran ‘install-prereqs-ubuntu.sh’ and venv but no luck. I am still getting the same message running sim_vehicle.py.

Specifically, I followed these steps 1) installed Ubuntu 24.04 on virtualbox, 2) installed a bunch of dependencies, 3) downloaded and installed install-prereqs-ubuntu.sh, 4) installed various python packages using ‘pip-install --break-system-packages ’, 5) created my directories and ArduCopter using the git repo, 6) installed and activated venv, 7) Ran sim_vehicle.py.

I have installed all of the above in earlier versions of Ubuntu excluding steps 3, 4 (using just pip) and 6 without any issues. Something has changed?

That sounds like too many steps. You shouldn’t need to globally install any pip modules or one-off dependencies. Yes, things have changed since Ubuntu 22.04. But if you clone the repo and update submodules, then run the script, you should have a venv available to activate (created by the script and not named “myvenv”). And it works first time every time.

I am running python dronekit and pymavlink programs, so my dependencies include:
numpy, opencv, wxgtk, matplotlib, libxml2, libxslt-dev, scipy, pexpect future, screen
The pip modules are
pygame, pyyaml, mavproxy, pymavlink, empy, dronekit, dronekit-sitl

Does this not seem correct? I was told by an IT guy that since python3.12 there are package management conflicts, and to use the --break-system-packages statements.

What are the general steps you use to build your copter sitl?
Once I get this sorted, I would also like to try out Arch Linux.

You are blindly running a whole lot of extra commands that make little sense.

You don’t need just any venv, you need to source the one located at venv_ardupilot, which should have been created in the same directory as your ArduPilot clone (which, by the sounds of it, you may have cloned incompletely…).

what I did to setup the toolchain for version 4.5.x on recent ubuntu/mint without changing my system python version:

  • install ardupilot normally, if installing the master branch (version 4.6) the nstall-prereqs-ubuntu.sh script on master worked for me
  • checkout a 4.5.x branch or tag, run ./Tools/gittools/submodule-sync.sh
  • now you will need to get python 3.11
  • without changing your whole system, you can use this stand alone portable python for linux Releases · 25077667/standalone-python · GitHub
    • use a release with pthon 3.11 (x64) and unzip it, so it replaces the preinstalled ardupilot python venv next to your ardupilot folder in venv-ardupilot
    • after that you can run e.g. .waf clean inside the ardupilot folder which might prompt you to install additional python packages, install them until it runs like
<path-parent-dir-of-ardupolot-instlal>/venv-ardupilot/bin/python -m pip install <package-name-from-prompt>
  • for sitl to work you will also need to install:
<path-parent-dir-of-ardupolot-instlal>/venv-ardupilot/bin/python -m pip install future
<path-parent-dir-of-ardupolot-instlal>/venv-ardupilot/bin/python -m pip install MAVProxy