I don't want to save all of my python files in the default directory, but if I save them elsewhere, python can't find the path to any modules I might be using. How can I change this to work?
jworld2
0
Light Poster
Recommended Answers
Jump to PostAn even easier workaround for being able to import modules from ANY directory is to modify the sys.path variable at run time. So at the beginning of your code do something like this:
import os,sys sys.path.append('/path/to_my/module') import my_custom_module
And if you're working with a hard-coded windows path …
Jump to PostI would go with jlm699 ...
sys.path lists all the directories Python will check, so simply append it with the path of a directory you have created for all your modules.
All 6 Replies
Stefano Mtangoo
455
Senior Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
jlm699
320
Veteran Poster
woooee
814
Nearly a Posting Maven
Yoloswag
0
Newbie Poster
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
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.