`Why is Visual Studio Code not recognising an installed Python Module? - python

Recently, I started learning python. I'm at the 8th chapter of a book called "Automate the Boring stuff with Python". The author uses a module named 'pyinputplus'. I installed this module with pip (pip3) and tried to play with it in my terminal, and it worked.
After playing around with this module, I decided to make a small project in Visual Studio Code. I had the "Python" plug-in installed. (It was made by Microsoft, and it had linting, debugging, syntax highlighting and more.) Strangely, it started throwing errors. This was my code:
import pyinputplus as pyip
It seems to work fine without the plug-in, although running it without the plug-in wasn't very convenient, as I had to run it in my terminal every time.
This was what is was saying:
Cannot import 'pyinputplus' due to syntax error 'invalid syntax (, line 268)'
Obviously, I didn't have this many lines in my code. (I had only one line of code.) I'm guessing this is a bug, but as a beginner I'm not really sure. Does anyone else have this problem? Any answer would be appreciated.
Here are my plug-ins in use, Vscode version, etc.
MacOS Version: 10.14.6
VsCode version: 1.47.2
Python-related plug-ins/extensions: 'Code Runner' and 'Python'
Python version: 3.8.4
I tried reloading VSCode, But it still didn't work.

First, you have to install that module on your computer.
By using:
pip install pyinputplus
Then, you can import it by:
import pyinputplus as pyip
and then, you will get results like this.

Related

Python Perforce module Error: No module named 'P4'

I got the P4 python module for win10 via the installer on their page: https://www.perforce.com/downloads/helix-core-api-python
Its for python 3.9, though I have tested it with both Python 3.10 and 3.9 and it always throws the error: No module named 'P4' when I try to use it inside visual code.
Interpreter is set up correctly (tried 310 and 39).
Im a bit lost here and would love some help.
The installer package installed the following files into site-packages:
P4.py
P4API.cp39-win_amd64.pyd
and a folder with 4 metadata files in it
P4.py on itself looks good, no errors.
Edit: Im trying to use it with Blender. Currently shipped python version within that is 3.10.2. Also tried a older blender version which uses 3.9.7.
Both wont work.
Visual studio code is known for it's bugs with python. You could try using "PyCharm". Make sure to install the modules from Pycharm's settings instead of terminal.
(I'm not sure if this is the reason, but it's worth the try. Had the same issue and the mentioned way solved it.)

importing dlls with pythonnet in spyder

I have a python package that acts like a wrapper for a .dll written in C#. The binary is imported into python by the pythonnet package. Thus, the user can access the functionality of the so called cs_backend.dll conveniently from the python side. The import looks as follows:
import clr as __clr
import System as __System
__clr.AddReference(PATH_TO_CS_BACKEND + "\\cs_backend")
import cs_backend as __csb # exception thrown here
However, it seems that python can't import the .dll as I get a ModuleNotFoundError: No module named 'cs_backend'.
The odd thing is that this error only occures when running the code from Spyder. Executing exactly the same code from Visual Studio Code works perfectly fine.
Do you have any idea what the problem could be?
system specifications
os: windows 10
.net framework v4.5.1
python 3.8.5
spyder 4.2.1
Edit:
I have found a github issue on the pythonnet repository that describes a similar (although not quite the same) problem. However, since it has been open for about 3 years, it seems that there is little to no progress on that.
Edit 2:
There is a similar question here on stack overflow. The solution was to install a newer .NET Framework. This is not my problem since with any IDE other than Spyder the code works just fine. Thus, I guess that this is mainly a Spyder issue.
Currently, it seems that there is neither a solution nor an effort to resolve this problem.
It looks like the problem is caused by the IPython console integrated in Spyder.
A simple workaround is to execute the python script from the system console.
In Spyder this can be done by going to Run - Configuration per file - Execute in external system console.
With the new configuration it is at least possible to run the script.

visual studio code can't find my python module i've installes with pip

this might be a very stupid question but I'm a beginner.
I want to use this package called colorgram, it's a module that extracts colours from images, and I installed it using pip via the CMD. but when I try to import it in VS code it can't find it and I don't know what to do, please help
#Andrii Zadaianchuk
sorry, I'm using windows 10. I tried to import it in python in the CMD but nothing either, just the same ModuleNotFoundError: No module named 'colorgram' error.
I did just notice that the modules python version is 3.5 and VS code is running 3.9.2. I think that might be the problem

Problems using Visual Studio through Anaconda

While Anaconda is nice, my experience trying to use Visual Studio through Anaconda is a mess. I have just ended up using Spyder which is great but I'd preferably use visual studio.
Couple of issues:
I select base conda interpreter in visual studio and proceed to try to import pandas or numpy. This results in an error (AttributeError: module 'tokenize' has no attribute 'Name').
A couple of days ago it randomly worked and now it's not. running print('hello') works just fine.
I have no issues with any imports in Spyder but I've started using Flask and it'd be much easier to have it all in one workspace.
Any understanding why I cannot import pandas.
Edit: Did a clean reinstall of both and now am able to install numpy. However, I get the error when trying to install pandas. Using 3.8.3. Pandas works in Spyder, not in VS Code.
Ok so it started working for no apparent reason. I just restarted VS Code through Anaconda and this time it had no issues. As I said, I feel like VS Code is all over the place through Anaconda and not very reliable.

The PIL library import fails

Being a complete begginer in python, I decided to install the python interpreter 3.4.4, and also PyDev plugin for eclipse IDE. I am also using windows 10.
I have encountered a problem regarding certain imports, namely : from PIL import Image, ImageTk, which is apparently an unresolved import.
I have looked at certain questions which were similar to my situation, but most of them gave a solution like installing packaged from the linux apt-get. Here are some topics I visited :
Uninstalling and reinstalling Pillow and pip,
Installing pillow-imaging-tk on Ubuntu,
This topic which left me very confused,
This one seemed helpful, but is on Unix OS again
So please, could someone explain to me why I am seeing this error, and how could I correct it if I absolutely want to use Eclipse, Pydev, windows 10 and Python 3.
Found the solution, here's what I did:
Set the PYTHONPATH like it is shown in this article, make sure python.exe is accessible via cmd,
Via cmd, type pip install pillow. Alternatively, you can enter the same command from Windows+R,
(Not sure if relevant step) Via eclipse, Windows->Preferences->PyDev->PythonInterpreterremove your interpreter to re-add it,
Restart eclipse.
For Python import problems in PyDev, the project web site has a page on interpreter configuration that is a good place to start. I recently had a similar problem that I solved by adding a module to the forced builtins tab.

Categories

Resources