Importing class from file in package

  • Thread starter Florian Lindner
  • Start date
F

Florian Lindner

Hello,
I've two files in my package.
In the first file I want to inport a class which is declared in the
second file. How can do that without stating the absolute path to the
file, just the relative one?

Thanks,

Florian
 
P

Peter Hansen

Florian said:
I've two files in my package.
In the first file I want to inport a class which is declared in the
second file. How can do that without stating the absolute path to the
file, just the relative one?

Assuming you mean by "package" that you have a folder named, say,
"mypkg" and an __init__.py file in it (that last part is required
for it to be a package in Python), and files a.py and b.py, this
works just fine if you do nothing special:

file mypkg/a.py contains:

class A:
pass


file mypkg/b.py contains:

import a
print a.A()

If I run the interpreter in the folder above mypkg,
this works just fine:
<mypkg.a.A instance at 0x00AE8120>


So that leads me to think that either you didn't try this,
or I misunderstood the question...

-Peter
 
J

Joe Raul

from second import class


--
======================

best regards

======================
 

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

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top