Importing class from another file

K

Kevin Holleran

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
 
J

John Gordon

In said:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top