Home > Net >  How do I install GNU Radio in a Python virtualenv
How do I install GNU Radio in a Python virtualenv

Time:04-23

I am trying to get gnuradio to work in a virtualenv. I want to keep my system somewhat clean and my project needs Python 3.9, but my system Python is 3.8 (Ubuntu 20.04.4 LTS).

I install it using sudo apt install gnuradio, but then it ends up in the system dist-packages and I can't import it in my virtualenv. The imports work fine when running the system Python.

I have tried to recreate my virtualenv to use system packages:

python3.9 -m venv --system-site-packages venv

This at least made my virtualenv able to find gnuradio, but when I try importing stuff it fails in several different ways.

>>> from gnuradio import uhd
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gnuradio/uhd/__init__.py", line 21, in _prepare_uhd_python
    from . import uhd_python
ImportError: cannot import name 'uhd_python' from partially initialized module 'gnuradio.uhd' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gnuradio/uhd/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/gnuradio/uhd/__init__.py", line 68, in <module>
    _prepare_uhd_python()
  File "/usr/lib/python3/dist-packages/gnuradio/uhd/__init__.py", line 26, in _prepare_uhd_python
    from . import uhd_python
ImportError: cannot import name 'uhd_python' from partially initialized module 'gnuradio.uhd' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gnuradio/uhd/__init__.py)
>>> import pmt
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pmt/__init__.py", line 34, in <module>
    from .pmt_python import *
ModuleNotFoundError: No module named 'pmt.pmt_python'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pmt/__init__.py", line 38, in <module>
    from .pmt_python import *
ModuleNotFoundError: No module named 'pmt.pmt_python'

I have struggled with this for several days now and found no solution. Is it even possible to run GNU Radio from within a virtualenv?

CodePudding user response:

I have struggled with this for several days now and found no solution.

Your distro's GNU Radio package is built against your distro's python, so you can't use it with a different python.

So, this is expected.

Is it even possible to run GNU Radio from within a virtualenv?

Sure, you'd need to build it from that virtualenv as well, and tell cmake -DCMAKE_INSTALL_PREFIX=/path/to/virtualenv/ to install into it.

We have a pretty nice anaconda packager, so installing GNU Radio into a Python prefix using conda might be the easiest alternative to that.

Alternatively:

Ubuntu 20.04.4 LTS

There's a new Ubuntu LTS version out! If you go for a system upgrade to Ubuntu 22.04 LTS, you get Python 3.10, and a current GNU Radio 3.10.1 "for free".

  • Related