0

My code is currently in one big file:

# file 1: ./mainfile.py
class Namespace:
    class This:
        ...
    class That:
        ...

mainFunc(Namespace.This(), Namespace.That())

I would like my code to look something like this:

# file 1: ./namespace/this.py
class This:
    ...

# file 2: ./namespace/that.py
class That:
    ...

# file 3: ./mainfile.py
import ???
mainFunc(Namespace.This(), Namespace.That())

What changes do I have to make so that I can still use exactly the same call to mainFunc()?

1 Answer 1

1

You need to make a package. See the documentation.

Sign up to request clarification or add additional context in comments.

4 Comments

I got to this part. I put the class This in the file namespace/this.py, but when I import namespace.this in the main file, I get an error saying ImportError: No module named namespace
@f.ardelian: did you add the __init__.py file into the directory namespace?
Okay, that solved one thing. Now, do you know anything about using Namespace.This() instead of Namespace.this.This()?
I believe in Python, if you want them to be in the same namespace, the proper way to do it is to put all of the code in a namespace.py module file. I know that coming from other languages you might have a desire to have separate files, but that's not Python. (You can however accomplish this with some hacking in the __init__.py file, but I don't recommend it.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.