VS code display ModuleNotFoundError: No module named 'pandas' - python

I use VSCode to write python scripts about gateway and created a project. This project needs to use pandas to generate and read Excel.Now, I have installed pandas with pip.I have simply written a demo to test to generate and read Excel, but when the code is integrated, run the code and display ModuleNotFoundError: No module named 'pandas'
I use pip list to display module, when i run the project,Only a part of modules are displayed after pip, and there is no pandas, but when I run a demo, he displays many modules, including pandas. how to solved it?
enter image description here
enter image description here

First picture: pip 21.1.1
Second picture: pip 19.0.3
Obviously, your project and the demo running in different environments.
From the picture you provided, it seems like the project using the global python environment.
You can try to install the packages in the global environment. But it's recommended to create a virtual environment instead.
If you not familiar with the virtual environment you can refer to here

I would uninstall pandas with pip and download anaconda which contains pandas, numpy, and lots of other very useful data science tools. Just open vs code, select the conda python interpreter, and everything should run smoothly. Conda ensures all your data science software works together and gets continual updates. It will download a separate version of python so make note of that in the PATH variables if you want your computer to use conda as the main python interpreter.

I think there are already many posts related to this problem. I think you should go through all that. I am leaving the link of a relatable post here. You should also check if you have selected correct python interpreter.
If you have already gone through all that, let us know.
VS Code: ModuleNotFoundError: No module named 'pandas'

You could be in a virtual environment in the project. Check which python interpreter is in use.
Once you have the right python environment selected, just for good measure, install pandas once again, possibly from the vs code terminal.
pip install
Sometimes when there are more than one python installations available incorrect order of python and python\scripts or python\bin can throw off the import statements.
you can run python -m pip install pandas to install pandas in same env from which python is picked.

Related

Trying to check my Python packages but keeps telling me that aiohttp is not installed

I am trying to check what packages are installed in my virtual environments, I have different packages from different virtual environments depends on what project I am working on. Whatever virtual environment I switch I always get this error when using help("modules")
aiohttp dependency is not installed: No module named 'aiohttp'. Please re-install black with the '[d]' extra install to obtain aiohttp_cors: `pip install black[d]`
My question is how do I install this package correctly, and by that I mean should I install it in my main Python PATH? I'm not sure if this is a mandatory package that I need it installed in my main environment if that makes sense.
My Python version is 3.9.6 by the way.
EDIT: I was able to fetch all of my packages.
I had to do a . Scripts/activate first, but it was weird because before I did not have to do the activate command before, since VS Code displays on the lower left portion of the screen what environment you are currently using, I'm not sure if I had any settings change prior to this bug.
Maybe you should try to open new terminal

Python and modules have different paths

