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.
Related
Trying to install new packages via pip on Windows, but after it installs - Python cannot resolve the import. For Example, I have just installed the Python package similaritymeasures and pip correctly installed it, but when I import it into my Python program, it cannot find the import. Has anyone know a way around this?
Make sure that you restart VSCode after downloading your package. Sometimes, when you download a package while VSCode is still running, it doesn't recognize the package when using import similaritymeasures.
If that didn't work, then make sure that VSCode is using your preferred version of python. It is possible that you have more than one python version and it's using a non-compatible one to run your code. This can be done at the bottom right corner of the window by clicking the box that says: (for example) 3.9.5 64-bit. Then select a different (preferably older) version from the pop up widnow.
These are the solutions that came to my mind, I hope this helped.
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.
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).
I'm pretty new to programming, and very new to doing so in a UNIX environment, so please bear with me.
When I run
import sys
sys.executable
in my Python 3 console, the output is
usr\bin\python3
and I'm able to import whatever libraries I've installed with pip3, no problem.
When I do so in my Jupyter Notebook running a Python 3 kernel, the output is
usr\bin\python
and the libraries that I've installed with pip3 are inaccessible to me. What can I do to fix it so Jupyter is executing Python from the right place so I can use anything I've installed for Python 3 with pip3?
Thank you!
Short
You need to register your Python kernel with Jupyter, for it to find the "right" Python
Long
Jupyter is meant to work with multiple kernels, and languages; it is common for some users to have tens of kernels, many can be the same language, with small differences. You usually need to "tell" jupyter about these kernels, it is often referred to as "Installing a kernelspec". In your case you need to:
Install IPython & ipykernel for your Python 3
Using the ipykernel you just installed: install the kernelspec.
You'll find instruction on above link, a few tip though:
You can always safely replace python by /full/path/to/python if you are unsure.
when using pip, you can always replace pip (or pip3) by /full/path/to/python -m pip to use the pip linked to the python you want.
If you are tempted to use sudo, don't. People telling you to use sudo get issues after a few month when their linux distribution need to be updated – unless you really know what you're doing.
Once you've registered the Python kernel with Jupyter, it should just appear in the menus. You may need to refresh your browser though.
Enjoy !
My python installation is a mess.
Therefore I'd like to reinstall the entire installation of it. (Unfortunately,) I've also installed QGIS and PyCharm (mostly making it a mess) and I want to start clean..!
So, what is the best way to get rid of every little python thingy and what are the best packages/methods for reinstalling Python27, QGIS and PyCharm? Should I go for osgeo or not, should I first install Pycharm or Python etc. Hopefully you have some good thoughts and tools on this. I'm a fan of pip, so in the end I hope its possible just to use pip for installing the packages all around.
I'm working on Windows 7, 64bit (thanks for the headsup Karel)
Go to add/remove programs in windows and uninstall everything. Then reinstall everything. Do your Python install first and PyCharm should detect everything. I'd suggest doing the 32 bit Python install as some packages aren't compiled for 64 bit and it makes things a bit challenging to find and install them.
Pip is standard in the python install now. You'll see a check box in the installer for it, which is pre-checked. I'd change your install directory to something simple like C:\Python27
Other than that it should be pretty straightforward. However I doubt you really need to uninstall Python. You can just go to settings and under the interpreter section add the C:\Python27 directory. PyCharm is constantly scanning for installed modules, so it will know in real time that you've installed them. You can even install packaged via PIP while PyCharm is open and within second PyCharm will recognize it as a valid package.
If you're missing packages you can also import them in PyCharm and when you get the red underline saying it's missing, hover your mouse over it and hit alt+enter and you'll get a menu to install it.