0

I have created a conda environment named "pino" by:

conda create -n pino

Then, I installed the python 3.8 and pytorch in pino. I did activate pino in the build-in terminal by

conda activate pino

And the terminal has shown:

(pino) jipengcui inform %

To test whether the installaton of pytorch is successful, I created a python file named 'test.py':

import torch
print("done")

Then, I have tested it in 3 different ways.

  1. Debug in VS code. It worked and shown result: done
  2. Test it in VS code build-in terminal(zsh) by run:
python3 run test.py

It didn't work and provided information:

Traceback (most recent call last):
 File "/Users/jipengcui/Documents/3_vs_code/physics_informed/test.py", line 1, in <module>
 import torch
ModuleNotFoundError: No module named 'torch'
  1. Test it in MacOS terminal(zsh) by run:
python3 test.py

It shown the result:done The launch.json is:

{
 "configurations": [
 {
 "name": "Python Debugger: Python File",
 "type": "debugpy",
 "request": "launch",
 "program": "${file}"
 }
 ]
}

And the setting.json is:

{
 "workbench.settings.applyToAllProfiles": [
 
 
 ],
 "terminal.integrated.shell.osx": "/bin/zsh",
 "workbench.editor.enablePreview": false,
 "terminal.integrated.defaultProfile.osx": "zsh",
 "python.autoComplete.extraPaths": [],
 "python.analysis.extraPaths": [],
 "python.terminal.activateEnvironment": false,
 "python.condaPath": "/opt/anaconda3/envs/pino/share/man/man1/python3.1"
}
Why the code could run in original terminal and could not run in VS Build-in terminal? How to fix it? Please!
asked Aug 28, 2024 at 0:45
13
  • Hard to know without more debugging information. Check your VS Code Python interpreter (CTRL+SHIFT+P, then type "Python: Select Interpreter"). Make sure it matches with the environment that the terminal uses. Commented Aug 28, 2024 at 0:54
  • 1
  • 1
    Sounds like you have two different installations of Python on your computer. VS Code is using one, and the terminal is using the other. Commented Aug 28, 2024 at 1:36
  • @Anerdw My debug mode could run so the interpreter in vs code might be correct. To my suprise, the code could run in the terminal in zsh could run but could not in VS code build-in terminal. I'm sorry I couldn't provide more information since I have not enough reputation. Commented Aug 28, 2024 at 2:39
  • @JohnGordon Yeah, I do have two different installation of python. Python in conda env base is 3.12 and in the pino is 3.8. My question is, if VS use one while teminal use the other, how it could run in terminal in zsh and could not in VS build-in terminal? Commented Aug 28, 2024 at 2:45

1 Answer 1

0

Thank you all. The problem has been fixed refer to Common questions -terminal profile. As depicted in the common question:

standalone macOS terminals all run as login shells by default, since macOS does not run a login shell when the user logs into the system.

Then, with "terminal.integrated.inheritEnv": false added to setting.json, the problem was fixed.

PatB
5161 gold badge6 silver badges16 bronze badges
answered Aug 28, 2024 at 21:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.