I just installed Python and Visual Studio code on a Windows 10 machine.
I have imported i few libraries using pip. When I did that I got a warning saying I needed to add a certain folder to PATH which I did.
Using the cdm, I can start a python environment and import matplotlib without issues. I note that my Python version in the cmd is Python 3.9.7
When doing the same thing in Visual Studio code I get the following error message
ModuleNotFoundError: No module named 'matplotlib'
I also notice that my Pyhton version in Visual Studio Code is Pyhton 3.9.1.
I dont really understand what is going on here since I've never had this issue before. Usually it "just works"
Thanks
You have 2 versions of python installed. One of them could be a virtual environment. You can diagnose which Python interpreter you are using by running:
import sys
print(sys.executable)
If VSCode you can switch between interpreters; it may take some searching through the options.
In to the visual code terminal, which probably is using your venv (you should see "(venv)" at begin of each row) type:
pip list
and look if you module is installed. Otherwise you can install here or change environment.
You have installed in two python versions Python 3.9.7 and Python 3.9.1 in vs code.Matplotlib was installed in Python 3.9.7 in vscode, but you used python3.9.1 as an interpreter, that’s why it threw No module error. You need to add a virtual environment of Python 3.9.1. Python has a full guide here on how to set virtual environment How to add virtual environment
then you change python interpreter
matplotlib has been installed in the python 3.9.7 while not in the python 3.9.1.
So you can try to switch the python version in the VSCode to the python 3.9.7 or install the matplotlib in the python 3.9.1.
You can click here to switch the environment. And you can refer to the official docs for more methods and details.
Or you can install the matplotlib package again in the VSCode terminal.
Related
So I am trying to open a.xlsc file in Spyder using pandas. After importing pandas as pd, when using pd.read_excel() command in the console, it showed the error:
ImportError: Missing optional dependency 'openpyxl'. Use pip or.....
Then I installed the module with:
pip install openpyxl
successfully in CMD, but again Spyder shows the same error.
I tried to restart the kernel, close and reopen Spyder, and even restarted my machine to no avail.
Openpyxl is listed in both help ("modules") in python shell as well as in pip list in CMD.
When I tried to import the openpyxl itself in Spyder (import openpyxl)it says "Module not found error: No module named "openpyxl"."
I have only python 3 (3.9) not both 2 & 3 and Spyder is the only IDE I use with python.
I apologize if this question sounds stupid, I'm fairly new to Python.
I would appreciate any help regarding this.
Thank you.
Spyder has its own Python interpreter 3.7.9 so it loads only the modules installed for that Python.
Since I had a separate Python 3.9.7 installation, whenever I used pip via CMD it installed all the modules for the 3.9.7 Python, that's why my Python terminal & CMD were able to list openpyxl but Spyder said it isn't installed.
After ransacking through the internet I switched the Spyder interpreter to the 3.9.7 stand-alone installation by Tools > Preferences > Python interpreter > Use the following interpreter > navigate through your directory and select the stand-alone python 3.7.9 .exe. Afterwards, Spyder said "Your Python environment or installation doesn't have the spyder‑kernels module or the right version of it installed. Use pip install spyder-kernels==2.1. to install..."*. So back to CMD, I installed the spyder-kernels and now everything's fine.
Spyder IPython console shows Python 3.9.7 (Used to be 3.7.9, Spyder's own interpreter). Now I'm able to import openpyxl without any issues.
I had the same issue as Anoban, but the answer provided by Anoban didn't work for me. I was able to resolve it in two ways
1. Changing my Python environment
This is the better, quicker answer IMO. My Spyder environment was somehow using a custom Python environment as its default (see picture).
Once I changed the Python environment (Tools > Preferences> Python interpreter) from "Default" to the following path (it should have been the default Python interpreter in the first place IMO):
"C:\Users\username\miniconda3\python.exe"
...it worked (see image below to what it should look like in the IDE).
I still had to do an install of spyder-kernels=2.3 (see the next point in my post if you have trouble with that)
2. Creating a separate Python environment
I got this way to work before I figured out the method described in #1 above, so I am posting it too, since it may help someone.
I used the help provided on the Spyder website, followed the directions provided on their site exactly, and it worked for me.
FYI, my installation is slightly different than Anoban's - I didn't use pip. I installed miniconda and a stand-alone version of Spyder on my Windows machine. I used the Anaconda prompt as the terminal window, not the Windows Command Prompt.
Hope this helps someone.
update: when I changed the Python interpreter to the new environment, the Python console gave me the following error
The Python environment or installation whose interpreter is located at
C:\Users\rebecca\miniconda3\python.exe
doesn't have the spyder‑kernels module or the right version of it installed (>= 2.3.0 and < 2.4.0). Without this module is not possible for Spyder to create a console for you.
You can install it by activating your environment (if necessary) and then running in a system terminal:
conda install spyder‑kernels=2.3
But it couldn't find the correct channels for installation so I needed to use
conda -c conda-forge spyder-kernels=2.3
then it worked.
#rpinto73
I know it's tad late. I'm wondering how my answer differs from yours lol.
We faced the same issue and came up with the same solutions. The difference is you set the Python interpreter in a conda environment as default in Spyder whereas I set my standalone Python installation as default. Thus we both assigned an external Python interpreter to be used inside Spyder. Since the external interpreters we assigned (a global Python interpreter in my case (that's why I used pip) and a Python interpreter inside a conda virtual environment in your case (so you had to use conda, you could use pip inside a conda virtual environment as well but not a good practice since it could break dependencies) didn't have the spyder-kernels module we had to install it manually.
The major issue with Spyder is it is an IDE WRITTEN in Python. Thus it needs a Python interpreter to load & function; which is the reason for it being painfully slow during loading. Spyder packs some mundane data science libraries like pandas, numpy, scipy with itself, which you make use of when using Spyder's default Python interpreter. However Spyder does not allow users to manually install packages for its default interpreter (as far as I know). Which makes sense since any conflicts in dependencies introduced by users manually installing/upgrading packages might break the functionality of Spyder itself, making it dysfunctional. Thus the best solution is to leave the Spyder's interpreter to Spyder itself and use a custom Python interpreter where we can install and upgrade modules as we please!.
And a free advice, it's okay to use Spyder when you are starting out with Python as beginners, but it is useless when you have to do heavy computations. You will mostly find yourself facing memory errors. It is lightweight IDE best for learners & beginners. It's best to switch to a more general yet capable development environments like Visual Studio Code, Jupyter Lab.. or a full-fledged Python IDE like PyCharm (there is a free community edition) if you want the in-IDE variables pane & plots pane. But be warned PyCharm (generally any IDE from JetBrains) is notorious for having massive memory footprints.
I am trying to import from lxml.html import fromstring in VSCode but I am getting the error: ModuleNotFoundError: No module named 'lxml' I have installed lxml with pip install lxml
I am using a virtual environment and VS code
Version: 1.52.1 (user setup)
OS: Windows_NT x64 10.0.18363
My python version is 3.7.4 and I forced a reinstall of the wheel file
https://files.pythonhosted.org/packages/7f/7e/e74f03e6e56184f8af8e33055223cb1d8ecec91a7084ca626b8070b2c67f/lxml-4.6.2-cp37-cp37m-manylinux2014_aarch64.whl
This did not solve the issue, and the 32bit wheel version wont work on my 64 bit machine.
The only solutions I could find were the install from wheel and also try the 32bit version.
VS Code shows that the module cannot be found because there is no such module in the currently selected Python environment, Please refer to the following command to check the module:
Please use the command "pip --version" in VS Code to check whether the Python currently used in the terminal is consistent with the Python displayed in the lower left corner of VS Code:
If it is inconsistent, please use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, then it will automatically enter the selected Python environment (Python shown in the lower left corner).
We can use the command "pip show lxml" to check its installation location:
Run:
Reference: Python environment in VS Code.
Thank You #JillCheng I resolved the issue by deleting the PATH variable I had in the system environment - it was being used with VScode and conflicting with the virtual environment.
I have been working on python for a quite some time now but I am new to visual code studio and Linux. The OS I'm using is Ubuntu 18.04.3 . I installed python extension in visual code studio. But it did not has pandas library installed. Also we cannot install packages from python terminal. So I installed pandas from Linux terminal using pip install pandas. I reopened my VS code and I still couldn't import pandas library. It is still throwing module not found error. Can someone please explain how to install pandas in VS code for linux?
Thank you
Pandas you installed with pip must be installed with the python you have in your system (if you are not using virtual env).
In VS code set the python interpreter to be used as same present in your linux terminal.
Have a look at https://code.visualstudio.com/docs/python/python-tutorial . It is explained in detail over here.
But we should try to use virtual env so that these type of problems can be resolved quickly.
I'm new to python trying to figure out some basics. I installed python 3.5 and then, to use jupyter notebook I installed anaconda distrubition. After that, I installed pycharm and started learning python.
After a few weeks, I was able to create a simple project which makes use of pyqt5. My project runs when I run it from pycharm itself, but when I try to run it with "python" command from cmd I get an error, stating that import from pyqt5 has failed. I did some research, and installed pyqt5 using pip, but the problem persists.
So what should I do to run my project without anaconda, using python 3.5?
It seems the anaconda distrubution and python itself were conflicting. Whenever I tried to install a module using pip (like pyqt5), it is installed under anaconda's python so when I try to invoke my own python from console i failed to use those installed modules. Hence there seems to be two option;
Delete all python related stuff from the computer and clean install python only - no anaconda. (I did this)
Or when installing a module, try using different pip's for the two python distrubutions on our computer. Check this link: Install a module using pip for specific python version
I've installed the new Anaconda 5.1. Before that, I had Anaconda 4 installed on my Windows 10 computer.
However I'm only able to use the preinstalled anaconda modules like numpy, matplotlib. If I install a new module for example over pip I can't use it, because my IDE (Visual Studio Code, pyCharm or Jupyter Notebook) will not find it. If I open the python console directly, I can import any module. The IDEs use the correct python path.
I've reinstalled anaconda a few times, but without any positive result.
Anyone with the same problem have a solution?
Not sure if I understood your question on 100%, but you can try this approach:
1) Open Anaconda prompt
2) Type conda install [packagename]