Installing Python packages for Visual Studio Code
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.
Related videos on Youtube
rbb091020
Updated on June 04, 2022Comments
-
rbb091020 almost 2 years
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?
-
rbb091020 over 5 yearsWell there's a space in the folder name, so I would have to include it in the path, no?
-
Admin over 5 yearsAre you sure? If it's running fine in your mac terminal but not in vscode, then it is most likely that the two terminals are not operating in the same environment. This is a common problem with VSCode and is unique to that particular editor. I commonly run programs out of my mac terminal or windows command prompt because sometimes VSCode won't find my environment even when I do the command palette search but it never defaults to the same environment as your mac terminal. You have to tell it to do that.
-
rbb091020 over 5 yearsOkay, so this may just be a different question then, but I found the problem. When I type in the vsc terminal python3 hello.py, it runs. However, when I press the run code button (play button), I get the error. Why is that?
-
Admin over 5 yearsIf you have selected your environment in the command palette then I honestly don't know, because normally I would peg that as being under the same problem. On windows, VSCode uses Powershell as it's in-built terminal, so VSCode won't run anything unless it is in your environment. However, on mac, VSCode uses your bash terminal. That means that you can run code by typing it in just like you can in your regular bash terminal but when you try to hit play, it runs out of your base environment. See if it works when you install the library to base. If it does, VSC is probably in the wrong env.