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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:09:56+00:00 2026-06-10T14:09:56+00:00

I have a python script that is trying to import another script somewhere in

  • 0

I have a python script that is trying to import another script somewhere in the file-system (path is only known at runtime).
To my understanding I need to use the imp module and this might work, but when loading the module I get an error that modules used by the imported module are not found.

Heres the code:

importer.py:

import imp
imp.load_compiled("my_module","full_path_to_my_module\\my_module.pyc")

my_module.py:

import sys
import another_module

When I run importer.py I get htis error message:

ImportError: No module named another_module

Whats going wrong here ?
I suspect that when ‘importer.py’ is loading ‘my_module.pyc’ hes also trying to load ‘another_module’ (thats good) but is looking in the wrong place (eg not ‘full_path_to_my_module’)

EDIT:
I tried adding ‘full_path_to_my_module’ to the system path:

import imp
import sys

sys.path.append(full_path_to_my_module)
imp.load_compiled("my_module",full_path_to_my_module+my_module)

But I still get the same error

Maybe I do something thats not necessary – Here’s my goal:
I want to be able to use all functionality of ‘my_module.pyc’ inside ‘importer.py’. But the location of ‘my_module.pyc’ is given as a parameter to ‘importer.py’.

  • 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-06-10T14:09:58+00:00Added an answer on June 10, 2026 at 2:09 pm

    imp.load_compiled returns the compiled module object, it is different to the import statement which also binds the module to a name

    import imp
    my_module = imp.load_compiled("my_module", "full_path_to_my_module/my_module.pyc")
    

    Then you can do something like:

    my_module.yayfunctions('a')
    

    Complete example session:

    $ cat /tmp/my_module.py
    def yayfunctions(a):
        print a
    $ python -m compileall /tmp/my_module.py
    $ ls /tmp/my_module.py*
    my_module.py   my_module.pyc
    $ python
    >>> import imp
    >>> my_module = imp.load_compiled("my_module", "/tmp/my_module.pyc")
    >>> my_module.yayfunctions('a')
    a
    

    Edit regarding comment (ImportError: No module named another_module), I assume the error is caused by the code in my_module.pyc, and the another_module.py lives in the same directory

    In that case, as others have suggested, it’s simpler to just add the directory containing my_module to sys.path and use the regular import mechanism, specifically __import__

    Here’s a function which should do what you want:

    import os
    
    
    def load_path(filepath):
        """Given a path like /path/to/my_module.pyc (or .py) imports the
        module and returns it
        """
    
        path, fname = os.path.split(filepath)
        modulename, _ = os.path.splitext(fname)
    
        if path not in sys.path:    
            sys.path.insert(0, path)
    
        return __import__(modulename)
    
    
    if __name__ == '__main__':
        # Example usage
        my_module = load_path('/tmp/my_module.py')
        my_module.yayfunctions('test')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to import one python script from another. I have a few
I have a python script that is trying to create a directory tree dynamically
I received this python script that generates a file checksum: import sys,os if __name__==__main__:
I have a simple python script (moo.py) that i am trying to stream though
I have a problem trying to abort a Python script, for example: #!/usr/bin/python import
I have a python script that runs a program, which generates few .exe files
I have a python script that I want always to run in the background.
I have a Python script that makes use of 'Print' for printing to stdout.
I have a Python script that needs to issue a number of shell commands.
I have a Python script that is using some closed-box Python functions (i.e. 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.