# Issue with python terminal

**URL:** <https://discuss.avogadro.cc/t/issue-with-python-terminal/1355>\
**Category:** Development\
**Created:** [May 20, 2009, 6:33pm UTC](https://discuss.avogadro.cc/t/issue-with-python-terminal/1355 "2009-05-20T18:33:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alfredo\_Palace\_Carva](https://discuss.avogadro.cc/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@Alfredo\_Palace\_Carva](https://discuss.avogadro.cc/u/Alfredo_Palace_Carva)\
**Post date:** [May 20, 2009, 6:33pm UTC](https://discuss.avogadro.cc/t/issue-with-python-terminal/1355/1 "2009-05-20T18:33:34Z")

</div>

Hi all,

I am back with an old problem of mine (in fact, I haven’t had  
the time to look into it lately, but still cannot solve it anyhow).

If I open a molecule in avogadro and execute the following python  
statements in the Python Terminal:

> > > import Avogadro  
> > > mol=Avogadro.Molecule  
> > > print “num atoms = %d” % mol.numAtoms

I get the following error message:

Traceback (most recent call last):  
File “”, line 1, in   
TypeError: int argument required

This is due to the fact that:

> > > print mol.numAtoms  
> > > \<property object at 0xb10d2464\>

Is this the normal behavior? I was expecting “mol.numAtoms” to be an  
(int) in fact but instead it seems to be an instance of a (undocumented)  
“property” class. How do I deal with these objects? Or is this exposure  
of a “property” (instead of a integer (or other data types)) a bug in  
the python interface? Do I go ahead and report a bug?

Thank you in advance for your care.  
Best regards,  
Alfredo Carvalho

---

<div class="post-metadata">

**Author:** ![Tim\_Vandermeersch](https://discuss.avogadro.cc/user_avatar/discuss.avogadro.cc/tim_vandermeersch/32/3479_2.png) [@Tim\_Vandermeersch](https://discuss.avogadro.cc/u/Tim_Vandermeersch)\
**Post date:** [May 20, 2009, 6:54pm UTC](https://discuss.avogadro.cc/t/issue-with-python-terminal/1355/2 "2009-05-20T18:54:00Z")

</div>

Hi,

On Wed, May 20, 2009 at 8:33 PM, Alfredo Palace Carvalho  
[ajpalace@uevora.pt](mailto:ajpalace@uevora.pt) wrote:

> Hi all,
> 
> I am back with an old problem of mine (in fact, I haven’t had  
> the time to look into it lately, but still cannot solve it anyhow).
> 
> If I open a molecule in avogadro and execute the following python  
> statements in the Python Terminal:
> 
> > > > import Avogadro  
> > > > mol=Avogadro.Molecule

If you want to get the current molecule, use Avogadro.molecule (with a  
small ‘m’). If you use Molecule, you get the class definition and not  
the instance of the molecule. As a rule of thumb, classes start with  
capital letters instances with lower case letters.

There is also Avogadro.molecules, which is an instance of the  
MoleculeList class (python only). Use this class like:

> > > newMol = Avogadro.molecules.addMolecule()

to create a new molecule if needed. (It makes sure the molecule  
doesn’t get deleted when you assigned it to a GLWidget for example.)

Tim

> > > > print “num atoms = %d” % mol.numAtoms
> 
> I get the following error message:
> 
> Traceback (most recent call last):  
> File “”, line 1, in   
> TypeError: int argument required
> 
> This is due to the fact that:
> 
> > > > print mol.numAtoms  
> > > > \<property object at 0xb10d2464\>
> 
> Is this the normal behavior? I was expecting “mol.numAtoms” to be an  
> (int) in fact but instead it seems to be an instance of a (undocumented)  
> “property” class. How do I deal with these objects? Or is this exposure  
> of a “property” (instead of a integer (or other data types)) a bug in  
> the python interface? Do I go ahead and report a bug?
> 
> Thank you in advance for your care.  
> Best regards,  
> Alfredo Carvalho
> 
> * * *
> 
> Crystal Reports - New Free Runtime and 30 Day Trial  
> Check out the new simplified licensing option that enables  
> unlimited royalty-free distribution of the report engine  
> for externally facing server and web deployment.  
> [http://p.sf.net/sfu/businessobjects](http://p.sf.net/sfu/businessobjects)
> 
> * * *
> 
> Avogadro-devel mailing list  
> [Avogadro-devel@lists.sourceforge.net](mailto:Avogadro-devel@lists.sourceforge.net)  
> [avogadro-devel List Signup and Options](https://lists.sourceforge.net/lists/listinfo/avogadro-devel)

---

<div class="post-metadata">

**Author:** ![Alfredo\_Palace\_Carva](https://discuss.avogadro.cc/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@Alfredo\_Palace\_Carva](https://discuss.avogadro.cc/u/Alfredo_Palace_Carva)\
**Post date:** [May 21, 2009, 3:00pm UTC](https://discuss.avogadro.cc/t/issue-with-python-terminal/1355/3 "2009-05-21T15:00:56Z")

</div>

Hi,

Thanks for the prompt reply.  
This is a problem I’ve been struggling with for quite a long time  
(maybe from the switch from 0.8 versions to 0.9 versions) and  
has been stoping me from running my python scripts, because, basically  
I’ve been unable to do anything useful with python…

Now I am a bit confused and I realize I do not know the basics  
of python scripting in Avogadro.  
At first, when I retried to run those statements (in the Python  
Terminal) following your instructions I was very glad to see that it worked.  
I could not believe it was such a basic thing. I have reminiscences  
that in the past (i.e. avogadro 0.8.x) I used Avogadro.molecule  
to refer to the current molecule in scripts , but after a certain  
upgrade of Avogadro my python scripts stoped working (due to severe  
changes in the programming interface, i.e. the introduction  
of the Extension class, etc.) and following the documentation  
online and examples scripts I could never get them to work  
anymore. Probably then I made this change (Avogadro.molecule  
to Avogadro.Molecule) and saw fewer error messages and assumed  
I was on the right path.

Anyway, now I made this change in one of my test python extension  
scripts (which I include in attachment) and I am still unable  
to run it. I now realize that what I still can’t (don’t know how)  
to do is to refer to the current molecule inside the performAction  
method. “Avogadro.molecule” works inside the Python Terminal  
but not inside the “Extension” class’s performAction method.  
Running my code gives the following error:

AttributeError: ‘module’ object has no attribute ‘molecule’

Could you please take a look at my script? It is fairly simple.  
If I can refer to the current molecule in this script I think  
I can work out the rest by myself, but I could not find any  
instructions on how to do it in the documentation/examples.  
In the examples I have seen, either they work by creating a new  
molecule or the python code in the examples is meant for the  
python Terminal but do not work (apparently) inside an extension  
script. Maybe I have overlooked something in the documentation but  
I feel stuck just in the beginning of my scripts!!!

Thank you very much.

Cheers,  
Alfredo

On Wed, 20 May 2009, Tim Vandermeersch wrote:

> Hi,
> 
> On Wed, May 20, 2009 at 8:33 PM, Alfredo Palace Carvalho  
> [ajpalace@uevora.pt](mailto:ajpalace@uevora.pt) wrote:
> 
> > Hi all,
> > 
> > I am back with an old problem of mine (in fact, I haven’t had  
> > the time to look into it lately, but still cannot solve it anyhow).
> > 
> > If I open a molecule in avogadro and execute the following python  
> > statements in the Python Terminal:
> > 
> > > > > import Avogadro  
> > > > > mol=Avogadro.Molecule
> 
> If you want to get the current molecule, use Avogadro.molecule (with a  
> small ‘m’). If you use Molecule, you get the class definition and not  
> the instance of the molecule. As a rule of thumb, classes start with  
> capital letters instances with lower case letters.
> 
> There is also Avogadro.molecules, which is an instance of the  
> MoleculeList class (python only). Use this class like:
> 
> > > > newMol = Avogadro.molecules.addMolecule()
> 
> to create a new molecule if needed. (It makes sure the molecule  
> doesn’t get deleted when you assigned it to a GLWidget for example.)
> 
> Tim
> 
> > > > > print “num atoms = %d” % mol.numAtoms
> > 
> > I get the following error message:
> > 
> > Traceback (most recent call last):  
> > File “”, line 1, in   
> > TypeError: int argument required
> > 
> > This is due to the fact that:
> > 
> > > > > print mol.numAtoms  
> > > > > \<property object at 0xb10d2464\>
> > 
> > Is this the normal behavior? I was expecting “mol.numAtoms” to be an  
> > (int) in fact but instead it seems to be an instance of a (undocumented)  
> > “property” class. How do I deal with these objects? Or is this exposure  
> > of a “property” (instead of a integer (or other data types)) a bug in  
> > the python interface? Do I go ahead and report a bug?
> > 
> > Thank you in advance for your care.  
> > Best regards,  
> > Alfredo Carvalho
> > 
> > * * *
> > 
> > Crystal Reports - New Free Runtime and 30 Day Trial  
> > Check out the new simplified licensing option that enables  
> > unlimited royalty-free distribution of the report engine  
> > for externally facing server and web deployment.  
> > [http://p.sf.net/sfu/businessobjects](http://p.sf.net/sfu/businessobjects)
> > 
> > * * *
> > 
> > Avogadro-devel mailing list  
> > [Avogadro-devel@lists.sourceforge.net](mailto:Avogadro-devel@lists.sourceforge.net)  
> > [avogadro-devel List Signup and Options](https://lists.sourceforge.net/lists/listinfo/avogadro-devel)

---

<div class="post-metadata">

**Author:** ![Tim\_Vandermeersch](https://discuss.avogadro.cc/user_avatar/discuss.avogadro.cc/tim_vandermeersch/32/3479_2.png) [@Tim\_Vandermeersch](https://discuss.avogadro.cc/u/Tim_Vandermeersch)\
**Post date:** [May 21, 2009, 5:29pm UTC](https://discuss.avogadro.cc/t/issue-with-python-terminal/1355/4 "2009-05-21T17:29:07Z")

</div>

On Thu, May 21, 2009 at 5:00 PM, Alfredo Palace Carvalho  
[ajpalace@uevora.pt](mailto:ajpalace@uevora.pt) wrote:

> Hi,
> 
> Thanks for the prompt reply.  
> This is a problem I’ve been struggling with for quite a long time  
> (maybe from the switch from 0.8 versions to 0.9 versions) and  
> has been stoping me from running my python scripts, because, basically  
> I’ve been unable to do anything useful with python…

The Avogadro.molecule variable is only set when using the python  
terminal at the

> Now I am a bit confused and I realize I do not know the basics  
> of python scripting in Avogadro.  
> At first, when I retried to run those statements (in the Python  
> Terminal) following your instructions I was very glad to see that it worked.  
> I could not believe it was such a basic thing. I have reminiscences  
> that in the past (i.e. avogadro 0.8.x) I used Avogadro.molecule  
> to refer to the current molecule in scripts , but after a certain  
> upgrade of Avogadro my python scripts stoped working (due to severe  
> changes in the programming interface, i.e. the introduction  
> of the Extension class, etc.) and following the documentation  
> online and examples scripts I could never get them to work  
> anymore. Probably then I made this change (Avogadro.molecule  
> to Avogadro.Molecule) and saw fewer error messages and assumed  
> I was on the right path.

Yes, there have been some drastic changes but these where needed to  
allow python scripts to be used as Tools, Engines (Display types) and  
Extensions. Apart from some minor changes (user suggestions), the  
python API will remain unchanged for 1.0.

> Anyway, now I made this change in one of my test python extension  
> scripts (which I include in attachment) and I am still unable  
> to run it. I now realize that what I still can’t (don’t know how)  
> to do is to refer to the current molecule inside the performAction  
> method. “Avogadro.molecule” works inside the Python Terminal  
> but not inside the “Extension” class’s performAction method.  
> Running my code gives the following error:
> 
> AttributeError: ‘module’ object has no attribute ‘molecule’

The Avogadro.molecule variable is only set when using the python  
terminal. It is provided for convenience. When performing an action in  
performAction, you can get the molecule like this:

def performAction(self, action, glwidget):  
mol = glwidget.molecule

However, since we have a static method to get the current GLWidget, I  
could add the Avogadro.molecule directly to the python module. This  
way Avogadro.molecule would also be available in standalone scripts.

> Could you please take a look at my script? It is fairly simple.  
> If I can refer to the current molecule in this script I think  
> I can work out the rest by myself, but I could not find any  
> instructions on how to do it in the documentation/examples.  
> In the examples I have seen, either they work by creating a new  
> molecule or the python code in the examples is meant for the  
> python Terminal but do not work (apparently) inside an extension  
> script. Maybe I have overlooked something in the documentation but  
> I feel stuck just in the beginning of my scripts!!!

There are some examples on the wiki page  
([http://avogadro.openmolecules.net/wiki/Python\_Extensions](http://avogadro.openmolecules.net/wiki/Python_Extensions)). I will try  
to find time to add more examples but if you have some examples you  
can contribute, it would be greatly appreciated.

Thanks,  
Tim

> Thank you very much.
> 
> Cheers,  
> Alfredo
> 
> On Wed, 20 May 2009, Tim Vandermeersch wrote:
> 
> > Hi,
> > 
> > On Wed, May 20, 2009 at 8:33 PM, Alfredo Palace Carvalho  
> > [ajpalace@uevora.pt](mailto:ajpalace@uevora.pt) wrote:
> > 
> > > Hi all,
> > > 
> > > I am back with an old problem of mine (in fact, I haven’t had  
> > > the time to look into it lately, but still cannot solve it anyhow).
> > > 
> > > If I open a molecule in avogadro and execute the following python  
> > > statements in the Python Terminal:
> > > 
> > > > > > import Avogadro  
> > > > > > mol=Avogadro.Molecule
> > 
> > If you want to get the current molecule, use Avogadro.molecule (with a  
> > small ‘m’). If you use Molecule, you get the class definition and not  
> > the instance of the molecule. As a rule of thumb, classes start with  
> > capital letters instances with lower case letters.
> > 
> > There is also Avogadro.molecules, which is an instance of the  
> > MoleculeList class (python only). Use this class like:
> > 
> > > > > newMol = Avogadro.molecules.addMolecule()
> > 
> > to create a new molecule if needed. (It makes sure the molecule  
> > doesn’t get deleted when you assigned it to a GLWidget for example.)
> > 
> > Tim
> > 
> > > > > > print “num atoms = %d” % mol.numAtoms
> > > 
> > > I get the following error message:
> > > 
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > TypeError: int argument required
> > > 
> > > This is due to the fact that:
> > > 
> > > > > > print mol.numAtoms
> > > 
> > > \<property object at 0xb10d2464\>
> > > 
> > > Is this the normal behavior? I was expecting “mol.numAtoms” to be an  
> > > (int) in fact but instead it seems to be an instance of a (undocumented)  
> > > “property” class. How do I deal with these objects? Or is this exposure  
> > > of a “property” (instead of a integer (or other data types)) a bug in  
> > > the python interface? Do I go ahead and report a bug?
> > > 
> > > Thank you in advance for your care.  
> > > Best regards,  
> > > Alfredo Carvalho
> > > 
> > > * * *
> > > 
> > > Crystal Reports - New Free Runtime and 30 Day Trial  
> > > Check out the new simplified licensing option that enables  
> > > unlimited royalty-free distribution of the report engine  
> > > for externally facing server and web deployment.  
> > > [http://p.sf.net/sfu/businessobjects](http://p.sf.net/sfu/businessobjects)
> > > 
> > > * * *
> > > 
> > > Avogadro-devel mailing list  
> > > [Avogadro-devel@lists.sourceforge.net](mailto:Avogadro-devel@lists.sourceforge.net)  
> > > [avogadro-devel List Signup and Options](https://lists.sourceforge.net/lists/listinfo/avogadro-devel)

---

<div class="post-metadata">

**Author:** ![Alfredo\_Palace\_Carva](https://discuss.avogadro.cc/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@Alfredo\_Palace\_Carva](https://discuss.avogadro.cc/u/Alfredo_Palace_Carva)\
**Post date:** [May 23, 2009, 3:01pm UTC](https://discuss.avogadro.cc/t/issue-with-python-terminal/1355/5 "2009-05-23T15:01:27Z")

</div>

Hi,

I had to reply just to say many thanks!  
I was really stuck! I am a relatively newbie working with avogadro  
and am still trying to get acquainted with its “guts”. But, you  
realize that without dealing with this problem there wasn’t much  
I could do.

Sure, now as I browse the documentation, I see references to this  
glwidget object. But, before, I never paid much attention to it  
(its name sounded like something quite internal to avogadro, it  
didn’t sound anything like what I was looking for). It is always  
easier to look after things once you have found them … 🙂  
Even that protein.py example in the page you pointed at  
([http://avogadro.openmolecules.net/wiki/Python\_Extensions](http://avogadro.openmolecules.net/wiki/Python_Extensions))  
it was just what I was looking for! Was it always there???  
Sorry for wasting your time by not looking properly at the avogadro’s  
website.

Anyway, if you allow me a suggestion: it would be useful for anyone  
reading the docs about python scripting to have a reference to this  
glwidget right at the beginning of introductory docs. Just a single  
sentence would suffice calling attention to it and explaining its role.  
I assume the glwidget is essential, as it is the entry point to  
any objects that you have on the screen, right? So if you are  
getting started with python scripting, that should be a place  
where you get started at.

I think that the introductory text on python scripting is oriented  
towards the Python Terminal. It refers to the Avogadro.molecule  
and not to glwidget.molecule.

As you explained, we have the Avogadro.molecule variable as a  
convenience in Python Terminal but not in the python standalone  
scrits. In my experience, this can be a source of confusion as  
one would expect (at least I did) a uniform behavior inside the  
Python Terminal and inside python scripts. It doesn’t matter  
so much if you refer to something named “Avogadro.molecule” or  
“glwidget.molecule” but I would expect it to be present both in  
the terminal and inside a script.

On Thu, 21 May 2009, Tim Vandermeersch wrote:

> Yes, there have been some drastic changes but these where needed to  
> allow python scripts to be used as Tools, Engines (Display types) and  
> Extensions. Apart from some minor changes (user suggestions), the  
> python API will remain unchanged for 1.0.

I didn’t mean this as a criticism, I was just pointing out the fact.  
I know that these changes, although they do require some extra work  
in adapting the scripts, they are meant for a greater good ultimately!

Anyway, I reiterate my thanks for your help (and sorry for the length  
of this message!) and extend it to the developers of Avogadro, which  
is becoming a nice software. Maybe when I get more familiar with it  
I can give my contribution too.

Cheers,  
Alfredo
