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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:57:44+00:00 2026-05-12T10:57:44+00:00

Ok, I think whatever I’m doing wrong, it’s probably blindingly obvious, but I can’t

  • 0

Ok, I think whatever I’m doing wrong, it’s probably blindingly obvious, but I can’t figure it out. I’ve read and re-read the tutorial section on packages and the only thing I can figure is that this won’t work because I’m executing it directly. Here’s the directory setup:

eulerproject/
  __init__.py
  euler1.py
  euler2.py
  ...
  eulern.py
  tests/
    __init__.py
    testeulern.py

Here are the contents of testeuler12.py (the first test module I’ve written):

import unittest
from .. import euler12

class Euler12UnitTests(unittest.TestCase):


    def testtriangle(self):
        """
        Ensure that the triangle number generator returns the first 10
        triangle numbers.

        """
        self.seq = [1,3,6,10,15,21,28,36,45,55]
        self.generator = euler12.trianglegenerator()
        self.results = []
        while len(self.results) != 10:
            self.results.append(self.generator.next())
        self.assertEqual(self.seq, self.results)

    def testdivisors(self):
        """
        Ensure that the divisors function can properly factor the number 28.

        """
        self.number = 28
        self.answer = [1,2,4,7,14,28]
        self.assertEqual(self.answer, euler12.divisors(self.number))


if __name__ == '__main__':

    unittest.main()

Now, when I execute this from IDLE and from the command line while in the directory, I get the following error:

Traceback (most recent call last):
  File "C:\Documents and Settings\jbennet\My Documents\Python\eulerproject\tests\testeuler12.py", line 2, in <module>
    from .. import euler12
ValueError: Attempted relative import in non-package

I think the problem is that since I’m running it directly, I can’t do relative imports (because __name__ changes, and my vague understanding of the packages description is that __name__ is part of how it tells what package it’s in), but in that case what do you guys suggest for how to import the ‘production’ code stored 1 level up from the test code?

  • 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-12T10:57:44+00:00Added an answer on May 12, 2026 at 10:57 am

    Generally you would have a directory, the name of which is your package name, somewhere on your PYTHONPATH. For example:

    eulerproject/
        euler/
            __init__.py
            euler1.py
            ...
            tests/
                ...
        setup.py
    

    Then, you can either install this systemwide, or make sure to set PYTHONPATH=/path/to/eulerproject/:$PYTHONPATH when invoking your script.

    An absolute import like this will then work:

    from euler import euler1
    

    Edit:

    According to the Python docs, “modules intended for use as the main module of a Python application should always use absolute imports.” (Cite)

    So a test harness like nose, mentioned by the other answer, works because it imports packages rather than running them from the command line.

    If you want to do things by hand, your runnable script needs to be outside the package hierarchy, like this:

    eulerproject/
        runtests.py
        euler/
            __init__.py
            euler1.py
            ...
            tests/
                __init__.py
               testeulern.py
    

    Now, runtests.py can do from euler.tests.testeulern import TestCase and testeulern.py can do from .. import euler1

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

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

Ok, I think whatever I'm doing wrong, it's probably blindingly obvious, but I can't
I'm creating a Yahoo! Widget and have done this before without any problems (creating
this module does a good job at creating a tagcloud block - all good
ok, so I have a table with the following: <table id=servertable> <tr> <th scope=col>Server
EDIT 2: Well, I went to the code. Note what they are doing here.

Trending Tags

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

Top Members

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.