turtle_in_mind
turtle_in_mind

Reputation: 1152

How to make Code Runner in VS Code recognize my current working directory?

I'm using VS Code with the following steps:

  1. cd into my Dev folder
  2. Then write code in the my_projects folder

Now inside the my_projects folder, it's the root workspace directory and I can navigate to the following folders/files:

my_projects/
        > DBHelper
            > dbhelper.py
            > config.ini
        > PortfolioManagement
        > Learning

Now when I run dbhelper.py, I tried the following test:

import os
print(os.getcwd())

I get the following:

my_projects

But I'm expecting

my_projects/DBHelper/

How do I get Code Runner in VS Code to recognize that the file that I'm currently in, which in this case dbhelper.py is in the current working directory?

The reason I ask is because I created a Database class using postgres and whenever I use dbhelper in different folders, it's not recognizing the config.ini file I have in that directory.

Upvotes: 4

Views: 5822

Answers (5)

liang
liang

Reputation: 1611

Other than set in json setting, it can also be set in GUI.

enter image description here

Upvotes: 0

eri_
eri_

Reputation: 1

When you open a folder to work in and there are other folders inside, the scripts will get launched in the original folder you have opened and not in the directory where the script is inside in.

For me, a quick fix was to just open the directory where the python file is saved.

But you can change this behaviour by enabling the setting:

python.terminal.executeInFileDir
  1. Go to "File -> Preferences -> Settings"
  2. Then search for python.terminal.executeInFileDir in the top search bar.
  3. Once you find it, activate/enable that option.

But you can't expect my_projects/DBHelper/dbhelper.py from os.getcwd(). Instead, you will get my_projects/DBHelper.

Upvotes: -1

Arthur Bruel
Arthur Bruel

Reputation: 673

VS code runs commands from inside the folder you have opened, so if you go to a script inside it, VS Code will start a terminal in the workspace and run the script with python ./DBHelper/dbhelper.py.

You can change the integrated terminal CWD by going to the workspace settings and adding terminal.integrated.cwd, but I don't think that would solve your problem since you are probably running scripts using VS Code's debugger.

If you are running through the debugger, you can edit your launch.json file (VS Code creates one automatically for the workspace), and add different launch commands for each of your .py files.

Upvotes: -1

turtle_in_mind
turtle_in_mind

Reputation: 1152

I figured it out. In my VS Code settings, I added these settings:

{
    "python.pythonPath": "/Users/anaconda/bin/python",
    "code-runner.executorMap": 
    { 
        "python": "$pythonPath -u $fullFileName" 
    },
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.terminal.executeInFileDir": true,
    "code-runner.fileDirectoryAsCwd": true
}

Upvotes: 8

Scott Hughes
Scott Hughes

Reputation: 31

I went to the Code Runner settings from the Extensions listing from the VS Code GUI. I found a setting 'Code-runner: File Directory as CWD' and set that on. That fixed my issue, not sure if that was what the JSON above does.

Upvotes: 2

Related Questions