
But now VSCode complains that it can’t find my_package.classes and it won’t give you auto-completion for any of the classes and functions from my_package, and having not auto-completion almost defeats the whole purpose of having an editor or IDE. Now the can be removed from main.py and you can run main.py by pressing Ctrl+F5/ Cmd+F5.

You can also use relative paths, including. is the variable that contains the path to the root folder of your current VSCode workspace, and as libs is a folder inside the root /libs/ is added. Inside the launch.json you have to add a new env segment that will tell VSCode to extend the PYTHONPATH before running your program: You can now run your code by pressing Ctrl+F5 or Cmd+F5. A launch.json allows you to run your code regardless of which files are currently opened or in focus. vscode directory to your workspace, and inside it, you can find a launch.json file. For the workspace in this example, you would enter my_code.main because main.py is inside my_code, which is the workspace’s root. Then click on create launch.json file and choose Module, press Enter, and enter the path to the Python file you would like to run while folders a separated with a dot. To create a launch.json, go to Run and Debug in the VSCode sidebar by clicking on the bug and run icon or pressing Ctrl+Shift+D. So the preferred solution is to ask VSCode to extend the PYTHONPATH only for your project which you can also add to your git repository such that others don’t have to extend their PYTHONPATH manually.įirst, you need to add a launch.json to your workspace that tells VSCode what and how to run your code.

But then you have to tell everyone who uses your code to do this for their system. You can either extend PYTHONPATH systemwide by appending the full path to libs to it, whereas several paths are separated using a colon. A better solution would be to have a single file to extend the PYTHONPATH. Every file that imports M圜lass has to be changed, which is tedious and error-prone. In each of these files, you have to add the first two lines. Imagine you got several Python files that want to access M圜lass. However, this solution not only looks terrible, but it also has a lousy code design.


append ( "./libs" ) from my_package.classes import M圜lass if _name_ = "_main_" : a = M圜lass ( "a" ) print ( a )
