Python Forum
How Do I Get the IDE to Find My Imports
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How Do I Get the IDE to Find My Imports
#1
I can't seem to get an IDE to find my imports.
I have tried IDLE and Visual Basic (Note I have used Visual Basic to write a few python programs quite successfully)

I have a python program as a file on my windows desktop.
The name is tutorial_three_1.py
If I double click it, it runs fine.
If I open it in either IDLE or Visual Basic I can't debug it because IDE reports:
"Traceback (most recent call last):
File "C:\Users\Pete\Desktop\tutorial_three_1.py", line 9, in <module>
from gnuradio import analog
File "C:\Program Files (x86)\AAours(x86)\Utilities\SDR\GNURadio-3.7\lib\site-packages\gnuradio\analog\__init__.py", line 33, in <module>
from analog_swig import *
ModuleNotFoundError: No module named 'analog_swig'
"

And Visual Basic reports:
"Unable to resolve 'gnuradio'. IntelliSense may be missing for this module".

I am new to python but I understand the program is just a script so when it runs it somehow finds the imports but in an IDE they are not found. The code is below. If you run it you should hear two tones through your sound card. 350 Hz and 440 Hz.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Tutorial Three 1
# Generated: Sat Feb 16 15:56:06 2019
##################################################

from gnuradio import analog
from gnuradio import audio
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser


class tutorial_three_1(gr.top_block):

    def __init__(self):
        gr.top_block.__init__(self, "Tutorial Three 1")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################
        self.audio_sink_0 = audio.sink(samp_rate, '', True)
        self.analog_sig_source_x_1 = analog.sig_source_f(samp_rate, analog.GR_SQR_WAVE, 350, 0.1, 0)
        self.analog_sig_source_x_0 = analog.sig_source_f(samp_rate, analog.GR_SIN_WAVE, 440, 0.1, 0)



        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_sig_source_x_0, 0), (self.audio_sink_0, 0))
        self.connect((self.analog_sig_source_x_1, 0), (self.audio_sink_0, 1))

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.analog_sig_source_x_1.set_sampling_freq(self.samp_rate)
        self.analog_sig_source_x_0.set_sampling_freq(self.samp_rate)


def main(top_block_cls=tutorial_three_1, options=None):

    tb = top_block_cls()
    tb.start()
    try:
        raw_input('Press Enter to quit: ')
    except EOFError:
        pass
    tb.stop()
    tb.wait()


if __name__ == '__main__':
    main()
Reply
#2
You need to make sure analog_swig is installed. You can install it with pip from the command line if pip is installed. I would recommend getting pip.
Reply
#3
I have 6 files in the folder "analog" with analog_swig as part of their name. I can't find a way to insert the image here but all have extensions like .i or .py or..... There is no, "analog_swig" without an extension
There are a total of 38 files in that folder.
From the error message it seems the error happens in "_int_.py" which is one of the files in "analog". The snipit that throws the error is at the bottom of this post. So it looks like the statement:
from gnuradio import analog
tries to import and (maybe) execute all 38 files. When it hits _int_.py it throws the error.

Now, please understand I am a newbie trying to understand the process. But bottom line, when I double click on this .py file it executes correctly. Doesn't that imply that it finds all the imports?
Why can't the IDE find them?

I'll look into pip

try:
    from analog_swig import *
except ImportError:
    dirname, filename = os.path.split(os.path.abspath(__file__))
    __path__.append(os.path.join(dirname, "..", "..", "swig"))
    from analog_swig import *
Reply
#4
If you are importing a program you made, you need to make sure it is in the same directory as the program you are importing it too.
Reply
#5
Well I may have found the problem but not the solution. It seems I have python installed in two places. I installed Python 3.7 but I also found that GNURadio installed Python 2.7 down in its folder. It also has windows run the 2.7 version when I double click on a .py program. But IDLE and Visual Studio both use the 3.7 version. The files that GNURadio wants to import are in a folder near the 2.7 version. Somehow python 2.7 knows how to find them.

But when I want to use a IDE like IDLE (3.7) or Visual Studio, they can't find the files to import. I tried to use the version of IDLE that with Python 2.7 but I can't get it to run. There is a .bat to setup to run IDLE but when I run it it just runs and returns to the windows cmd prompt. I put "pause" after each statement in the .bat file but I still haven't found why IDLE (2.7) doesn't run.

I suppose if:
(1) I could get IDLE 3.7 to go to the files to import in the Python 2.7 area

Or
(2) I could get Visual Studio to do something similar,
I might get past this hurtle.

Any ideas?

