skip to Main Content

Suppose I have two python files, funcs.py and current.py and that both of these files are inside the same directory. If I add from funcs import * inside current.py then VS Code will recognize all of the functions that I have in funcs.py and will also code complete for them.

For example, if within funcs.py, I have the following:

def some_function():
    # body of the function

then when I am working on current.py, VS Code will autocomplete some_function for me in the sense where if I start typing some_f... then some_function will be included in the list of suggestions that VS Code gives me.

Now suppose that current.py is inside directory_1 and that funcs.py is inside directory_2 and that inside current.py I add the following lines:

from sys import path
path.append(path_to_directory2)

Then if I am working within current.py, I can successfully import funcs.py and use the functions, but VS Code will now not autocomplete any of the functions imported from funcs.py like it did when both current.py and funcs.py were in the same directory.

Is there a way to have VS Code auto complete these functions if the two files are in different directories?

2

Answers


  1. For your question, I offer two answers.

    1. If you want to get automatic completion without needing it to be run successfully. You can add "python.autoComplete.extraPaths": [] to your settings.json and specify the path manually.
    2. If you want to run the code successfully without changing the original code. You can try .env file.

    Firstly, create the .env file under your workspace.

    Secondly, add python.envFile": "${workspaceFolder}/.env", to your settings.json.

    Thirdly, add the folder you need to import as an environment variable to the .env file. For example, PYTHONPATH=C:/.

    Login or Signup to reply.
  2. this extension works well for my path autocompletion in the past. Just tested your case and also works smoothly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search