Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:13:23+00:00 2026-05-28T04:13:23+00:00

I looked through previous questions to this effect, but my situation is slightly different…

  • 0

I looked through previous questions to this effect, but my situation is slightly different…

I’ve got a directory that contains several python classes that import one another here and there where necessary. I’d like to start packaging them up into smaller modules, mostly for purposes of organization. The central component of the package sets up a wsgi application and then additional modules provide functionality to it (this has worked great so far since I can test the mods individually on the command line before hooking their inputs up to the web app.)

I understand how supplying an __init__.py module works, its namespacing, and when it releases control. What I am having trouble with is that the modules in certain cases import one another.

For example, I have a JSONEncoder that is used as the cls argument for many calls to json.dumps() This class is used in virtually every file. If I move some of these modules to subdirectories within the package, how would I go “up one level” to import the json encoder? Would I need to put the parent directory on the PYTHONPATH in every file? Would it be better to give the encoder its own directory as well, with a separate __init__.py file?

Alternatively, would I just be better off putting in a function that drills down through all directories below the current working dir and adds them to the path?

Currently the main directory that contains all the code is not package in of itself… what would be the best approach to migrating like classes to subdirectories taking into account that they have circular imports?

EDIT:

For clarification:

The main working directory I’ve been developing is just a regular directory containing about ten .py files, all of which contain classes with names like their filenames. Currently the whole thing is just a git repo of .py files.

-project
  search.py
  jsonencoder.py
  webapp.py
  modela.py
  modelb.py
  modelc.py

Names are of course for example purposes.

Now as I understand it (which could be totally wrong) the sensible thing to do would be to package those models together, correct?

-project
  search.py
  jsonencoder.py
  webapp.py
  -models
    __init__.py
    modela.py
    modelb.py
    modelc.py

But then in turn, would the project folder need an __init__ too? How would modela, which uses jsonencoder, import it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:13:23+00:00Added an answer on May 28, 2026 at 4:13 am

    The answer is quite simple:

    just use the fully qualified name for the sub-module you need

    For example, under

    mybundle/test/__init__.py 
    

    you can have the line

    import mybundle.JSONEncoder
    
    • it will do just the right thing.

    For example, given this module structure:

    ├── mybundle
    │   ├── __init__.py
    │   ├── jsonencoder
    │   │   ├── __init__.py
    │   └── test
    │       ├── __init__.py
    

    with the files defined as in:

    [gwidion@powerpuff tmp]$ cat mybundle/__init__.py
    import test
    
    [gwidion@powerpuff tmp]$ cat mybundle/test/__init__.py
    import mybundle.jsonencoder
    
    [gwidion@powerpuff tmp]$ cat mybundle/jsonencoder/__init__.py
    print 5
    

    This works:

    [gwidion@powerpuff tmp]$ python
    Python 2.7.1 (r271:86832, Apr 12 2011, 16:15:16) 
    [GCC 4.6.0 20110331 (Red Hat 4.6.0-2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import mybundle
    5
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I looked through the existing questions of this kind but didn't find what I
I've looked but didn't find previous questions specific enough, so sorry if this is
This may be an easy one. I looked through previous questions (and other places
I have looked through as many previous questions as possible but never saw a
I've got an issue with my rspec tests and having looked through previous questions
I've looked through many of the existing threads about this error, but still no
Still a mysql newb and I looked extensively through previous questions trying to find
I've looked through previous questions and none of them have really worked for me,
Ran into another challenge. I looked through some of the questions that I found
I am aware that similar questions to this have been asked, but I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.