BTW, GNURadio is a beautiful program for understanding digital signal processing and Software Designed Radios.
Reply
#6
I have a .py program that runs using Python 2.7 but fails in Python 3.7.2.
It fails at the first import statement.
I started asking for help in thread:
https://python-forum.io/Thread-How-Do-I-...My-Imports
I started to wander around too much in that thread and I have tried many things since the last post.
I am pursuing the potential problem outlined in the title of this post. I simplified the program from that post. The one I am trying to run is below. If I run it with python 2.7 it runs fine. If I run it with python 3.7.2 the window opens then closes. If I load it into IDLE (python 3.7 version) and try to run it I get:
"Traceback (most recent call last):
File "(long path removed)gnuradio\gr\__init__.py", line 37, in <module>
from runtime_swig import *
ModuleNotFoundError: No module named 'runtime_swig'

(lots more error message removed)

It looks like "from gnuradio import gr" causes it to go to __init__.py which then has a step at line 37 which says "from runtime_swig import *" however gr desn't have runtime_swig. It has runtime_swig.py, runtime_swig.pyc, and runtime_swig.pyo.

Somehow python 2.7 doesn't mind this but python 3.7 does.
Any ideas?

from gnuradio import gr
from gnuradio import audio
from gnuradio import analog

class my_top_block(gr.top_block):

    def __init__(self):
        gr.top_block.__init__(self)
        sample_rate = 32000
        ampl = 0.1

        src0 = analog.sig_source_f(sample_rate, analog.GR_SIN_WAVE, 350, ampl)
        src1 = analog.sig_source_f(sample_rate, analog.GR_SIN_WAVE, 440, ampl)
        dst = audio.sink(sample_rate, "")
        self.connect(src0, (dst, 0))
        self.connect(src1, (dst, 1))


if __name__ == '__main__':

    try:

        my_top_block().run()

    except [[KeyboardInterrupt]]:

        pass
Reply
#7
Please, keep the discussion in the original thread.
Did you see this issue? It claims python3 support will come with version 3.8. Latest stable version is 3.7.9 and you use 3.7. Currently 3.8 release is in tech-preview status

Also in Readme there are instructions how to build gnuradio with PyBOMBS with python3 support. Did you follow them?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
OK, sorry for the new thread.
Please understand I'm an OLD assembly language programmer with a lot of Windows, Visual Basic, and some Java mixed in.
I am totally new to Linux, Python, and github.
I believe I have version 3.7.13.4 of GNURadio installed because I installed it about a month ago and github releases says 3.7.13.4 was released on July 15, 2018. But I can't decipher what the actual executable is so I can't find a version number. The file I use to start GNURadio companion is a BAT file that calls another BAT file that starts a python program and I can't tell where it goes from there.

I may be wrong but the PyBOMBS readme seems to be Linux oriented and I'm running on Windows.
Somehow I just think that if I could get the python 2.7 version of IDLE to run or if I can figure out why python 3.7 won't chain through the import(s) I might break my log jam. Or maybe not??????????????
Reply
#9
(Feb-19-2019, 06:13 PM)petec Wrote: I may be wrong but the PyBOMBS readme seems to be Linux oriented and I'm running on Windows.
you are right, PyBOMBS is Linux.
I guess you will have to wait for 3.8 in order to use python3
for time being install python2.7 and use GNUradio 3.7.x
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
I guess you are right but it is very frustrating. I have a very short (20 lines or so) program that runs under 2.7 but not 3.7. And it is just an import statement that causes the problem. I have spent hours reviewing the python docs (I hope they are current) about how it searches for the files. I have created copies of the folders anywhere I think it could be searching.
This is the kind of problem I should be able to solve.

Thanks
Pete
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,550 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Imports in my first package cuppajoeman 1 1,912 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,544 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Help wanted with python imports petros21 3 2,481 Apr-07-2021, 07:16 PM
Last Post: snippsat
Question How to include Modules not found (conditional imports) in my setup.py when I want to cff 0 3,767 Mar-17-2021, 11:57 AM
Last Post: cff
  threading across imports Nickd12 2 2,105 Nov-09-2020, 01:59 AM
Last Post: Nickd12
  refreshing imports seandepagnier 4 2,674 Sep-20-2020, 11:51 PM
Last Post: seandepagnier
  Multimode imports fine as script but not after compiling into exe chesschaser 0 2,374 Aug-13-2020, 01:28 PM
Last Post: chesschaser
  absolute imports between folders mikisDW 0 1,505 Aug-05-2020, 12:26 PM
Last Post: mikisDW
  Understanding the concept ( Modules , imports ) erfanakbari1 1 2,140 Nov-25-2019, 01:59 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020