Python Forum
modifying PYTHONPATH during a script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
modifying PYTHONPATH during a script
#1
i want to add a directory (folder) to the list of directories to search for modules so that it affects the script that is making the change. i prefer to add it to the front of the search list. is there a way to do this?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
sys.path.append(module_folder)

or

sys.path.insert(0, module_folder)
Skaperen and Gribouillis like this post
Reply
#3
so, if i want to load a specific file i could save the contents of sys.path, replace it all with the one directory that file is in, import the file, then restore the contents of sys.path?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Jul-04-2022, 04:38 PM)Skaperen Wrote: so, if i want to load a specific file i could save the contents of sys.path, replace it all with the one directory that file is in, import the file, then restore the contents of sys.path?
This may not be the best idea because the file may contain import statements that need modules from the python path. If you remove the content of sys.path, these modules could become unreachable.

The simpler way is to append or prepend the directory to sys.path and leave it this way (or remove it after loading if you want). This could only be a problem if the directory contains many python files that you don't want to import and which names could shadow other modules. For example if the directory contains a file named 'requests.py', it may shadow the usual requests module.

This problem can be addressed by storing the file in a directory where there is no other Python file, or perhaps by creating an ad hoc directory with a symbolic link to this file. For example if your file is /home/doe/spam.py but you don't want to add /home/doe to sys.path and you don't want to move the file, you could create a directory /home/doe/madness and a symlink named /home/doe/madness/spam.py which target is /home/doe/spam.py. Then you can append /home/doe/madness to sys.path and this only enables 'import spam' with no risk of shadowing some other module.
Reply
#5
(Jul-04-2022, 07:07 PM)Gribouillis Wrote: This may not be the best idea because the file may contain import statements that need modules from the python path. If you remove the content of sys.path, these modules could become unreachable
i was thinking about config files i do. these are similar to INI files but use Python syntax. but your point makes sense for modules in general. i was thinking of how to detect if the config file was absent and detecting the ImportError exception. what i probably should do is have two directories with the 2nd one having the default settings (will always exist).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Jul-04-2022, 09:59 PM)Skaperen Wrote: i was thinking of how to detect if the config file was absent and detecting the ImportError exception.
The import system can detect if the module cannot be imported without importing it
spec = importlib.util.find_spec('modname')
if not spec:
    print(f'cannot import {modname}')
This works even if modname contains dots as in 'spam.ham.eggs' (but in that case it will actually import 'spam.ham' if it is possible).
Reply
#7
well, since it is a file intended to be in a known place, i could just do os.stat(fn).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020