I am learning Python specifically for data science and have little programming language although I'm a bit more familiar with R.
I installed some modules e.g. pandas and matplotlib, using the code:
python -m pip install [name_of_package]
which appear to be successfully installed, however, the console cannot find the packages when I type: import pandas
What I discovered was that the packages and Python have different paths.
For Python, the path is:
C:\Users\stone\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.9
For the packages, the path is:
c:\users\stone\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages
I believe the different paths are responsible for Python not finding the packages.
My question, therefore, is how can I point pip to install in the Python path? In other words for the import to be successful.
This entry seems to be similar to my question but the answers are confusing and may have been become out of date. If answering, please be explicit with the code. I'm very new to this.
Note: I really don't want to install it in a virtual environment because it is needed presently.
Thank you in advance.
Python is probably not finding your pandas library, because it's installed in a folder where python is not looking into by default.
When you type "import pandas" python is going to look for a package named pandas within a list of folders set by default.
To solve your issue you can move/reinstall pandas into a folder which python searches through by default or you can add another folder to search through.
Here's a way to do it with python adding directory to sys.path /PYTHONPATH
I am the original poser of the question and found a simple solution that worked for me based on the recommendation of python.org.
First, I uninstalled all python programme from my system
During the reinstall, I checked the box to allow adjust the system PATH environment variable
After installing, I used the `python -m pip install
[name_of_package] to install the pandas packages.
These solved the problem and I am now able to import packages without an error message.
The current path to packages is now:
c:\users\stone\appdata\roaming\python\python39\site-packages

IDLE and python is different, not able to install modules properly

thanks for reading this. I am using macOS High Sierra. I am not very familiar with terminal or environment variables, but am trying to learn more. From reading other threads and google, it seems like I either have multiple pythons installed, or have pythons running from different paths. However I am not able to find a solution to resolving this, either by re-pathing my IDLE or deleting it entirely.
I do have python, python launcher, and anaconda (not very sure how anaconda works, have it installed a few years back and didn't touch it) installed. I am trying to install pandas (pip install pandas), which tells me that I have it installed, but when I run it on IDLE, it says module not found. Though if i run python3 on terminal and type my code in, it works (so pandas has indeed been installed).
When i run which python on terminal, it returns
/Users/myname/anaconda3/bin/python
(when i enter into this directory from terminal, it shows that in the bin folder, I have python, python.app, python3, python3-config, python3.7, python3.7-config, python3.7m, python3.7m-config)
When i run which idle on terminal, it returns
/usr/bin/idle (im not even sure how to find this directory from the terminal)
When i run import os; print(os.path) on IDLE, it returns module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/posixpath.py'
Would really appreciate some help to figure out how to ensure that when i install modules from terminal, it would be installed into the same python as the one IDLE is using. Also, I would like to know whether it is possible for me to work on VSCode instead of IDLE. I cant seem to find suitable extensions for data science and its related modules (like statsmodels, pandas etc). Thanks a lot!
First of all, a quick description of anaconda:
Anaconda is meant to help you manage multiple python "environments", each one potentially having its own python version and installed packages (with their own respective versions). This is really useful in cases where you would like multiple python versions for different tasks or when there is some conflict in versions of packages, required by other ones. By default, anaconda creates a "base" environment with a specific python version, IDLE and pip. Also, anaconda provides an improved way (with respect to pip) of installing and managing packages via the command conda install <package-name>.
For the rest, I will be using the word "vanilla" to refer to the python/installation that you manually set up, independent of anaconda.
Explanation of the problem:
Now, the problem arises since you also installed python independently. The details of the problem depend on how exactly you set up both python and anaconda, so I cannot tell you exactly what went wrong. Also, I am not an OSX user, so I have no idea how python is installed and what it downloads/sets alongside.
By your description however, it seems that the "vanilla" python installation did not overwrite neither your anaconda python nor anaconda's pip, but it did install IDLE and set it up to use this new python.
So right now, when you are downloading something via pip, only the python from anaconda is able to see that and not IDLE's python.
Possible solutions:
1. Quick fix:
Just run IDLE via /Users/myname/anaconda3/bin/idle3 every time. This one uses anaconda's python and should be able to see all packages installed via conda install of pip install (*). I get this is tiresome, but you don't have to delete anything. You can also set an "alias" in your ~/.bashrc file to make the command idle specifically linking you there. Let me know with a comment if you would like me to explain how to do that, as this answer will get too long and redundant.
2. Remove conda altogether (not recommended)
You can search google on how to uninstall anaconda along with everything that it has installed. What I do not know at this point is whether your "vanilla" python will become the default, whether you will need to also manually install pip again and whether there is the need to reinstall python in order for everything to work properly.
3. Remove your python "vanilla" installation and only use anaconda
Again, I do not know how python installation works in OSX, but it should be reasonably straightforward to uninstall it. The problem now is that probably you will not have a launcher for IDLE (since I am guessing anaconda doesn't provide one on OSX) but you will be able to use it via the terminal as described in 1..
4. Last resort:
If everything fails, simply uninstall both your vanilla python (which I presume will also uninstall IDLE) and anaconda which will uninstall its own python, pip and idle versions. The relevant documentation should not be difficult to follow. Then, reinstall whichever you want anew.
Finally:
When you solve your problems, any IDE you choose, being VScode (I haven't use that either), pycharm or something else, will probably be able to integrate with your installed python. There is no need to install a new python "bundle" with every IDE.
(*): Since you said that after typing pip install pandas your anaconda's python can import pandas while IDLE cannot, I am implying in my answer that pip is also the one that comes with anaconda. You can make sure this is the case by typing which pip which should point to an anaconda directory, probably /Users/myname/anaconda3/bin/pip
First: This would be a comment if I had enough reputation.
Second: I would just delete python. Everything. And reinstall it.
To repeat and summarized what has been said on various other question answers:
1a. 3rd party packages are installed for a particular python(3).exe binary.
1b. To install multiple packages to multiple binaries, see the option from python -m pip -h.
To find out which python binary is running, execute import sys; print(sys.executable).
3a. For 3rd party package xyz usually installed in some_python/Lib/site-packages, IDLE itself has nothing to do with whether import xyz works. It only matters whether xyz is installed for 'somepython' (see 1a).
3b. To run IDLE with 'somepython', run somepython -m idlelib in a terminal or console.
somepython can be a name recognized by the OS or a path to a python executable.

How to import python packages without pip install

The server that I am using has an older version of python installed, and I am unable to install packages. Is there a workaround to importing python modules? If I can create my own module and append the directory to be able to import the newly created module, then I am assuming that I should be able to do the same for already built packages. I just need to know how I would be able to install what is required for numpy and then import it using a similar method as when creating modules.
I would recommend that you find a machine to install the same version of Python to match your server. Perform the pip install from that stand alone machine. Do some level of testing to make sure that the code with run with that version of Python and everything is happy. Just the other day I found out the hard way that TensorFlow would now work on the latest Python.
The libraries are being installed for me under c:\users\<profilename>\AppData\Local\Programs\Python\Python36\Lib\site-packages So you could then copy that over to the server.
I found this link that is saying the same thing.
https://superuser.com/questions/943980/is-it-possible-to-install-python-packages-without-a-direct-outbound-network-conn
Good Luck with it.

Unable to use downloaded third-party library from within PyCharm

I'm working on a script that will plot data onto a map using the Basemap library. I'm trying to import Shapely as well for use in this same script. I'm working with Anaconda2 for Python2.7 in a Windows 7 environment. I used conda install to download the tar.bz2 file from the Conda Packages site (using Windows command line) and it looked like it all installed correctly.
When I open Pycharm and look at my accessible site-packages, I can see this package. However, when I try to use it within my script, I get an error saying that the package does not exist. I ran the script through the debugger to see if it would shed any more light, but I got the same error. Here's a screenshot of my available site packages when I go to Settings-->Project Interpreter from within Pycharm.
Screenshot of PyCharm site packages available
Is there something special I need to do in order to access this package from within a fresh Python file? I was trying to say "import shapely" or "import osx-64-shapely", but both give me the same "package does not exist" error message. I've been able to successfully use other third-party libraries within Python, so I'm not quite sure what the error is here....
I'm new to SO - if you need more details or there's some piece of info I didn't include, please let me know. Thank you for your help!
EDIT: I am NOT asking what the difference is between conda and pip, or how to use pip within PyCharm. I have used both successfully before to install third-party libraries. What I am asking is what might cause a third-party library that appears to have installed successfully from the command line become inaccessible from within PyCharm when I attempt to import it.
I'm not familiar with Shapely but I was astonished to see the name osx-64-shapely as a site-package for your python installation which is in windows 7. Are you sure you downloaded the right file? :)

Categories