Failure importing openpyxl in Spyder 5.1.5 - python

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.

Related

How can I download SciPy on Python (PyCharm app)? [duplicate]

I am using python 3.6.5 with PyCharm.
While trying to install any package, it gives the error
"Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' installed for your Python interpreter located at 'C:\projectPath\venv\Scripts\python.exe'"
Project path and actual python installed paths are different.
In settings of PyCharm, pip version is 19.0.3 and latest version is 19.3.1
Also, "python -m pip --version" from command prompt says pip version is 9.0.3
What do I do?
I imagine that you have more than one Python installed in your system.
you should go to Pycharm --> file> settings> project>project interpreter.
you should find the other Python instance on your system and try to change your interpreter to that one.
if it does not appear in the list click on the button that looks like star in front of the project interpreter.
I had the same problem.
I realized that I installed one python by myself and another is installed while installing PyCharm.
After changing my interpreter I do not receive that error anymore.
This error occurred to me too when using PyCharm, and trying to install MeCab in it. And by choosing mecab-python3 instead of just mecab (which is what is for Python2 I assume...).
You might install the package compatible to the Python version used in the package, but not to yours, which is that PyCharm refers to'dir:\projectPath\venv\Scripts\python.exe' . So you should choose the different version of the package, or change interpreter to another.
I got the same error along with no such option: --build-dir after upgrading pip from PyCharm's (2020.1 #PY-201.6668.115) terminal (PyCharm didn't allow me to do so from interpreter's settings).
Turned out older versions of PyCharm rely on --build-dir which was removed in newer versions of pip. To resolve the issue I had to downgrade pip.
Solution was found here
Faced same issue after trying to change the file path.
Tried changing the interpreter location but it dint work for me. so I uninstalled pycharm and re-installed it. working fine now.
I faced the same problem on Windows-10 (PyCharm 2020.1.2 version) when installing h5py package. I didn't want to change my interpreter as that will raise other compatibility errors in my code.
In my case:
What didn't work: upgrading the pip
What worked: I chose old h5py version-2.10 (instead of the latest h5py version selected by default by PyCharm) and it worked.
Mentioning just in case if this works for someone. Uninstalling both Python and Pycharm is too much to do for this error (in my opinion).
File-> settings
searching for project -> and choosing "python interpreter"
in "python interpreter" tab choosing "show all"
pressing in the left down side on "+" (add)
adding the right direction of python installed place and press "ok"
removing the previous version
I faced the same error while using Python 3.7.5 and Pycharm 2019.1.2 for my Django project.
It also causes failure in execution of many other python commands, and is not caused by environment variables or path.
I guess it's because some python files are broken or corrupted.
The easiest and fastest way I found to get out of this was :-
Uninstall Python.
Uninstall Pycharm.
And Reinstall latest versions of both.
I had the same problem and but see solution uninstall IDLE python and pycharm and install least version
Uninstall Python.
Uninstall Pycharm.
And Reinstall latest versions of both.

How to install Python packages for use with Spyder without Anaconda

I've been trying to lean Python and trying to find a good Python coding environment. Spyder seemed to be quite powerful. Though I having problems installing non-default packages. I have to install them in the default environment using pip, since I'm using LabVIEW to connect to python and for now LabVIEW only support the default environment path.
In Spyder I have specified the the Python root folder: C:\Users<user>\AppData\Local\Programs\Python\Python37-32. The user environment Windows key also contains this path.
I continue to get the "ModuleNotFoundError: No module named 'impedance'" error message. According to this post, this method should work, but no luck so far.

How to reset Python in macOS

