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

  • Home
  • SEARCH
  • 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 7018493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:03:30+00:00 2026-05-27T23:03:30+00:00

I have a module called spellnum . It can be used as a command-line

  • 0

I have a module called spellnum. It can be used as a command-line utility (it has the if __name__ == '__main__': block) or it can be imported like a standard Python module.

The module defines a class named Speller which looks like this:

class Speller(object):
    def __init__(self, lang="en"):
        module = __import__("spelling_" + lang)
        # use module's contents...

As you can see, the class constructor loads other modules at runtime. Those modules (spelling_en.py, spelling_es.py, etc.) are located in the same directory as the spellnum.py itself.

Besides spellnum.py, there are other files with utility functions and classes. I’d like to hide those files since I don’t want to expose them to the user and since it’s a bad idea to pollute the Python’s lib directory with random files. The only way to achieve this that I know of is to create a package.


I’ve come up with this layout for the project (inspired by this great tutorial):

spellnum/                # project root
    spellnum/            # package root
        __init__.py
        spellnum.py
        spelling_en.py
        spelling_es.py
        squash.py
        # ... some other private files
    test/
        test_spellnum.py
    example.py

The file __init__.py contains a single line:

from spellnum import Speller

Given this new layout, the code for dynamic module loading had to be changed:

class Speller(object):
    def __init__(self, lang="en"):
        spelling_mod = "spelling_" + lang
        package = __import__("spellnum", fromlist=[spelling_mod])
        module = getattr(package, spelling_mod)
        # use module as usual

So, with this project layout a can do the following:

  1. Successfully import spellnum inside example.py and use it like a simple module:

    # an excerpt from the example.py file
    import spellnum
    
    speller = spellnum.Speller(es)
    # ...
    
  2. import spellnum in the tests and run those tests from the project root like this:

    $ PYTHONPATH="`pwd`:$PYTHONPATH" python test/test_spellnum.py
    

The problem

I cannot execute spellnum.py directly with the new layout. When I try to, it shows the following error:

Traceback (most recent call last):
  ...
  File "spellnum/spellnum.py", line 23, in __init__
    module = getattr(package, spelling_mod)
AttributeError: 'module' object has no attribute 'spelling_en'

The question

What’s the best way to organize all of the files required by my module to work so that users are able to use the module both from command line and from their Python code?

Thanks!

  • 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-27T23:03:31+00:00Added an answer on May 27, 2026 at 11:03 pm

    How about keeping spellnum.py?

    spellnum.py
    spelling/
      __init__.py
      en.py
      es.py
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a module called MyApp, and another module called MyAppTests which has a
I have a function defined in a module called DataAccess like this module DataAccess
I am using a popular module called Computed Field in Drupal. I have used
Please forgive my gherkin Given I have a module called Hello And it has
I have a module called Plants which has a const_missing method and a class
Let's consider we have a module called mod which has a function func .
I have a django application, with a module called app Now this module has
I have a mex module called p.mexw64 which uses another dll called p.dll The
i have a module (a single .py file, actually), with a class called HashedDir.
I'm working on a Rails application. I have a Module called Animals. Inside this

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.