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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:06:49+00:00 2026-05-16T14:06:49+00:00

I used to think that once a module was loaded, no re-importing would be

  • 0

I used to think that once a module was loaded, no re-importing would be done if other files imported that same module, or if it were imported in different ways. For example, I have mdir/__init__.py, which is empty, and mdir/mymod.py, which is:

thenum = None
def setNum(n):
    global thenum
    if thenum is not None:
        raise ValueError("Num already set")
    thenum = n

def getNum():
    if thenum is None:
        raise ValueError("Num hasn't been set")
    return thenum

First few use cases from the same file go according to expectation. This file is ./usage.py, the same folder mdir is in:

import mdir.mymod

mdir.mymod.setNum(4)
print mdir.mymod.getNum()

from mdir import mymod
print mymod.getNum()

from mdir.mymod import *
print getNum()
try:
    setNum(10)
except ValueError:
    print "YHep, exception"

The output is as expected:

4
4
4
YHep, exception

However, if I muck with the system path, then it looks like the module is imported anew:

#BEHOLD
import sys
sys.path.append("mdir")
import mymod
try:
    mymod.getNum()
except ValueError:
    print "Should not have gotten exception"

mymod.setNum(10)
print mymod.getNum()
print mdir.mymod.getNum()

That code, running after the previous code, yields:

Should not have gotten exception
10
4

What gives?

  • 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-16T14:06:49+00:00Added an answer on May 16, 2026 at 2:06 pm

    mymod and mdir.mymod are considered different modules – here’s somewhat related discussion: http://code.djangoproject.com/ticket/3951

    Explanation:

    It’s best to play with python interactive interpreter and see for yourself. I created directory (package) mydir under some directory and inside it two files (modules) – __init__.py and mymod.py, both empty. I started python inside of directory containing mydir. Now see what happens:

    >>> import mydir.mymod
    >>> from mydir import mymod
    >>> mymod == mydir.mymod
    True
    

    Why are mymod and mydir.mymod considered the same thing? Well, both names refer to the same module object – modules equality is determined by their paths comparison:

    >>> mymod
    <module 'mydir.mymod' from 'mydir\mymod.py'>
    >>> mydir.mymod
    <module 'mydir.mymod' from 'mydir\mymod.py'>
    

    Now, if I alter sys.path to contain mydir and import mymod in such a way that path of imported module will seem to be different:

    >>> import sys
    >>> sys.path.append( "d:/zrodla/stack/mydir" )
    # note that importing mymod (and not mydir.mymod) prior to appending mydir to 
    # path would cause an error
    >>> mymod2
    <module 'mymod' from 'd:/zrodla/stack/mydir\mymod.pyc'>
    >>> mymod2 == mydir.mymod
    False
    

    then resulting module objects will not compare equal. This way one module will be imported twice – it’s normal and that’s the way python works. Just remember that imported modules are identified by their paths – more specifically by ‘dotted paths’ I think – look at sys.modules keys:

    >>> [x for x in sys.modules.keys() if "my" in x]
    ['mydir', 'mymod', 'mydir.mymod']
    

    I hope it’s clear now.

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

Sidebar

Related Questions

How do I serve files in Drupal without revealing a link that might be
I was wondering - how do people handle importing large numbers of commonly used
I notice in several API's, that you may create a struct which is used
I am designing the architecture for a module where a search takes place. the
In a program that I'm writing, I wanted to make a ConfigParser that's read
I'm developing a Django app, and I'm trying to use Python's logging module for
I've been using Ninject as my IOC in my web app. It's great and
I went through answers on similar topics here on SO but could't find a
I'm using Boost.Python to create Python modules from C++ classes. And I ran into
I need an answer to the following question to help understand what approach I

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.