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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:11:53+00:00 2026-05-25T23:11:53+00:00

I am making a server that listens on a socket. Whenever a new request

  • 0

I am making a server that listens on a socket. Whenever a new request comes in, the server spawns a new instance of handle_request. Each instance of handle_request.py imports the relevant handler from request_handlers.

server.py
handle_request.py
request_handlers
|_handler_beta
|_handler_1
|_handler_2

While handlerX is a module, request_handlers is not a package. The modules are self-contained and reloaded on each request. The modules may be added, modified or dropped while the program is running.


Question: What is the way to import a module from an arbitrary directory?


Doing my homework, I saw that most questions deal with packages, even one is titled “python: import a module from a folder“. Hence I believe this question is distinct. The architecture has been simplified; and yes, I am considering pre-forking with reload on file modification.

  • 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-25T23:11:54+00:00Added an answer on May 25, 2026 at 11:11 pm

    Create __init__.py in the directory. That makes it a package. If you’re scanning the directory for .py files, you’ll probably then want to skip __init__.py.

    Then, you can import them with __import__('request_handlers.' + module, fromlist=['']) (the fromlist is important, otherwise you’ll get request_handlers rather than the appropriate module).

    One way of doing it without an __init__.py would be to put the request_handlers directory in sys.path, but that then makes name clashes with other modules possible. Another way would be with execfile. You can research that more if you want to. I’d do it (and have done it before) the package/__import__ way.

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

Sidebar

Related Questions

I'm making server that make a tcp socket and work over port range, with
I've been working with socket programming lately; and I'm currently making a server that
I'm making AJAX calls to a server that sometimes return unparseable JSON. The server
I have determined that my JSON, coming from the server, is valid (making the
With regards to making a game server, it seems Erlang always comes up as
I am making a prototype client & server so that I can understand how
I'm making a server in java that has a MySQL connection in another thread,
Assuming I have implement that : making my app as a server which means
I'm working on making an image server that is similar to that of Flickr's.
I'm making a server with Java that will provide chat services for flash clients.

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.