Importing python modules in vscode vs interactive shell - python

I am just learning python. I am trying to import modules in vscode, installing them using pip. When I try to import the module it says (Pic 1) I have already satisfied the requirements but then (Pic 2/3) it says module is not found when I go to run the program in the Jupyter interactive shell. (Pic 4) However when I run the same code in the python interactive shell it works fine. This seems to be happening for all modules not just the one in this example (pyperclip). It seems like vscode/Jupyter may be looking in a different place than the python interactive shell is. However I am stuck at this point and not sure what else to try.
Thank you for you time and any help you may be able to provide.
(Pic1) Installing module with pip
(Pic 2) Code I am trying to run
(Pic 3) Output from Jupyter shell
(Pic 4) Same code in python interactive shell

I think you need to install you package using anaconda prompt, because sometimes its happen when you are using different platforms. So try pip install pyperclip in Anaconda prompt, hope this will solve your problem, if not then watch this video completely which will give you an idea how these import error occurs and what are the solutions.

Related

vsCode won't recognize python installation, but does?

When I put "py --list" in the terminal I get this:
Installed Pythons found by C:\Windows\py.exe Launcher for Windows
No Installed Pythons Found!
I set my environment variables. I actually got VS Code to run some code I threw together that didn't use any imports, but now that I'm trying to use any python commands in the terminal, I'm getting errors. What gives?
python should work, since it is finding installations.
If you are working on Windows, very often it's all about permissions. Try running your terminal and VSCode as administrator! Worked a lot of times for me, also with other languages.
Please ensure that you have installed the python extension and selected the correct python interpreter (Ctrl+Shift+P and type Python: Select Interpreter).
If this didn't work, you may have to reinstall python.
Read the document about Vscode-Python for more details.

Unable to Import & Import could not be resolved

i started programming today and i'm testing different code segments on Visual Studio Code. However, when i tried importing a "numpy", 2 errors occured. I then proceeded to check other stack-overflow posts regarding this and I tried switching the Python Interpreter, however nothing changed. I also attempted doing other suggestions, however I don't understand how to do some of the suggestions (its my first time programming etc.)
enter image description here
Below is a zoomed up picture of it
enter image description here
I was wondering if I could get some help?
Run
pip show numpy
in integrated terminal, check if its location is <your selected interpreter>\lib\site-packages.
If is, reload window then the error should go away.
If not, please open an integrated Terminal and reinstall the module to current selected python environment.
Try opening the project folder in VsCode instead of opening the file by itself. If that doesn't work, run the file through CMD, if running it through CMD doesn't work, you might not have the numpy library. You can do that by going to CMD and write the following command:
pip install numpy

Error occurring while installing and importing pynput

So I am trying to install and import pynput in VSCode but its showing me an error every time I try to do it. I used VSCode's in-built terminal to install it using pip and typed the following :
pip install pynput but this error is shown : Fatal error in launcher: Unable to create process using '"c:\users\vicks\appdata\local\programs\python\python38-32\python.exe" "C:\Users\vicks\AppData\Local\Programs\Python\Python38-32\Scripts\pip.exe" install pynput': The system cannot find the file specified
After receiving the following error, I tried using CMD to install it but the same error is shown. I also tried using python pip install pynput and it shows Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases. even though I have python 3.9.7 and I have selected it as my interpreter in VSCode and I have IDLE(Python 64 bit) installed. How may I resolve the following error? Any help regarding the same is appreciated
Thanks in advance :)
There's no such thing as an in-built terminal in VS code. When you open a terminal in VS Code, it opens the default, which on Windows is usually equivalent to opening up CMD.
If you selected Python 3.9.7 as your default interpreter in VS Code, it does not mean that it will visible to your CMD / terminal. It just means that the VS Code IDE will refer to that instance of Python when launching the program from VS Code itself using the green button (or F5), and when scanning your code to point out missing packages, etc.
CMD will only automatically detect your Python if it's in your PATH environment variable. You should add the Python 3.9.7 base and Scripts path to this.
Also, it would be best if you could first uninstall conflicting versions (like your 3.8.x) of Python and remove them from PATH, assuming that this won't cause any problems for you. Perhaps keep a record all the installed packages in this old version of Python for future reference using pip freeze or pip list.
Check if c:\users\vicks\appdata\local\programs\python\python38-32\python.exe exists by typing cd c:\users\vicks\appdata\local\programs\python\python38-32

PyCharm Run Doesn't Recognize Packages But Console and Terminal Are OK

Running PyCharm 2020.1.2 Community Edition in Win10 with a Python 3.6 venv as interpreter. Installed the package feature-engine through the Project Interpret interface, installs fine and appears in the list. I can successfully import feature_engine in the PyCharm console, and I can use it fine. I can also execute a .py file with this import statement in the Terminal with the venv activated, and it also works fine. However, when I try to Run the same .py file with the import statement, I get:
ModuleNotFoundError: No module named 'feature_engine'
I have tried using import and importlib, thinking the issue was the hyphen, but those didn't work. I have tried uninstalling and reinstalling, restarting PyCharm, etc. Nothing seems to work. Any suggestions how to get the Run function working?
EDIT: Thanks for the suggestions. Attached are the Run configuration and the Project interpreter configuration. As far as I can tell, the environment is the same.
Below are examples of the error trace. The object being Run is a Flask app, which imports packages that use the feature-engine library. The actual import statement in the final import is simply import feature_engine. Trying to import the method directly using from feature_engine import variable_transformers as vt also fails.
Make sure you're using the right configuration to build your program (that is, using the python executable of the right enviroment). You can check this in the top right corner, where the run button is.

VS Code does not detect Conda packages when launched without the Anaconda Launcher?

VS Code is unable to detect the torch package that I installed exclusively in one of the Conda environments. If I launch VS Code through Anaconda Launcher, however, then it detects torch and everything works fine.
I have done many things in order to make the Conda environment work on VS Code PowerShell terminal, and so far Conda is getting activated on VS Code terminal properly regardless of whether I launch it with or without the Anaconda Launcher. But when trying to run the the program via Code Runner, it always shows the error that the torch module is not found.
But doing the same thing in terminal or launching VS Code through Anaconda launcher works just fine.
I don't know what to do, it is so annoying. I really wanted to work with Python on VS Code as it looks neat and clean compared to other editors and also since it is also light and fast. Unfortunately, making Conda work on VS Code is a nightmare, and it seems like the only way to make it work is through launching it via Anaconda Launcher, but opening Anaconda Launcher takes too much time compared to VS Code -_-
Please help assist with launching the code through VS Code.
It's because conda activation sets various environment variables which are required for a conda environment to function appropriately. And I suspect the Code Runner extension simply doesn't understand the concept of a conda environment, and so it isn't doing any activating which makes the import search fail.
You can try executing code directly through the Python extension instead of Code Runner to see if that works better for you.

Categories

Resources