简体   繁体   中英

Installing Python packages for Visual Studio Code

I'm trying to import a package called "termcolor" that can run on VSC. I know the import is working because when I run the program from my mac terminal it executes fine. However, when I run from VSC, I get the ImportError "no module named termcolor".

I know it has been installed in the my virtual environment where I am building the program:

Kaylas-iMac:Ermes Marana rahme$ source "/Users/rahme/Desktop/Programming/Python/Ermes Marana/env/bin/activate"
(env) Kaylas-iMac:Ermes Marana rahme$ python3 -m pip install termcolor
Requirement already satisfied: termcolor in ./env/lib/python3.7/site-packages (1.1.0) 

And I think I'm importing from the correct path. I saw in another post to use sys.path.append() to explicitly look for packages in the correct path, so I tried that. Here is my code:

import sys

sys.path.append('users/Rahme/Desktop/Programming/Python/Ermes\ Marana/env/lib/python3.7/site-packages')
import termcolor
print(termcolor.colored(text = "I'm Rahme", color ="red", on_color="on_grey")) 

sys had a warning about the space in the directory /Ermes\\ Marana and suggested adding an r to the backslash which I did and nothing changed. What am I doing wrong?

How can I install packages on VSC in the future so that I can use the built in run in terminal?

try

sys.path.append(r'users/Rahme/Desktop/Programming/Python/Ermes/ 
 Marana/env/lib/python3.7/site-packages')

What is the purpose of the space? Edit: keep the space if it is part of the folderpath Please do see the r I have added infront of the folderpath

Just because you switched to your virtual environment in the mac terminal doesn't mean that the VSC terminal is operating in that virtual environment. To get VSC to know you want it to run in your environment, you need to hit ctrl+shift+p to pull up the command pallette. Then you type python: select interpreter . You will get a list of all the virtual environments that VSCode can see and you just select your environment from that list. Once you do that, VSC should operate fine inside your environment.

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