Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 902
turtle_in_mind Avatar asked Oct 27 '25 09:10

turtle_in_mind


2 Answers

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
}
like image 84
turtle_in_mind Avatar answered Oct 30 '25 14:10

turtle_in_mind


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.

like image 26
Scott Hughes Avatar answered Oct 30 '25 14:10

Scott Hughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!