I'm trying to learn how to use VSCode, after installing Jupyter and trying to add pandas. When I installed VSCode along with Node I got a second version of Python, 3.12.6. Within VSCode I installed Jupyter and trying to follow a tutorial tried importing pandas, but got the 'No Module Found' error.
From what I've read trying to understand the problem, I don't think it's because Jupyter doesn't know which version of Python to use as I've set up a .venv in this Python 3.12.6, the same version that had installed pandas. I used pip show pandas
to confirm. It's pandas 2.2.3.
So, do I have to install pandas again within Jupyter? I'm doing it within an .ipynb file which shows '.venv (Python 3.12.6)', so as far as I understand I'm using the right kernel.
What am I missing?
I'm trying to learn how to use VSCode, after installing Jupyter and trying to add pandas. When I installed VSCode along with Node I got a second version of Python, 3.12.6. Within VSCode I installed Jupyter and trying to follow a tutorial tried importing pandas, but got the 'No Module Found' error.
From what I've read trying to understand the problem, I don't think it's because Jupyter doesn't know which version of Python to use as I've set up a .venv in this Python 3.12.6, the same version that had installed pandas. I used pip show pandas
to confirm. It's pandas 2.2.3.
So, do I have to install pandas again within Jupyter? I'm doing it within an .ipynb file which shows '.venv (Python 3.12.6)', so as far as I understand I'm using the right kernel.
What am I missing?
Share Improve this question edited Mar 7 at 16:07 wjandrea 33.2k10 gold badges69 silver badges98 bronze badges asked Mar 6 at 17:50 IchoryDickIchoryDick 32 bronze badges 11 | Show 6 more comments1 Answer
Reset to default 0Since you installed Pandas before setting up the virtual environment, it was installed in the global environment and is not available to the virtual environment. There are three way to solve this:
Install Pandas again into the virtual environment.
This is the best option if you're starting a project that depends on Pandas, because it gives you control over dependency versioning, i.e, if the project relies on features in specific versions of Pandas, those are part of the project and not the OS, where Pandas could get upgraded or even downgraded if you switch OSes.
In Jupyter (IPython), you can use
%pip install pandas
to install within the current kernel (current environment).If you were using conda, you would use
%conda install pandas
Outside Jupyter, you can activate the virtual environment then use
pip install pandas
(orconda install pandas
for a conda env).You may want to enable
python.terminal.activateEnvironment
in VSCode to have it automatically activate the environment in the internal terminal.
Use the global environment.
If you don't really care about the Pandas version and don't want to futz with a virtual environment, this is fine.
Let the virtual environment access the global environment.
This sort of defeats the purpose of a virtual environment, so I would only do this as a last resort.
Related
- Python environments in VS Code - VSCode docs
- Why do I get a "ModuleNotFoundError" in VS Code despite the fact that I already installed the module?
venv
Python stdlib module docs - Gives an overview of virtual environments
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744957991a4603311.html
%pip install pandas
in the notebook on the cell before you want to import pandas. The magic install commands (there's a%conda
one, too) ensure the .... – Wayne Commented Mar 6 at 19:18%pip install pandas
as an answer :) along with some of the related info like the links – wjandrea Commented Mar 7 at 0:11%pip install pandas
. As I don't fully understand what happens I didn't want to risk perhaps duplicating installations as I see the problems people have in having more than one Python version. To @wjandrea, yes. Pandas was installed first. It is a while ago that I installed VSCode, and Node. I can't remember the sequence now. Pandas was installed at that time, along with the newer version of Python. Setting up the .venv is recent, as I was following a VSCode tutorial and it was recommended. I'd appreciate it if you could post your solutions. – IchoryDick Commented Mar 7 at 12:16