Python Forum

Full Version: SOLVED: VS Code: Adding external directories to project?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm not sure if this is the right place to ask, but I can't find anywhere better...

I'm trying to switch from PyCharm to VS Code as my Python IDE. There are a few reasons why, but lets just say I want the experience of using both IDEs.

Some of my projects use an external .py file to provide some common classes to my code. In my scripts that use these files, I add the directory to the python path using sys.path.append(). This allows the code to work as expected, but in the IDE, VS Code doesn't see the external files. In PyCharm, I was able to add a directory for my library directory so I didn't have this issue. Is it possible to do the same thing in VS Code?

My sample script:

import sys
sys.path.append("/coding/libs")   # Adding the directory that holds the mylib.py file

import mylib   # VS Code can't find this library file

x = mylib.MyClass() 
x.do_something("Bob")
Is there a setting in VS Code where I can add extra paths for it to search for files?
I use vscodium and to add extra paths is in settings extentions python
Here is a link I found
https://stackoverflow.com/questions/4147...ython-path
(Jul-31-2024, 07:06 PM)menator01 Wrote: [ -> ]I use vscodium and to add extra paths is in settings extentions python
Here is a link I found
https://stackoverflow.com/questions/4147...ython-path
Thanks for the link. It was very helpful.

I ended up creating a .env file that specified the directory I wanted in the PYTHONPATH.
I never had any problems using

import sys

sys.path.append("/path/2/mylibs")
in Idle, always works fine! I have imported so many self-made functions!

But I never imported classes, because I never wrote any!

What os are you using?
(Jul-31-2024, 10:50 PM)Pedroski55 Wrote: [ -> ]I never had any problems using

import sys

sys.path.append("/path/2/mylibs")
in Idle, always works fine! I have imported so many self-made functions!

But I never imported classes, because I never wrote any!

What os are you using?
I'm using Windows 10 and VS Code v1.92.0.

The sys.path.append lets the code finds the files when I run it, but VS Code doesn't find the files and marks the import as an issue. This stops intellisense from working on the methods in that file.

Adding the directory containing my files to the python path using a .env file lets intellisense work normally.
Install the folder containing the frequently used .py files as a local project using pip.

https://pip.pypa.io/en/stable/topics/loc...-installs/