0

How do I import a class from main.py into another file in the same directory?

File structure:

Project
    |-- main.py
    |-- first_file.py
    |-- second_file.py

main.py:

class ClassX():
    def random1(self):
        ....
        return stuff

    def get(self):
        class_x_stuff = self.random1()
        print class_x_stuff

app = webapp2.WSGIApplication([
    ('/classw', ClassW),
    ('/classx', ClassX)
], debug=True)

first_file.py:

import main

checkpoint = main.ClassX()
print checkout

Output:

AttributeError: 'module' object has no attribute 'ClassX'

Resources I've used:

  1. How to import the class within the same directory or sub directory?
  2. AttributeError: 'module' object has no attribute

I'm able to import the first_file.py into main.py as well as into second_file.py. But I am unable to import main.py into the other files.

Renaming the main.py file, (for example, proj_main.py) results in an error:

Traceback (most recent call last):
File "/usr/local/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
  handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/usr/local/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
  handler, path, err = LoadObject(self._handler)
File "/usr/local/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
  obj = __import__(path[0])

ImportError: No module named main

What are my options? How can I resolve this issue?

EDIT:

Full Traceback:

INFO     2017-10-02 22:10:21,744 dispatcher.py:226] Starting module "default" running at: http://localhost:7070
INFO     2017-10-02 22:10:21,747 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR    2017-10-02 22:10:24,017 wsgi.py:263] 
Traceback (most recent call last):
  File "/usr/local/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/usr/local/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
  File "/usr/local/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
  File "/Users/mycomputer/Documents/project_folder/main.py", line 34, in <module>
from first_file import ClassY
  File "/Users/mycomputer/Documents/project_folder/first_file.py", line 20, in <module>
    checkpoint = main.ClassX()
AttributeError: 'module' object has no attribute ‘ClassX’
INFO     2017-10-02 22:10:24,023 module.py:832] default: "GET / HTTP/1.1" 500 -
fragilewindows
  • 1,394
  • 1
  • 15
  • 26
  • How does `first_file.py` get executed (when you get the `AttributeError`)? – martineau Oct 02 '17 at 22:12
  • `Class ClassX():` -> `class ClassX:`: lower case _c_ and (optionally) no braces. – CristiFati Oct 02 '17 at 22:23
  • That was actually just an error in typing but in my code it is lowercase: `class` so the issue isn't that. – fragilewindows Oct 02 '17 at 22:29
  • @martineau `first_file.py` isn't running at all. When I run the application in localhost the result is: `This page isn’t working 127.0.0.1 is currently unable to handle this request. HTTP ERROR 500` – fragilewindows Oct 02 '17 at 22:32
  • In that case please [edit] your question again and add the full traceback that appears before the `AttributeError: 'module' object has no attribute 'ClassX'` exception. – martineau Oct 03 '17 at 00:33
  • @martineau I added the full traceback and realized I have circular dependency. I am able to fix it now, thanks. – fragilewindows Oct 03 '17 at 10:40

1 Answers1

0

The problem is that I was experiencing circular dependency:

File "/Users/mycomputer/Documents/project_folder/main.py", line 34, in <module>
from first_file import ClassY

Note that main.py was importing from first_file and then first_file was importing from main.py which caused a circular import gotcha.

Solution:

first_file.py

import second_file

checkpoint = second_file.class_x()
    print checkout

second_file.py

import main

def class_x():
    item = main.ClassX()
    return item
fragilewindows
  • 1,394
  • 1
  • 15
  • 26