Python VSCode: Unable to import module using "play" button despite adding PYTHONPATH

I am trying to use VSCode with Python3 on MacOS to run code in my workspace that imports other modules in the workspace. My code can be found here:

Here is the structure of my workspace:


In (which implements BFS and DFS) I want to make use of the stack and queue implementation I have under dataStructures. Hence I have such import statements (I don’t want to use relative imports):

from dataStructures.queues import Queue
from dataStructures.stacks import Stack
from dataStructures.graphs.graph import Graph

I was using a very hacky way of setting my PYTHONPATH which was working for F5, Ctrl+F5 as well as the "play" button on VSCode that runs code in the terminal: I had added this in my ~/.bash_profile:


Now to do it in a more elegant way that could work for different workspaces, I decided to modify VSCode workspace settings to do this. I followed this answer and this blog. However, I am able to get the code working with F5 and Ctrl+F5 but not using the "play" button.

I made the following modifications (also restarted VSCode, killed terminal):

1. launch.json: Added env and cwd

    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit:
    "version": "0.2.0",
    "configurations": [
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}",
            "env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}

2. settings.json: Added PYTHONPATH and envFile

    "python.pythonPath": "/usr/local/bin/python3",
    "python.testing.promptToConfigure": false,
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "terminal.integrated.env.osx": {
        "PYTHONPATH": "${workspaceFolder}"
    "python.envFile": "/Users/stutirastogi/dev/commonAlgorithms/.env"

3. .env file: Added a .env file at root of workspace


Since F5 and Ctrl+F5 are working, I feel launch.json changes work but settings.json ones do not from what I understand (please correct me if I am wrong). I usually use the "play" button to run code, so would love to find a way to get it to work.

Some other things I tried:

  • Hard coded the absolute path instead of workspaceFolder
  • Added to dataStructures/ and dataStructures/graphs
  • Tried only with the .env file and also only with the PYTHONPATH in settings.json

Not sure where I am going wrong, and I’d appreciate any help to make this work! Thanks!

Other references:

Source: Python Questions

One Reply to “Python VSCode: Unable to import module using "play" button despite adding PYTHONPATH”

Leave a Reply to Laurens Knoll Cancel reply