How can I debug a python code in a virtual environment using VSCode?












0















EDIT



Using VSCode, I had an issue while debugging in a virtual environment that have different packages which are not installed in the base environment. After activating the environment with activate my_env, I could use the environment normally like python main.py. But while debugging, it wasn't see the packages which are only installed in my_env. How can I debug a python code in a virtual environment using VSCode?



This is this the summary of the question. The rest has some specific info about my case.



BEFORE EDIT



I am trying to use xmltodict package with a simple code using visual studio code.



import xmltodict

with open('C:\Users\user\foo.xml') as f:
db_dict = xmltodict.parse(f.read())

print(db_dict)


I have a virtual environment named my_env, and I installed xmltodict package in it. When I activate the environment with activate my_env, this code works fine. But, when I try to use vscode debug option, it gives No module named 'xmltodict' error. Becuase vscode debug button opens new cmd and run the debugging command in it, I stopped debugging and typed activate my_envin that cmd and tried to debug again, but still it can't find the module. Also, I tried jupiter notebook in vscode, it also doesn't see the package.



I see that import xmltodict is underlined with red in vscode and it says Unable to import 'xmltodict', but it works when I run it normally from cmd. This happens sometimes for other modules and I don't know why. I installed xmltodict module using pip, maybe it causes that.



I am using Visual Studio Code 1.30.1 with Anaconda Python 3.7.1 on Windows 10.



How can I debug a python code in a virtual environment using VSCode?
I saw this question, but I don't think it is exactly what I want?










share|improve this question

























  • does this help: code.visualstudio.com/docs/python/environments ?

    – Robin Zigmond
    Jan 2 at 15:40











  • Yes it is helpful. I tried a couple things from that documentation but I couldn't find the exact answer in it. Still, I can't debug my code.

    – Alperen
    Jan 2 at 18:42


















0















EDIT



Using VSCode, I had an issue while debugging in a virtual environment that have different packages which are not installed in the base environment. After activating the environment with activate my_env, I could use the environment normally like python main.py. But while debugging, it wasn't see the packages which are only installed in my_env. How can I debug a python code in a virtual environment using VSCode?



This is this the summary of the question. The rest has some specific info about my case.



BEFORE EDIT



I am trying to use xmltodict package with a simple code using visual studio code.



import xmltodict

with open('C:\Users\user\foo.xml') as f:
db_dict = xmltodict.parse(f.read())

print(db_dict)


I have a virtual environment named my_env, and I installed xmltodict package in it. When I activate the environment with activate my_env, this code works fine. But, when I try to use vscode debug option, it gives No module named 'xmltodict' error. Becuase vscode debug button opens new cmd and run the debugging command in it, I stopped debugging and typed activate my_envin that cmd and tried to debug again, but still it can't find the module. Also, I tried jupiter notebook in vscode, it also doesn't see the package.



I see that import xmltodict is underlined with red in vscode and it says Unable to import 'xmltodict', but it works when I run it normally from cmd. This happens sometimes for other modules and I don't know why. I installed xmltodict module using pip, maybe it causes that.



I am using Visual Studio Code 1.30.1 with Anaconda Python 3.7.1 on Windows 10.



How can I debug a python code in a virtual environment using VSCode?
I saw this question, but I don't think it is exactly what I want?










share|improve this question

























  • does this help: code.visualstudio.com/docs/python/environments ?

    – Robin Zigmond
    Jan 2 at 15:40











  • Yes it is helpful. I tried a couple things from that documentation but I couldn't find the exact answer in it. Still, I can't debug my code.

    – Alperen
    Jan 2 at 18:42
















0












0








0








EDIT



Using VSCode, I had an issue while debugging in a virtual environment that have different packages which are not installed in the base environment. After activating the environment with activate my_env, I could use the environment normally like python main.py. But while debugging, it wasn't see the packages which are only installed in my_env. How can I debug a python code in a virtual environment using VSCode?



This is this the summary of the question. The rest has some specific info about my case.



BEFORE EDIT



I am trying to use xmltodict package with a simple code using visual studio code.



import xmltodict

with open('C:\Users\user\foo.xml') as f:
db_dict = xmltodict.parse(f.read())

print(db_dict)


