Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh sys.path?

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.

like image 371
rominf Avatar asked Aug 19 '14 13:08

rominf


People also ask

What is the SYS path?

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.

How does Sys path get populated?

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.

Is SYS path append temporary?

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.


1 Answers

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.

like image 135
rominf Avatar answered Sep 20 '22 16:09

rominf