0

I'm trying to do the following:

File 1:
  class x:
    def somefunc(self):
      # Some code
      ect...

File 2:
  import File 1
  # Inherits x
  class y(File1.x):
      # Some code
      ect...

But this raises an error:

"name 'x' is not defined"

Edit: Changed x to File1.x. Still not working

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tracing
  • 1
  • 1
  • 1
    You'll need to give us the proper exception here; you will no longer have a `NameError` in your edited code. – Martijn Pieters Dec 15 '13 at 10:55
  • @Tracing This is a bit of a wild guess, but maybe you are having problems because of spaces in your file names? See my answer below, I edited it to include some more information about this. – itsjeyd Dec 16 '13 at 08:10

2 Answers2

4

You imported the module into your namespace; x is an attribute of the module:

import modulename

class y(modulename.x):

Alternatively, use the from modulename import syntax to bind objects from a module into your local namespace:

from modulename import x

class y(x):
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

You need to do from file1 import x or class y(file1.x) to make this work.

EDIT: Make sure you have no spaces in your file names. Maybe it's just a typo in your question, but at the top of File2 you are saying import File 1 instead of import File1. If the name of your Python module corresponding to File1 does indeed contain one or more spaces, you should remove them (or replace them with underscores), both in the file name and the import statement. As explained in the accepted answer to this question, file names are used as identifiers for imported modules, and Python identifiers can't contain spaces.

Community
  • 1
  • 1
itsjeyd
  • 5,070
  • 2
  • 30
  • 49