question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

File directory added to sys.path on every change

See original GitHub issue

This seem to not happen when there is no import.

import sys
print (sys.path)
from something import *

Python 3.6.7 OS: nixos 18.09.1789.11c9922a7d5 (Jellyfish)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Almenoncommented, Dec 29, 2018

Fixed. I just need a try/finally around the yield in script_path

0reactions
Almenoncommented, Dec 29, 2018

it’s a problem with the script_path function in the backend. It inserts into the path each execution.

@contextmanager
def script_path(script_dir):
    """
        Context manager for adding a dir to the sys path
        and restoring it afterwards. This trick allows
        relative imports to work on the target script.
        if script_dir is empty function will do nothing
        Slightly modified from wolf's script_path (see https://github.com/Duroktar/Wolf)
    """
    if script_dir is None or script_dir == "":
        yield
    else:
        original_cwd = os.getcwd()
        os.chdir(script_dir)
        path.insert(1, script_dir)
        yield
        os.chdir(original_cwd)
        path.remove(script_dir)

# useage:

    with script_path(os.path.dirname(filePath)):
        try:
            start = time()
            exec(codeToExec, evalLocals)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Best way to add to sys.path relative to the current running script
This is what I use: import os, sys sys.path.append(os.path.join(os.path.dirname(__file__), "lib")).
Read more >
Check and add the module search path with sys.path in Python
Since the module search path changes depending on the current directory, the import may not work depending on where the script file is...
Read more >
Dynamically Changing the Python Search Path - O'Reilly
sys.path is a list, so it's easy to add directories to its end, using sys.path.append . Every import performed ...
Read more >
sys.path in Python - GeeksforGeeks
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 ......
Read more >
The initialization of the sys.path module search path — Python ...
The PYTHONPATH environment variable is often used to add directories to the search path. If this environment variable is found then the contents...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found