0

My problem

Problem 1:

I am using window 10 and I cannot find a way to automatically initialize python.exe for my interpreter. That's say I have a two interpreter called torchpy39 and tensorflow_environment The will show as folliowing

C:\Users\Brian\my_folder>C:/Users/Brian/anaconda3/Scripts/activate

(base) C:\Users\Brian\stock_prediction>conda activate torchpy39

(torchpy39) C:\Users\Brian\stock_prediction>C:/Users/Brian/anaconda3/envs/torchpy39/python.exe

and I can directlly use python, but when I use tensorflow_evnvironment, the python.exe will not automatically execute. I will stop at activate tensorflow_environment, how can I handle this?

Problem 2:

Another problem is that even I choose interpreter as tensorflow_environment , if I press Shift + Enter, the code will not execute in tensorflow_environment but in torchpy39. It seems that the system set my torchpy39 as defualt interpreter. How can I handle this as well?

What I have tried.

What I have done is that I have add the python.exe to env variable in my PC, but it does not help me to execute python when I start my venv in vs code.

enter image description here

  • Does this answer your question? [Auto activate virtual environment in Visual Studio Code](https://stackoverflow.com/questions/58433333/auto-activate-virtual-environment-in-visual-studio-code) – ShadowCrafter_01 Jun 12 '23 at 08:39
  • Sorry for misunderstanding. I edit to clarify my questions. When I execute the terminal, it won't execute the python exe automatically, in the picture, you can see that I have to type in `python` to initialize python. But before I don't need to, How can I do so? – Chi-Yuan Li Jun 12 '23 at 09:17
  • Are you looking for the kind of "Python Console" window that PyCharm has? – slothrop Jun 12 '23 at 09:54
  • No, I am looking for the solution to execute python.exe automatically in terminal (or environment). I found my "work-fine" env that will execute python.exe with this line `(torchpy39) C:\Users\my_name\stock_prediction>C:/Users/my_name/anaconda3/envs/torchpy39/python.exe` after `conda activate`. I can easily start run my python code. But my another environment, it does not do so, After `conda activate my_env`, it will run the scripts directly without init python. So I have to enter python to initialize python code. – Chi-Yuan Li Jun 12 '23 at 10:04
  • May I know what's going on with the problem? If it has been solved, can you change the status of the answer to help more people with similar problems. – JialeDu Jun 30 '23 at 01:29
  • Currently I still struggle with problem, I found out that some env will execute these lines when I first initial the vs code, `C:\Users\Brian\stock_prediction>C:/Users/Brian/anaconda3/Scripts/activate` `(base) C:\Users\Brian\stock_prediction>conda activate torchpy39` `(torchpy39) C:\Users\Brian\stock_prediction>C:/Users/Brian/anaconda3/envs/torchpy39/python.exe` But some of them won't, I think that's my real questions. – Chi-Yuan Li Jul 01 '23 at 12:14
  • After selecting the interpreter, creating a new terminal will automatically activate the environment, `"python.terminal.activateEnvironment": true,` setting control it and the default is fakfk. If you use `Run Python File` it will definitely use the interpreter you choose to execute the script, but `Shift+Enter` not necessarily, because the choice of interpreter only works for python extensions. You can check what command is bound to the shortcut key `Shift+Enter`. – JialeDu Jul 06 '23 at 06:03

1 Answers1

0

You may be using Shift+Enter or right mouse button Run Python --> Run Selection/Line in Python Terminal to execute a selected part of the code.

enter image description here

This is an error brought by the Python extension update, currently you can get the fix by downgrading the python extension.

Similar SO question:

Github link:

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • This is not relevant answer, my problem is that I have to type in python to initialize python. How can I get rid of it? – Chi-Yuan Li Jun 12 '23 at 09:18
  • It's the same problem. You type python code directly in the terminal which of course won't work. If you want to get python to interact with the terminal, of course you need to type `python` to activate it first. The easy way is the `Shift+Enter` shortcut in the question. – JialeDu Jun 12 '23 at 09:26
  • If you still have doubts, you should first google the difference between vscode integrated terminal and python interactive terminal. The first line of command `conda activate ...` in your picture just activates the virtual environment, it doesn't open the python interactive terminal. – JialeDu Jun 12 '23 at 09:28
  • I am sorry, but my question is that my environment won't initial python by itself, for example, I found out that one of my venv will execute this line `(torchpy39) C:\Users\my_name\stock_prediction>C:/Users/my_name/anaconda3/envs/torchpy39/python.exe` And I can direclty execute python code. But for my another one venv, I have to manually enter python to initialize python. – Chi-Yuan Li Jun 12 '23 at 09:54
  • How much do you know about python? How do you get the above command? If you can describe all the steps in detail, and explain what works correctly and where it does not meet expectations. This will get a better explanation. – JialeDu Jun 13 '23 at 01:10
  • I have already said above that the first line of command in your picture just activates the conda virtual environment, it does not have the python command to activate the python interactive window. As for the command you just mentioned, I don't know how you got it, but it's not the command to activate the virtual environment, it uses python to open the interactive terminal. So this is the normal response of the terminal and there is no problem. – JialeDu Jun 13 '23 at 01:24