How to make codeRunner in VS code recognize my current working directory?

给你一囗甜甜゛ 提交于 2020-12-08 01:56:23

问题


I recently downloaded VSCode. Im doing some development work. Usually I do the following steps: 1) cd into my Dev folder 2) then code my_projects

Now inside my_projects folder, its the root workspace directory. I have my VS code opened, and can navigate to following folders/files:

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

Now when i open 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 codeRunner in VSCode to recognize the file that im currently in, in this case dbhelper.py is the current working directory?

The reason I ask is because i created a Database class using postgres to help me with some personal research and wheneever i use dbhelper in different folders, its not recognizing the config.ini file I have in that directory.

Thanks


回答1:


I experienced this a while ago, 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.

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

But you can change this behaviour by putting a checkmark in the setting: python.terminal.executeInFileDir

Go to "File -> Preferences -> Settings" And then search for this term in the top search bar.

There you can find and activate this option.

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

I hope it helped you.




回答2:


Vs code runs commands from inside the folder you have opened, so, if you go to a script inside it, vscode 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 workspace settings and adding terminal.integrated.cwd but i don't think that would solve your problem since you are probably running scripts from the launcher option.

If you are running from the launcher, you can edit your launch.json file (vscode creates one automatically on the workspace), and add different launch commands for each of your .py files.




回答3:


This may help you

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))
print  dir_path

cwd = os.getcwd()
print cwd

os.chdir(dir_path)
cwd = os.getcwd()
print cwd



回答4:


I figured it out. In my VScode settings, i added this:

{
"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

}



来源:https://stackoverflow.com/questions/62805327/how-to-make-coderunner-in-vs-code-recognize-my-current-working-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!