Currently, I feel like my Python ecosystem is out of whack. Several years ago, I had shared my computer with someone else, and now I am discovering that my machine has many versions of Python scattered about. From what I could find, starting from Macintosh HD:
anaconda
Applications/Python 3.6
Contains IDLE, Python Launcher, etc...
Library/Python/2.7/site-packages
This contains things like pip and wheel (I am unsure what this is)
Library/Frameworks/Python.framework/Versions
This contains two folders, 3.5 and 3.6.
System/Library/Frameworks/Python.framework/Versions
Inside here there are many alias folders, all which point to 2.7
Is all of this supposed to be normal? I am trying to run Python from the terminal, yet I have been getting messages such as ImportError: No module named site. pip has also not been working.
Is there a way for me to reset the Python on my machine to just that which comes with macOS? I feel like starting over from a blank slate would be helpful, since I want to get things setup in some type of comprehendible way (e.g. a way in which I've set things up so I know what is on my machine instead of many random things put on it by another).
This is very normal. you have a preinstalled python2.7 which comes with macOS and another one "Anaconda" which has installed manually. You first need to check what is your default python path (version) on your macOS, I mean if you are using anaconda or the default pre-installed python2.x. To remind you can check like below:
python --version
output (for me): Python 3.7.6
then if you want to change it to another version/or use another version under conda you can check these two answeres of mine.
How to add anaconda to PATH?
and here:
How to set the default python3 to python3.7?
It is normal that when you install a package for your default python version you don't expect it to be installed on the other one too. Normally it is better to install python2.x as an environment of conda and switch between two environments with "conda activate py2" and "conda deactivate" to go back to your default version. for each of them, you need to be in the environment and then use pip.
if pip is not working, it may need to be installed. On macOS, as you remember, you can use
brew update
brew install pip
or "easy-install" instead of brew (or whatever you use for installation).

(Python)When installing pip, preferences-->project interpreter <PLZ>

For example, I installed (pip install pandas) on terminal on MacBook.
I could import pandas well in Idle, but when I imported pandas on Pycharm,
there's red line below pandas, and it did not work.
So, I went to preferences ->Project interpreter-> and then I installed pandas (by clicking + button). After that, everything worked.
My question is do I have to do that process whenever installing pip on terminal?
installing pip on terminal is not enough ?
It is very common for Python developers to use multiple virtual environments for different projects as they might, e.g. want to install Python 3.8 for one project, but for another they're using libraries that don't yet support 3.8.
PyCharm allows you to choose, and even create, a virtual environment for each project. You control that for the project at the Project Interpreter screen that you already know. You don't have to use a different one for each one but you have that option.
Apparently the default environment that you used to install Pandas in Terminal is not the same as the default you chose in PyCharm. Now you know that. PyCharm also has its own terminal (click the Terminal tab at the bottom-left of the IDE) and if you use that to pip install it will do so into the environment that you are currently using within PyCharm.
If you're always working in PyCharm, you can get used to the idea of installing inside PyCharm for the sake of simplicity.
You might want to take some time and read the PyCharm documentation here as that goes into much greater detail that I can about how to use different types of virtual environments.

Clean Mac from different python versions

I've started to use python and recently i've bought my first mac.
I saw that Mountain Lion comes with their own python versions, despite i've decided to install a new python 2.7.5, and also Django and MySQL-Python. Everything works fine.
I've decided to try scipy and following the installed steps suggested by the official site with macports It seemed that macports installed another version 2.7.5 of python. Now if i try to run python in my terminal and import numpy it fails.
I saw that when i run python in my terminal, it execute
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
But if I go in
cd /System/Library/Frameworks/Python.framework/Versions/2.7/bin
and run ./python i can import django and numpy modules but not MySQLdb modules.
I'm really confused how I can fix that mess.
I would like to have just one Python version installed in one place with all the modules I need.
Or please suggest something to clean that mess, know and have the control to what modules I have, where they are and how to import them! And also control which version of Python I want to run.
Edit 10/27/2013: i've solved following the link suggested in the comment, deleting all versions in /Library/ and all simlinks in /usr/local/bin, after that i changed to the path i wanted the py interpreter in my bash_profile.
Actually the problems seems to be created by macports that has installed numpy using the py interpreter installed under /System/, so i changed my default py interpreter to that installed under /System/ and installed the rest of packages i need. Now everything works as i want.
Personally, I wouldn't mess around with the built-in version of Python. I had some real issues with my Mac when I did that a few months ago.
You could try Canopy instead. It's a Python development environment which is great for writing and executing code. Any packages you need can be easily added (or removed) using the package manager. https://www.enthought.com/products/canopy/

Categories

Resources