I have a virtual environment named my_env, and I installed xmltodict package in it. When I activate the environment with activate my_env, this code works fine. But, when I try to use vscode debug option, it gives No module named 'xmltodict' error. Becuase vscode debug button opens new cmd and run the debugging command in it, I stopped debugging and typed activate my_envin that cmd and tried to debug again, but still it can't find the module. Also, I tried jupiter notebook in vscode, it also doesn't see the package.



I see that import xmltodict is underlined with red in vscode and it says Unable to import 'xmltodict', but it works when I run it normally from cmd. This happens sometimes for other modules and I don't know why. I installed xmltodict module using pip, maybe it causes that.



I am using Visual Studio Code 1.30.1 with Anaconda Python 3.7.1 on Windows 10.



How can I debug a python code in a virtual environment using VSCode?
I saw this question, but I don't think it is exactly what I want?










share|improve this question
















EDIT



Using VSCode, I had an issue while debugging in a virtual environment that have different packages which are not installed in the base environment. After activating the environment with activate my_env, I could use the environment normally like python main.py. But while debugging, it wasn't see the packages which are only installed in my_env. How can I debug a python code in a virtual environment using VSCode?



This is this the summary of the question. The rest has some specific info about my case.



BEFORE EDIT



I am trying to use xmltodict package with a simple code using visual studio code.



import xmltodict

with open('C:\Users\user\foo.xml') as f:
db_dict = xmltodict.parse(f.read())

print(db_dict)


I have a virtual environment named my_env, and I installed xmltodict package in it. When I activate the environment with activate my_env, this code works fine. But, when I try to use vscode debug option, it gives No module named 'xmltodict' error. Becuase vscode debug button opens new cmd and run the debugging command in it, I stopped debugging and typed activate my_envin that cmd and tried to debug again, but still it can't find the module. Also, I tried jupiter notebook in vscode, it also doesn't see the package.



I see that import xmltodict is underlined with red in vscode and it says Unable to import 'xmltodict', but it works when I run it normally from cmd. This happens sometimes for other modules and I don't know why. I installed xmltodict module using pip, maybe it causes that.



I am using Visual Studio Code 1.30.1 with Anaconda Python 3.7.1 on Windows 10.



How can I debug a python code in a virtual environment using VSCode?
I saw this question, but I don't think it is exactly what I want?







python debugging visual-studio-code anaconda






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 6:49







Alperen

















asked Jan 2 at 15:35









AlperenAlperen

1,3961721




1,3961721













  • does this help: code.visualstudio.com/docs/python/environments ?

    – Robin Zigmond
    Jan 2 at 15:40











  • Yes it is helpful. I tried a couple things from that documentation but I couldn't find the exact answer in it. Still, I can't debug my code.

    – Alperen
    Jan 2 at 18:42





















  • does this help: code.visualstudio.com/docs/python/environments ?

    – Robin Zigmond
    Jan 2 at 15:40











  • Yes it is helpful. I tried a couple things from that documentation but I couldn't find the exact answer in it. Still, I can't debug my code.

    – Alperen
    Jan 2 at 18:42



















does this help: code.visualstudio.com/docs/python/environments ?

– Robin Zigmond
Jan 2 at 15:40





does this help: code.visualstudio.com/docs/python/environments ?

– Robin Zigmond
Jan 2 at 15:40













Yes it is helpful. I tried a couple things from that documentation but I couldn't find the exact answer in it. Still, I can't debug my code.

– Alperen
Jan 2 at 18:42







Yes it is helpful. I tried a couple things from that documentation but I couldn't find the exact answer in it. Still, I can't debug my code.

– Alperen
Jan 2 at 18:42














1 Answer
1






active

oldest

votes


















1














Set "python.pythonPath" to point to the interpreter in the virtual environment that you want to use.






share|improve this answer
























  • Thanks. If we add python.pythonPath to settings.json, it works. But, I just want to add another way which is making the same thing. Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment. I tried this one at the beginning but I couldn't see a python interpreter for new environment in the selection list. Restarting VSCode fixed the situation fro me. After restart, I could see the python interpreter for the new environment in the selection list.

    – Alperen
    Jan 5 at 6:38













  • @Alperen glad it worked! Yes, the Select Interpreter command actually writes out the setting for you. And the newest release of the extension should detect a new virtual environment in the directory, but we have to notice it which isn't necessarily immediate.

    – Brett Cannon
    Jan 7 at 23:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009081%2fhow-can-i-debug-a-python-code-in-a-virtual-environment-using-vscode%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Set "python.pythonPath" to point to the interpreter in the virtual environment that you want to use.






