Python Forum
Bringing two files together
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bringing two files together
#1
I have 2 files that I'm trying to join. I've tried this with the files below. I've also tried this with the files provided by the instructor - still no dice. I've set this up in a virtual environment so I would have more control in what was installed.

class A:
    def spam(self):
        print('A.spam')
from .a import A

class B(A)
    def bar(self):
        print('B.bar')
from .a import A
from .b import B
This is the error I get:

Error:
C:\Python365\python.exe C:/LearningEnv/LearningEnv/10/splitting_a_module_into_multiple_files/TJN_Splitting_Module/__init__.py Traceback (most recent call last): File "C:/LearningEnv/LearningEnv/10/splitting_a_module_into_multiple_files/TJN_Splitting_Module/__init__.py", line 2, in <module> from .a import A ModuleNotFoundError: No module named '__main__.a'; '__main__' is not a package Process finished with exit code 1
As always - your help is most appreciated!
Reply
#2
I suspect that the class A is in a file named a.py and the class B is in a file named b.py, right?
remove the . infront of the a and b. What it does right now is that it looks in the __main__ module where it doesn't find the files a and b. Where did you place the files? Are they in the same directory? If so, just write:
from a import A
 
class B(A)
    def bar(self):
        print('B.bar')
Reply
#3
I must be missing something because I've done as you instructed.

from a import A


class B(A)
    def bar(self):
        print('B.bar')
and this is the error I get. I also tried copying yours - still nada.

Error:
C:\Python365\python.exe C:/LearningEnv/LearningEnv/10/splitting_a_module_into_multiple_files/TJN_Splitting_Module/Theif_of_time.py File "C:/LearningEnv/LearningEnv/10/splitting_a_module_into_multiple_files/TJN_Splitting_Module/Theif_of_time.py", line 4 class B(A) ^ SyntaxError: invalid syntax Process finished with exit code 1
Is there a plugin I might be missing?

Again - thanks for your help!
Reply
#4
import a
 
 
class B:
    def __init__(self):
        self.a = a.A()
 
    def bar(self):
        print('B.bar')

    def bspam(self):
        self.a.spam()

if __name__ == '__main__':
    b = B()
    b.bspam()
output:
Output:
A.spam
Reply
#5
(Oct-12-2018, 02:21 PM)tjnichols Wrote: I must be missing something because I've done as you instructed.

from a import A


class B(A)
    def bar(self):
        print('B.bar')
and this is the error I get. I also tried copying yours - still nada.

Error:
C:\Python365\python.exe C:/LearningEnv/LearningEnv/10/splitting_a_module_into_multiple_files/TJN_Splitting_Module/Theif_of_time.py File "C:/LearningEnv/LearningEnv/10/splitting_a_module_into_multiple_files/TJN_Splitting_Module/Theif_of_time.py", line 4 class B(A) ^ SyntaxError: invalid syntax Process finished with exit code 1
Is there a plugin I might be missing?

Again - thanks for your help!

The error explains what the issue is. class B(A) is a syntax error. You need a colon at the end of that line.
Reply
#6
Lars60+ - I'm sorry I missed your post.

import a
  
  
class B:
    def __init__(self):
        self.a = a.A()
  
    def bar(self):
        print('B.bar')
 
    def bspam(self):
        self.a.spam()
 
if __name__ == '__main__':
    b = B()
    b.bspam()
This is so much more than what is in the book. It was written by David Beazley so I know it's old because he is writing a newer edition. Can you point me in the direction of where you learned this so I can learn how and why you did it this way?

As always - I appreciate your help!
Reply
#7
nilamo - that did it! THANK YOU!!!

Lars60+ - I would still like to know where you got the code you posted. I understand you wrote it, but I would be very interested to know where you learned.

Thank all of you!
Reply
#8
Which part of the code are you curious about? There really isn't anything special there, B is keeping a reference to an instance of A, instead of inheriting from A.
Reply
#9
Lars60+ - I would still like to know where you got the code you posted. I understand you wrote it, but I would be very interested to know where you learned.

I wrote it. It's standard class layout.
to do it the way you were trying, that is to use another class as object of a class, you need to initialize the object
within the init of the new class.

As far as where I learned how to do this, I don't have a clue, coming from c++ background, I was already familiar with the basic concepts.

David Beazley is a Great author, and an even better speaker! You should be able to get his code to work without problems, perhaps re-read the section.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020