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 656869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:45:41+00:00 2026-05-13T22:45:41+00:00

I have a Python project with following directory structure: /(some files) /model/(python files) /tools/(more

  • 0

I have a Python project with following directory structure:

/(some files)
/model/(python files)
/tools/(more python files)
...

So, I have Python files in couple subdirectories and there are some
dependencies between directories as well: tools are used by model, etc. Now
my problem is that I want to make doctests for both models and tools,
and I want be able to run tests from command line like this: ./model/car.py .
I can make this work, but only with messy boilerplate code. I would like
to know what is the correct way, or is there any?

Question: How should I write my imports?

Thanx. Here is an example…

Content of tools/tool.py:

#!/usr/bin/env python
"""
   >>> is_four(21)
   False
   >>> is_four(4)
   True
"""

def is_four(val):
    return val == 4

if __name__ == '__main__':
    import doctest
    doctest.testmod()

… and model/car.py:

#!/usr/bin/env python   
"""
   >>> car = Car()
   >>> car.ok()
   True
"""

from tools.tool import *

class Car(object):
    def __init__(self):
        self.tire_count = 4
    def ok(self):
        return is_four(self.tire_count)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

By adding following lines in the begin of car.py it works, but doesn’t look nice. 🙁

if __name__ == '__main__':
    import sys
    import os
    sys.path.append(os.path.abspath(os.path.dirname('..')))
  • 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-13T22:45:42+00:00Added an answer on May 13, 2026 at 10:45 pm

    What you are trying to do is a relative import. It works fine in Python, but on the module level, not on the file system level. I know, this is confusing.

    It means that if you run a script in a subdir, it doesn’t see the upper dirs because for the running script, the root of the module is the current dir: there is no upper module.

    So what are relative imports for?

    Well, module in subdirs car import module in upper dirs as long as they are themself imported from a upperdir.

    In your case it means you must run your scripts from “/” so it becomes the root of the module, and the submodules are allowed to use relative import.

    A possible solution to your problem is to remove your if __name__ == "__main__" block and create /tests.py:

    import doctest
    from model import car
    from tools import tool
    
    doctest.testmod(car)
    doctest.testmod(tool)
    

    Then run in too launch all the tests.

    Ultimately you will want to automatize the process, a simple solution is to use unittest so you can create test suites and just add the module names you want to test:

    import unittest
    import doctest
    
    modules = ("model.car", 
               "tools.tool")
    
    suite = unittest.TestSuite()
    for mod in modules:
        suite.addTest(doctest.DocTestSuite(mod))
    runner = unittest.TextTestRunner()
    runner.run(suite)
    

    Another solution (recommended) is to use a tool such as nose that automates this for you.

    easy_install nose
    nosetests --with-doctest # done :-)
    

    And by the way, avoid from x import *. This works for quick scripts, but when your program will grow, you really will need to explicitly name what you import. Either import x or from x import y

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Assuming that I have the following directory structure for a Python project: config/ scripts/
I have a Python project that has the following structure: package1 class.py class2.py ...
OK, I have the following directory structure (it's a django project): -> project -->
I have a Visual Studio project which uses nmake to call a Python file
What is the correct way to fix this ImportError error? I have the following
Suppose a programmer has the following problem: he wants to start a new python
I have a Python script, named script.py . It's located on ~/scripts/script.py . I
I have been receiving errors when trying to save and run this Python 3.1
I maintain a Django project with a database that has several model constraints that
I'm building a small web project using Django that has one model ( Image

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.