简体   繁体   中英

Python interpreter (VScode) "activates" a conda environment, but the environment doesn't change

I'm a bit new to Python, and I was trying to run a project in a different conda environment. I wanted the program to automatically run in the new conda environment that I created ("mySchedule"), instead of manually typing "conda activate mySchedule" every time. So I followed VScode's instructions and used a python interpreter. I applied a python interpreter to my program folder and workspace, but there's a problem:

As in the text below (which is a result of running the program ONCE), it DOES run "conda activate mySchedule" (but does not include the '(base) name@name-MacBook..."). However, the "mySchedule" environment is NOT activated, since the environment stays as "base". Because it wasn't activated, the program ends up running in the "base" environment, which does not include the packages I'm using for the program.

After the program shoots out errors, the python interpreter runs "conda activate mySchedule" again (but this time with the "(base) name@name-..."). This activation WORKS, but it only activates at the very end, which is pretty useless.

python -u "/Users/name/Desktop/Desktop Organizer/Education/Coding/Schedule/program/python/Test.py"
source /Users/name/opt/miniconda3/bin/activate

conda activate mySchedule

(base) name@name-MacBook-Air program % python -u "/Users/name/Desktop/Desktop Organizer/Education/Coding/Schedule/program/python/Test.py"

<bunch of errors because the program uses packages/modules that only exist in the "mySchedule" environment, but not in the "base" environment.>

(base) name@name-MacBook-Air program % source /Users/name/opt/miniconda3/bin/activate

(base) name@name-MacBook-Air program % conda activate mySchedule

(mySchedule) name@name-MacBook-Air program % 

Does anyone have any advice on how to activate my "mySchedule" conda environment at the BEGINNING of the run? Also, I've seen some solutions that make my "mySchedule" environment as the default environment, but it would be great if I didn't have to do this.

Maybe: Would a problem arise because I applied the python interpreter to both the program folder and the workspace?

python -u "/Users/name/Desktop/Desktop Organizer/Education/Coding/Schedule/program/python/Test.py" source /Users/name/opt/miniconda3/bin/activate

conda activate mySchedule

(base) name@name-MacBook-Air program % python -u "/Users/name/Desktop/Desktop Organizer/Education/Coding/Schedule/program/python/Test.py"

When you run the command conda activate mySchedule , it seems that base has not been started correctly, which is equivalent to entering an invalid command, because there is no basic information of base before the command you enter.

I wanted the program to automatically run in the new conda environment that I created ("mySchedule"),

You can manually specify the Python interpreter by adding the following code to the setting.json file:

For example,

{
  "python.defaultInterpreterPath": "c:/dev/ala/venv/Scripts/python.exe"
}

You can refer to the document for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM