Is there a way to add a directory to the locations that python look at when you call __import__ ?

I need to import some scripts from a user folder at runtime, and since __import__ doesn't accept absolute file names, I'd like to try this. Copying the script to the program directory isn't a solution I'm willing to risk (requires administrator privileges on some machines).

Recommended Answers

All 2 Replies

You simply append the directory to PYTHONPATH before you import your custom module:

import sys
# appends to PYTHONPATH  the location of the example codes
sys.path.append(r'C:\Python25\Examples')

Never thought it would be that easy. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.