I've installed some packages during the execution of my script as a user. Those packages were the first user packages, so python didn't add ~/.local/lib/python2.7/site-packages
to the sys.path
before script run. I want to import those installed packages. But I cannot because they are not in sys.path
.
How can I refresh sys.path
?
I'm using python 2.7.
sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules.
As the docs explain, sys. path is populated using the current working directory, followed by directories listed in your PYTHONPATH environment variable, followed by installation-dependent default paths, which are controlled by the site module.
Appending a value to sys. path only modifies it temporarily, i.e for that session only. Permanent modifications are done by changing PYTHONPATH and the default installation directory.
As explained in What sets up sys.path with Python, and when? sys.path
is populated with the help of builtin site.py
module.
So you just need to reload it. You cannot it in one step because you don't have site
in your namespace. To sum up:
import site
from importlib import reload
reload(site)
That's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With