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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:09:04+00:00 2026-05-23T02:09:04+00:00

I have a package in my PYTHONPATH that looks something like this: package/ __init__.py

  • 0

I have a package in my PYTHONPATH that looks something like this:

package/
    __init__.py
    module.py
        print 'Loading module'

If I’m running Python from the package/ directory (or writing another module in this directory) and type

import module

it loads module.py and prints out “Loading module” as expected. However, if I then type

from package import module

it loads module.py and prints “Loading module” again, which I don’t expect. What’s the rationale for this?

Note: I think I understand technically why Python is doing this, because the sys.modules key for import module is just "module", but for from package import module it’s "package.module". So I guess what I want to know is why the key is different here — why isn’t the file’s path name used as the key so that Python does what one expects here?

  • 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-23T02:09:05+00:00Added an answer on May 23, 2026 at 2:09 am

    Effectively, by running code from the package directory, you’ve misconfigured Python. You shouldn’t have put that directory on sys.path, since it’s inside a package.

    Python doesn’t use the filename as the key because it’s not importing a file, it’s importing a module. Allowing people to do ‘import c:\jim\my files\projects\code\stuff‘ would encourage all kinds of nastiness.

    Consider this case instead: what if you were in ~/foo/package/ and ~/bar were on PYTHONPATH – but ~/bar is just a symlink to ~/foo? Do you expect Python to resolve, then deduplicate the symbolic link for you? What if you put a relative directory on PYTHONPATH, then change directories? What if ‘foo.py’ is a symlink to ‘bar.py’? Do you expect both of those to be de-duplicated too? What if they’re not symlinks, but just exact copies? Adding complex rules to try to do something convenient in ambiguous circumstances means it does something highly inconvenient for other people. (Python zen 12: in the face of ambiguity, refuse the temptation to guess.)

    Python does something simple here, and it’s your responsibility to make sure that the environment is set up correctly. Now, you could argue that it’s not a very good idea to put the current directory on PYTHONPATH by default – I might even agree with you – but given that it is there, it should follow the same consistent set of rules that other path entries do. If it’s intended to be run from an arbitrary directory, your application can always remove the current directory from sys.path by starting off with sys.path.remove('').

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

Sidebar

Related Questions

Running Python 2.6.1 on OSX, will deploy to CentOS. Would like to have a
I have a few Python packages that I would like to tidy up and
Please help me understand this. Here you can see that I have PYTHONPATH set
On WinXP sp2 I'd like to have a directory of modules that other python
I have a small web.py Python application that I would like to serve under
I have a python script start.py that executes well from the command line. There
I have a package that I just made and I have an old-mode that
I have a package with logic classes(like CheckAuthenticationDataLogic.java, GetVocabulariesLogic.java). And another class - ApiService.java
I have a package with a few modules, each module has a class (or
I have a dtsx package with a precedence constraint that evaluates an expression and

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.