"ImportError: No module named myblock" when generating py file in GRC

Hello,

I am new to GNU radio. I am trying to write a simple block and add it to
GRC. All steps before are fine. However, when I generate the
top_block.py file, it says “ImportError: No module named myblock”.

The myblock is actually the name of the folder I create to put all
necessary files of my block such as files like MakeFile.am and
sub-folders lib, swig, etc. Can this be called a module?

What is a module? I see the code in how-to-write-a-block, it seems that
it also import a module howto. Where could I find such modules or create
one my own?

Sorry to ask so many questions; I cannot find a clear way to study this.
I would really appreciate any help.

Thank you in advance.

On 04/20/2011 03:00 AM, Yulong Y. wrote:

Hello,

I am new to GNU radio. I am trying to write a simple block and add it to
GRC. All steps before are fine. However, when I generate the
top_block.py file, it says “ImportError: No module named myblock”.

The myblock is actually the name of the folder I create to put all
necessary files of my block such as files like MakeFile.am and
sub-folders lib, swig, etc. Can this be called a module?

In the case of python, a module is a python file or a directory w/ a
python file init.py

What is a module? I see the code in how-to-write-a-block, it seems that
it also import a module howto. Where could I find such modules or create
one my own?

Sorry to ask so many questions; I cannot find a clear way to study this.
I would really appreciate any help.

Thank you in advance.

Python needs to be able to find your block: Did you install it into the
python search path? Or set PYTHONPATH to where your block is installed?

-josh

On Thu, Apr 21, 2011 at 7:17 AM, Josh B. [email protected] wrote:

necessary files of my block such as files like MakeFile.am and
sub-folders lib, swig, etc. Can this be called a module?

In the case of python, a module is a python file or a directory w/ a
python file init.py

Thank your for your reply.

So for example, the how-to–write-a-block folder stands for a module
called
“howto”, right? Is the name of the module defined in the py file?

Python needs to be able to find your block: Did you install it into the
python search path? Or set PYTHONPATH to where your block is installed?

Do your mean PYTHONPATH or just system search path?
I have set my block path as the PYTHONPATH in ~/.profile, but it still
cannot work. Is there a way to check whether the path is set
appropriately?

Hello,

I have tried both python -c “import…” and echo $. I see the path to my
folder in both of them. But it still says ImportError in top_block.py.
Where exactly should the path direct to? I tried both “~/gnuradio” and
“~/gnuradio/myblock”.

When you say init.py do you mean the one in sub-folder “python”? I
find this comment in that file: # The presence of this file turns this
directory into a Python package.

I am wondering where I could see the declare of my module.

To Mike: yes, I am in Ubuntu 10.04

Many thanks to your help.

Do your mean PYTHONPATH or just system search path?
I have set my block path as the PYTHONPATH in ~/.profile, but it still
cannot work. Is there a way to check whether the path is set appropriately?

You are in linux I presume? “echo $PYTHONPATH” (without quotes) should
tell you what your python path is set to. “export” will show all
environment variables.

You can also use export to set a variable for example export
PYTHONPATH=/home/user/project/howto/.

Another thing you can do in python is:
import sys
sys.path.append(’/path/to/module’). Add that before you import the
module.

Mike

On Wed, 20 Apr 2011 17:47:57 -0700
Nick F. [email protected] wrote:

file turns this directory into a Python package.
make

–n

Actually my block folder is copied from a wiki tutorial here:
http://gnuradio.org/redmine/wiki/gnuradio/More_examples

I tried create-out-of-tree command, it seems to download the newest
how-to block from the gnuradio site and make it into the path I create
it, right? In the folder, it looks the same with the default folder
“gr-howto-write-a-block”. Do you mean I should add my own block besides
the already exist files like howto_square_ff.cc?

On Thu, 2011-04-21 at 08:37 +0800, Yulong Y. wrote:

I am wondering where I could see the declare of my module.

How did you create your project? Did you use
“create-gnuradio-out-of-tree-project myblock”? If you did, you should be
able to:

cd myblock
./bootstrap
./configure
make
sudo make install

and then
python -c “import myblock”

Otherwise, you’re going to have to go into the howto-write-a-block
directory which you cloned, and edit the SWIG stuff to change all the
“howto” references to “myblock”, and that is going to be a pain. Try
re-creating your project using create-gnuradio-out-of-tree-project, it’s
the recommended way to start your own block project.

–n

On Thu, 2011-04-21 at 09:15 +0800, yyl wrote:

cd myblock
“howto” references to “myblock”, and that is going to be a pain. Try
how-to block from the gnuradio site and make it into the path I create
it, right? In the folder, it looks the same with the default folder
“gr-howto-write-a-block”. Do you mean I should add my own block besides
the already exist files like howto_square_ff.cc?

The friendly part about the out-of-tree script is it also renames
everything from “howto” to “myblock” or “monkeys” or whatever name you
invoked the out-of-tree script with, and so takes care of half the work
for you.

After that, you will have to add your custom blocks to SWIG by editing
myblock/swig/myblock.i and adding them there, as well as making .i files
to correspond to your blocks, just like
myblock/swig/myblock_square_ff.i. You will also obviously have to add
the C++ blocks themselves to the Makefile.am in your myblock/lib
directory, and if you want GRC blocks made, put the .xml files in
myblock/grc and add them to the Makefile.am in the myblock/grc
directory.

Basically, wherever you see “square” in the project, replace it with the
name of your own block.

–n

Hi,

I try to add all it needs to put my custom block into this module. I add
the corresponding .xml/.cc/.h/.i files into grc/lib/swig folder
separately and modify MakeFile.am files in each folders. However, it
still cannot work. When generating top_block.py in GRC, the console
says:
self.myblock_amplifier_ff_0 = myblock.amplifier_ff(1)
AttributeError: ‘module’ object has no attribute ‘amplifier_ff’

My block is a simple amplifier with a parameter “Gain”. My module is
“myblock” generated with the out-of-tree-project command. I tried the
square_ff block before and it works well in GRC. What does this error
exactly mean?

Thank you.

On Thu, Apr 21, 2011 at 10:29 PM, Yulong Y. [email protected] wrote:

My block is a simple amplifier with a parameter “Gain”. My module is
“myblock” generated with the out-of-tree-project command. I tried the
square_ff block before and it works well in GRC. What does this error
exactly mean?

Thank you.

It’s very likely that you have missed something in the Makefile
somewhere.
There are a lot of steps here, and since you can import myblock, you’ve
gotten that far. It’s just missing the actual block inside of the
module.
Make sure you really have added everything you need to in the Makefiles
in
grc, lib, and swig directories and that all of the .i files are
accounted
for.

Tom

Tom R. wrote in post #994428:

On Thu, Apr 21, 2011 at 10:29 PM, Yulong Y. [email protected] wrote:

My block is a simple amplifier with a parameter “Gain”. My module is
“myblock” generated with the out-of-tree-project command. I tried the
square_ff block before and it works well in GRC. What does this error
exactly mean?

Thank you.

It’s very likely that you have missed something in the Makefile
somewhere.
There are a lot of steps here, and since you can import myblock, you’ve
gotten that far. It’s just missing the actual block inside of the
module.
Make sure you really have added everything you need to in the Makefiles
in
grc, lib, and swig directories and that all of the .i files are
accounted
for.

Tom

hi Yulong Y.

did u solve the problem I also facing it. If you solved please let me
know. Thank you in advance

Praveen