user169808
user169808

Reputation: 513

pyrtlsdr on windows doesnt import

I'm trying to get pyrtlsdr-0.2.9 to work on a windows 10 computer with python 3.6. First, I installed it with pip like the instructions says on the site. Then, for some reason when I try to import the module or run an example script I get the following error message:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import rtlsdr
  File "C:\...\Programs\Python\Python36-32\lib\site-packages\rtlsdr\__init__.py", line 56, in <module>
    from .librtlsdr import librtlsdr
  File "C:\...\Programs\Python\Python36-32\lib\site-packages\rtlsdr\librtlsdr.py", line 50, in <module>
    librtlsdr = load_librtlsdr()
  File "C:\...\Programs\Python\Python36-32\lib\site-packages\rtlsdr\librtlsdr.py", line 45, in load_librtlsdr
    raise ImportError('Error loading librtlsdr. Make sure librtlsdr '\
ImportError: Error loading librtlsdr. Make sure librtlsdr (and all of its dependencies) are in your path

I've tried to lightly edit the module and to keep it in the same folder as my script in which case I get this error:

OSError: Error code -12 when opening SDR (device index = 0)

How can I get this module to work on windows? librtlsdr works with my dongle, but apparently not via python.

Upvotes: 1

Views: 6462

Answers (4)

Zahi
Zahi

Reputation: 1

David insights are true, but I just wanted to state that you can comment all the "driver_files" lines and write the explicit path for all 3 dll's files. Below is an example assuming you are using conda. Open and Edit this file:

"MyDrive:\Anaconda3\Lib\site-packages\rtlsdr\librtlsdr.py"

Like this:

# driver_files += ['librtlsdr.so', 'rtlsdr/librtlsdr.so']
# driver_files += ['rtlsdr.dll', 'librtlsdr.so']
# driver_files += ['..//rtlsdr.dll', '..//librtlsdr.so']
# driver_files += ['rtlsdr//rtlsdr.dll', 'rtlsdr//librtlsdr.so']
# driver_files += [lambda : find_library('rtlsdr'), lambda :find_library('librtlsdr')]
driver_files += ['D:\\Anaconda3\\Lib\\site-packages\\rtlsdr\\librtlsdr.dll']
driver_files += ['D:\\Anaconda3\\Lib\\site-packages\\rtlsdr\\libusb-1.0.dll']
driver_files += ['D:\\Anaconda3\\Lib\\site-packages\\rtlsdr\\libwinpthread-1.dll']

Upvotes: 0

Henk van Asselt
Henk van Asselt

Reputation: 1

I also had this 'Error loading librtlsdr. Make sure librtlsdr', but my root cause was different.

I did use a 64bit Win10 PC, so thought I had to use the 64bit librtlsdr drivers, downloaded from https://ftp.osmocom.org/binaries/windows/rtl-sdr/

That did not work because the Python 3.8 version is the 32bit version, so I also had to download and use the 32bit drivers.

//Henk

Upvotes: 0

David
David

Reputation: 702

I was also having trouble. In my case I was executing test.py from the top directory of pyrtlsdr.

If you look into rtlsdr/librtlsdr.py, which actually loads the DLL, you can see:

driver_files += ['librtlsdr.so', 'rtlsdr/librtlsdr.so']
driver_files += ['rtlsdr.dll', 'librtlsdr.so']
driver_files += ['..//rtlsdr.dll', '..//librtlsdr.so']
driver_files += ['rtlsdr//rtlsdr.dll', 'rtlsdr//librtlsdr.so']
driver_files += [lambda : find_library('rtlsdr'), lambda : find_library('librtlsdr')]

find_library() only looks in the system PATH:

def find_library(name):
    if name in ('c', 'm'):
        return find_msvcrt()
    # See MSDN for the REAL search order.
    for directory in os.environ['PATH'].split(os.pathsep):
        fname = os.path.join(directory, name)
        if os.path.isfile(fname):
            return fname
        if fname.lower().endswith(".dll"):
            continue
        fname = fname + ".dll"
        if os.path.isfile(fname):
            return fname
    return None

So unless it's on the system PATH it has to be named rtlsdr.dll. So after renaming, I was expecting it would work with the DLL in the top directory (due to the entry driver_files += ['rtlsdr.dll', 'librtlsdr.so']), but it didn't. Only the relative paths (like driver_files += ['rtlsdr//rtlsdr.dll', rtlsdr//librtlsdr.so']) seem to work. So if you want to have it in the directory you are executing, you have to change the fully unqualified entry to driver_files += ['.//rtlsdr.dll', 'librtlsdr.so']. Or, alternatively put the DLL in the rtlsdr folder. I didn't have time to dig further but I suspect has something to do with how the WIN32 LoadLibrary API (or whatever is used to load the DLL into the process space) is being invoked, and it's search rules.

rtl_test.exe works out of the box because it's looking for librtlsdr.dll:

dumpbin /dependents rtl_test.exe
Microsoft (R) COFF/PE Dumper Version 14.23.28106.4
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file rtl_test.exe

File Type: EXECUTABLE IMAGE

  Image has the following dependencies:

    KERNEL32.dll
    msvcrt.dll
    librtlsdr.dll

Upvotes: 1

Vijay Anand Pandian
Vijay Anand Pandian

Reputation: 1165

After reading the documentation -

https://github.com/roger-/pyrtlsdr

You have to make sure you have correctly installed the dependencies. In your case, its a windows machine.

Dependencies

  • Windows/Linux/OSX
  • Python 2.7.x/3.3+
  • librtlsdr
  • Optional: NumPy (wraps samples in a more convenient form)

matplotlib is also useful for plotting data. The librtlsdr binaries (rtlsdr.dll in Windows and librtlsdr.so in Linux) should be in the pyrtlsdr directory, or a system path. Note that these binaries may have additional dependencies.

If you're having librtlsdr import errors:

  • Windows: Make sure all the librtlsdr DLL files (librtlsdr.dll, libusb-1.0.dll) are in your system path, or the same folder as this README file. Also make sure you have all of their dependencies (e.g. libgcc_s_dw2-1.dll or possibly the Visual Studio runtime files). If rtl_sdr.exe works, then you should be okay. Also note that you can't mix the 64 bit version of Python with 32 bit builds of librtlsdr, and vice versa.

For more information - https://github.com/roger-/pyrtlsdr#troubleshooting

Upvotes: 1

Related Questions