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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:24:36+00:00 2026-05-15T16:24:36+00:00

In interactive python I’d like to import a module that is in, say, C:\Modules\Module1\module.py

  • 0

In interactive python I’d like to import a module that is in, say,

C:\Modules\Module1\module.py

What I’ve been able to do is to create an empty

C:\Modules\Module1\__init__.py

and then do:

>>> import sys
>>> sys.path.append(r'C:\Modules\Module1')
>>> import module

And that works, but I’m having to append to sys.path, and if there was another file called module.py that is in the sys.path as well, how to unambiguously resolve to the one that I really want to import?

Is there another way to import that doesn’t involve appending to sys.path?

  • 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-15T16:24:36+00:00Added an answer on May 15, 2026 at 4:24 pm

    EDIT: Here’s something I’d forgotten about: Is this correct way to import python scripts residing in arbitrary folders? I’ll leave the rest of my answer here for reference.


    There is, but you’d basically wind up writing your own importer which manually creates a new module object and uses execfile to run the module’s code in that object’s “namespace”. If you want to do that, take a look at the mod_python importer for an example.

    For a simpler solution, you could just add the directory of the file you want to import to the beginning of sys.path, not the end, like so:

    >>> import sys
    >>> sys.path.insert(0, r'C:\Modules\Module1')
    >>> import module
    

    You shouldn’t need to create the __init__.py file, not unless you’re importing from within a package (so, if you were doing import package.module then you’d need __init__.py).

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

Sidebar

Related Questions

I would like to create interactive Python script (probably using curses?) with menu where
Is there something like python's interactive REPL mode, but for Java? So that I
In the Python interactive interpreter: I am importing a module that contains a class.
In shells like the interactive python shell, you can usually use the arrow keys
I can import MySQLdb from the interactive python command line but Zope gives me
I would like to add some interactive capability to a python CLI application I've
Scenario: An interactive CLI Python program, that is in need for a password. That
I have a completely non-interactive python program that takes some command-line options and input
I want to be able to bring up an interactive python terminal from my
I'd like to extend the Python interactive shell to allow execution of non-python specific

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.