Re: Importing class from another file

Discussion in 'Python' started by Tobias M., Jan 23, 2013.

  1. Tobias M.

    Tobias M. Guest

    Hi,

    Kevin Holleran wrote:
    >
    > 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
    >
    >

    Make sure, the script you execute by passing it to the python
    interpreter is in the parent directory of Sub_Dir.
    Additionaly put an empty file called __init__.py (double underscores!)
    in Sub_Dir if you don't already have. This is necessary to use Sub_Dir
    as a package and import modules from it.

    For more details see:
    http://docs.python.org/3.3/tutorial/modules.html#packages

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

    When using "from...import" on the module that contains the class, you
    can use the class without the package identifier:
    q_api = My_Class_Connector(string1,string2)

    For your information: I think beginner questions like this one should be
    asked on the tutor list:
    http://mail.python.org/mailman/listinfo/tutor
     
    Tobias M., Jan 23, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cyril
    Replies:
    2
    Views:
    3,878
    cyril
    Aug 25, 2004
  2. plb
    Replies:
    2
    Views:
    358
  3. Gary Oberbrunner
    Replies:
    1
    Views:
    264
    Steven D'Aprano
    Apr 28, 2009
  4. Steven D'Aprano
    Replies:
    0
    Views:
    320
    Steven D'Aprano
    Apr 29, 2009
  5. Kevin Holleran

    Importing class from another file

    Kevin Holleran, Jan 22, 2013, in forum: Python
    Replies:
    1
    Views:
    179
    John Gordon
    Jan 22, 2013
Loading...

Share This Page