share|improve this answer
























  • Thanks. If we add python.pythonPath to settings.json, it works. But, I just want to add another way which is making the same thing. Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment. I tried this one at the beginning but I couldn't see a python interpreter for new environment in the selection list. Restarting VSCode fixed the situation fro me. After restart, I could see the python interpreter for the new environment in the selection list.

    – Alperen
    Jan 5 at 6:38













  • @Alperen glad it worked! Yes, the Select Interpreter command actually writes out the setting for you. And the newest release of the extension should detect a new virtual environment in the directory, but we have to notice it which isn't necessarily immediate.

    – Brett Cannon
    Jan 7 at 23:29
















1














Set "python.pythonPath" to point to the interpreter in the virtual environment that you want to use.






share|improve this answer
























  • Thanks. If we add python.pythonPath to settings.json, it works. But, I just want to add another way which is making the same thing. Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment. I tried this one at the beginning but I couldn't see a python interpreter for new environment in the selection list. Restarting VSCode fixed the situation fro me. After restart, I could see the python interpreter for the new environment in the selection list.

    – Alperen
    Jan 5 at 6:38













  • @Alperen glad it worked! Yes, the Select Interpreter command actually writes out the setting for you. And the newest release of the extension should detect a new virtual environment in the directory, but we have to notice it which isn't necessarily immediate.

    – Brett Cannon
    Jan 7 at 23:29














1












1








1







Set "python.pythonPath" to point to the interpreter in the virtual environment that you want to use.






share|improve this answer













Set "python.pythonPath" to point to the interpreter in the virtual environment that you want to use.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 23:00









Brett CannonBrett Cannon

2,06911410




2,06911410













  • Thanks. If we add python.pythonPath to settings.json, it works. But, I just want to add another way which is making the same thing. Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment. I tried this one at the beginning but I couldn't see a python interpreter for new environment in the selection list. Restarting VSCode fixed the situation fro me. After restart, I could see the python interpreter for the new environment in the selection list.

    – Alperen
    Jan 5 at 6:38













  • @Alperen glad it worked! Yes, the Select Interpreter command actually writes out the setting for you. And the newest release of the extension should detect a new virtual environment in the directory, but we have to notice it which isn't necessarily immediate.

    – Brett Cannon
    Jan 7 at 23:29



















  • Thanks. If we add python.pythonPath to settings.json, it works. But, I just want to add another way which is making the same thing. Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment. I tried this one at the beginning but I couldn't see a python interpreter for new environment in the selection list. Restarting VSCode fixed the situation fro me. After restart, I could see the python interpreter for the new environment in the selection list.

    – Alperen
    Jan 5 at 6:38













  • @Alperen glad it worked! Yes, the Select Interpreter command actually writes out the setting for you. And the newest release of the extension should detect a new virtual environment in the directory, but we have to notice it which isn't necessarily immediate.

    – Brett Cannon
    Jan 7 at 23:29

















Thanks. If we add python.pythonPath to settings.json, it works. But, I just want to add another way which is making the same thing. Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment. I tried this one at the beginning but I couldn't see a python interpreter for new environment in the selection list. Restarting VSCode fixed the situation fro me. After restart, I could see the python interpreter for the new environment in the selection list.

– Alperen
Jan 5 at 6:38







Thanks. If we add python.pythonPath to settings.json, it works. But, I just want to add another way which is making the same thing. Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment. I tried this one at the beginning but I couldn't see a python interpreter for new environment in the selection list. Restarting VSCode fixed the situation fro me. After restart, I could see the python interpreter for the new environment in the selection list.

– Alperen
Jan 5 at 6:38















@Alperen glad it worked! Yes, the Select Interpreter command actually writes out the setting for you. And the newest release of the extension should detect a new virtual environment in the directory, but we have to notice it which isn't necessarily immediate.

– Brett Cannon
Jan 7 at 23:29





@Alperen glad it worked! Yes, the Select Interpreter command actually writes out the setting for you. And the newest release of the extension should detect a new virtual environment in the directory, but we have to notice it which isn't necessarily immediate.

– Brett Cannon
Jan 7 at 23:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009081%2fhow-can-i-debug-a-python-code-in-a-virtual-environment-using-vscode%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory