Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37340 > unrolled thread

Importing class from another file

Started byKevin Holleran <kdawg44@gmail.com>
First post2013-01-22 14:33 -0500
Last post2013-01-22 15:44 -0500
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Importing class from another file Kevin Holleran <kdawg44@gmail.com> - 2013-01-22 14:33 -0500
    Re: Importing class from another file John Gordon <gordon@panix.com> - 2013-01-22 19:47 +0000
      Re: Importing class from another file Kevin Holleran <kdawg44@gmail.com> - 2013-01-22 15:44 -0500

#37340 — Importing class from another file

FromKevin Holleran <kdawg44@gmail.com>
Date2013-01-22 14:33 -0500
SubjectImporting class from another file
Message-ID<mailman.830.1358883233.2939.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hello,

I have a class called My_Class in a subdir called Sub_Dir.

in My_Class.py is the following

class My_Class_Connector:

    def __init__(self,un,pw,qs_srv="domain.com"):

        self.username = un

        self.password = pw

...



Then I am trying to call from a script in the parent dir like this:

from Sub_Dir.My_Class import *

q_api = My_Class.My_Class_Connector(string1,string2)


I have not worked much with Python classes so I am not sure what I am doing
wrong.  When running this from my script, I get the following error:


Traceback (most recent call last):

  File "testing.py", line 1, in <module>

    from Sub_Dir.My_Class import *

ImportError: No module named Sub_Dir.My_Class


I have played around a bit with the calls (removing the My_Class in the
q_api assignment to instantiate the object, etc).


Thanks for any help.


Kevin

[toc] | [next] | [standalone]


#37342

FromJohn Gordon <gordon@panix.com>
Date2013-01-22 19:47 +0000
Message-ID<kdmqcm$rd8$1@reader1.panix.com>
In reply to#37340
In <mailman.830.1358883233.2939.python-list@python.org> Kevin Holleran <kdawg44@gmail.com> writes:

> I have a class called My_Class in a subdir called Sub_Dir.

> in My_Class.py is the following

> class My_Class_Connector:
>     def __init__(self,un,pw,qs_srv="domain.com"):
>         self.username = un
>         self.password = pw

> Then I am trying to call from a script in the parent dir like this:

> from Sub_Dir.My_Class import *

> q_api = My_Class.My_Class_Connector(string1,string2)

Even if your import had worked, this would be wrong.  You're importing
everything from Sub_Dir.My_Class, so My_Class_Connector is in the current
namespace.  You don't need to add "My_Class." on the front (and in fact
it's an error to do so.)

> Traceback (most recent call last):
>   File "testing.py", line 1, in <module>
>     from Sub_Dir.My_Class import *
> ImportError: No module named Sub_Dir.My_Class

Is there a file named __init__.py in Sub_Dir?  A directory must contain
that file in order to be considered a "module".  (If you don't know what
to put in the file, just leave it empty.)

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#37348

FromKevin Holleran <kdawg44@gmail.com>
Date2013-01-22 15:44 -0500
Message-ID<mailman.836.1358887486.2939.python-list@python.org>
In reply to#37342

[Multipart message — attachments visible in raw view] — view raw

Thanks, you got me straightened out.


--
Kevin Holleran
Master of Science, Computer Information Systems
Grand Valley State University
Master of Business Administration
Western Michigan University
SANS GCFA, SANS GCFE, CCNA, ISA, MCSA, MCDST, MCP

"Do today what others won't, do tomorrow what others can't" - SEALFit

"We are what we repeatedly do. Excellence, then, is not an act, but a
habit." - Aristotle


On Tue, Jan 22, 2013 at 2:47 PM, John Gordon <gordon@panix.com> wrote:

> In <mailman.830.1358883233.2939.python-list@python.org> Kevin Holleran <
> kdawg44@gmail.com> writes:
>
> > I have a class called My_Class in a subdir called Sub_Dir.
>
> > in My_Class.py is the following
>
> > class My_Class_Connector:
> >     def __init__(self,un,pw,qs_srv="domain.com"):
> >         self.username = un
> >         self.password = pw
>
> > Then I am trying to call from a script in the parent dir like this:
>
> > from Sub_Dir.My_Class import *
>
> > q_api = My_Class.My_Class_Connector(string1,string2)
>
> Even if your import had worked, this would be wrong.  You're importing
> everything from Sub_Dir.My_Class, so My_Class_Connector is in the current
> namespace.  You don't need to add "My_Class." on the front (and in fact
> it's an error to do so.)
>
> > Traceback (most recent call last):
> >   File "testing.py", line 1, in <module>
> >     from Sub_Dir.My_Class import *
> > ImportError: No module named Sub_Dir.My_Class
>
> Is there a file named __init__.py in Sub_Dir?  A directory must contain
> that file in order to be considered a "module".  (If you don't know what
> to put in the file, just leave it empty.)
>
> --
> John Gordon                   A is for Amy, who fell down the stairs
> gordon@panix.com              B is for Basil, assaulted by bears
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web