close

How to set the root directory for Visual Studio Code Python Extension?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to set the root directory for Visual Studio Code Python Extension in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to set the root directory for Visual Studio Code Python Extension?

  1. How to set the root directory for Visual Studio Code Python Extension?

    Also creating the file under the folder where your consuming code is, instead of under repo root, seems to work e.g. your_repo/service/.env.

  2. set the root directory for Visual Studio Code Python Extension

    Also creating the file under the folder where your consuming code is, instead of under repo root, seems to work e.g. your_repo/service/.env.

Method 1

You can create a .env file with:

PYTHONPATH=server

That will add your server folder to PYTHONPATH as needed.

(You may need to restart VSCode for it to take PYTHONPATH into account correctly.)

Edited to clarify…

Create a file named .env under the repo root e.g. your_repo/.env.

Also creating the file under the folder where your consuming code is, instead of under repo root, seems to work e.g. your_repo/service/.env.

For me this worked without restarting VSC, perhaps this is a matter of newer VSC and extensions versions.

Method 2

If you are using the Pylance extension you can set your source folder via the python.analysis.extraPaths option. It also looks for common source folder names like src by default, this option is called python.analysis.autoSearchPaths.

Go to File > Preferences > Settings, search for pythonpath. Under the Pylance options you should see Extra Paths, this is where you set your source folder.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read