Issue: I am having issues with the environment and version of Python not matching the settings in VSCode, and causing issues with the packages I am trying to use in Jupyter notebooks. I am using a Windows 10 machine with Python 3.9.1 installed (including older versions), with Visual Studio Code 1.52.1 . Short summary - I install a package using pip. My guess is that it associates with the latest version of Python. I set up an interpreter in VS Code for that version of python, and try to import the package. The package is not found. If I call sys.version from the Jupyter notebook, I see that a default version of Python is running (3.8.5). The simple notebook throws an error because it cannot find that package that I installed with pip.
Screenshot that shows the associations:
This error is reproducible with only the
import pygmt
command in the notebook, i.e. it does not depend on the other packages imported.
Question: How can I control the version that Jupyter notebooks will run in VSCode if changing the interpreter doesn't work? Every other issue like this that I have found was due to not choosing the correct interpreter. In this case, I have. Why is it not running that version of the interpreter?
Thank you.
Edit
This edit is in response to #Jill Cheng's answer (see my comment below for tl;dr). I can change the interpreter (lower left corner of VSCode) to match the default that VSCode wants to run. No problem.
But there still are problems.
I cannot install pygmt, the targeted package, into this version of Python (see the command in in the terminal).
I cannot make VSCode operate in the opposite direction - i.e. I can never get it to run Python 3.9.1 even when that is the interpreter I chose.
I have reinstalled VSCode now several times, and Python 3.8.5 seems to always be the default. To me, the easiest solution seems to be changing the default Python version of VSCode - in fact, isn't that the point of selecting an interpreter? It is more nebulous to me why I cannot install pygmt into Python 3.8.5, and I don't know if it is beneficial to have multiple different versions of Python all with different packages (or is this just what Python users deal with daily?).
In VS Code, the Python kernel (Python environment) used by Jupyter notebook can be independent of the Python environment we selected in VS Code (shown in the lower left corner of VS Code).
As the output in the screenshot shows, the Python kernel of Jupyter you are using is "Python3.8.5", but the module "pygmt" is not installed in this environment. (Jupyter uses the last selected Python environment by default.)
Solution: Click the Python kernel on the upper right in the Jupyter notebook, and select the python environment where the module "pygmt" has been installed. In addition, it is recommended that you reopen the jupyter file after switching Jupyter's Python kernel so that it can reload the new python kernel.
Reference: Jupyter notebooks in VS Code.
Related
I have been a staunch user of Eclipse on Windows - mostly for developing Python code. Lately, I needed to do something with the packages xarray and netcdf4. I first used an old version of Eclipse, but when I encountered problems I installed the latest LiClipse - version 8.2.0 (64 bits) on my Windows 10 machine. I use Miniconda 3 py37_4.9.2 (64 bits) with Anaconda Navigator 2.1.2 to manage my Python environments. I wrote a script of only a few lines. When I tried to import package netcdf4, I got an import error immediately that one of the netcdf4 DLLs could not be found after pressing the debug button. When I tried working without direct involvement of netcdf4 but only with xarray, I also got an import error that my packages were not configured correctly. When I started the script - outside Eclipse - from the command-line, there was no problem at all. BTW, I tried running with different Python versions: 3.6, 3.8 and 3.9 but that made no difference. I suspect that Pydev does not work together well with the packages netcdf4 and xarray. Has anybody else experienced similar problems?
It seems like some environment variable isn't properly set when running from PyDev...
Do you have the flag to load conda environment variables set in the interpreter configuration?
i.e.:
Note: if it runs in the command line you can compare the values you have in os.environ from one to the other to find what may be different (in general just making sure that the conda environment variables are loaded should do the trick, but if it doesn't comparing those and setting what's needed in the Environment tab may help).
Imagine the situation like below. There must be simple explanation.
I have a program written in Jupyter notebook and use Python 3.7. There is no errors while executing the program and works on two PC computers. As I remember there where just only Python 3.7 (and adds like matplotlib etc. all what is needed).
Then on the others computers where is Python 3.8 the same programs gives me an error. Why?
I took another two PC removed all Python installation and install 3.7 3.8 3.9 3.10 Python (tried every of them) and I can't execute the programs without error. Why is like that? All extensions like matplotlib, numpy , scipy which is needed every time are install in proper way by pip etc...Path in Windows are always correct.
It seems to me that on those two computer where program work fine was only Python 3.7 (but I am not sure in 100% that there hadn't been installed previous version of the Python) the programs works. Any other installation or refreshment of the Python makes that teh program gives me errors.
The only one what I should to do is 100% reset Windows and do fresh and the newest one Python instillation (Jupyther etc.) and check whether the error exist or not. But I would like to know what makes the problem and not to refresh all my Windows installation every time.
The problem is even more interesting when I am taking the same program
and put them to the virtual Jupyther using the Internet and browser they always work fine without any errors and there is for sure the news Python engine I guess not 3.7.
Did you try to execute "pip freeze" or "conda list" on these computers (with Ananconda Powershell for ex.) to verify that all the libraries' versions you are using are the same?
Looking at the error on the screenshot, I would bet that the problem does not come from your Python version but more from a higher version on matplotlib installed on the first two PCs, which makes that the third one (with potentially an older version of matplotlib) does not support the arguments given.
Just try executing:
(base) PS C:\XXX\Python> conda list matplotlib
and should get the version of matplotlib installed on each PC, like this:
# packages in environment at C:\XXX\Miniconda3:
#
# Name Version Build Channel
matplotlib 3.4.2 pypi_0 pypi
matplotlib-inline 0.1.2 pypi_0 pypi
If they are different, this might be where the issue comes from.
I have downloaded Pandas library with pip install pandas through the command prompt, when I try to import pandas as pd PyCharm returns an error : ModuleNotFoundError: No module named 'pandas'
I have tried to uninstall and install again many times but nothing seems to work. Does anybody know a solution to this?
You can try downloading the library from PyCharm settings:
File -> Settings
then, Project: -> Python Interpreter
Click a + sign to the right,
Search for the pandas library,
and finally, press 'Install Package'
I think you have to choose the right python interpreter. Check my screenshot
You likely have multiple copies of Python installed on your system. PyCharm can be configured to use any version of Python on your system, including any virtual environments you've defined. The solution is to match up the version of Python you've installed Pandas into with the version of Python that PyCharm is using to run your code.
There are two places where you specify a Python version. First of all, your Project has a version associated with it. Check the "Python Interpreter" section of the "Project" section of your Preferences for that. That version is used for syntax highlighting, code completion, etc.
By default, the abovementioned Python version will also be used to run your code. But you can change the version of Python that your code is run with by creating or modifying a Run Configuration. To do this, check the menu next to the Run and Debug toolbar buttons near the top-left of your PyCharm window.
When you do get into the Python Interpreter section of the Preferences, you'll find that you can see all of the modules installed for each Python version that PyCharm knows about. You can use this to check to see if Pandas is installed for a particular Python version.
I would suggest you get comfortable with all that I've said above. It will save you many headaches in the future.
I'm trying to add Keras module into PyCharm. Keras is installed into /usr/local/lib/python2.7/site-packages/Keras-1.0.8-py2.7.egg.
PyCharm interpreter settings looks like that:
Last two paths are just attempts to make it work.
This is definitely PyCharm configuration problem because keras is imported from interpreter without any problems.
Check that the version of python you are utilizing is matched with the python version the Keras uses.
Check your default python version by running this command in your cmd or terminal: python --version
Check your python version in PyCharm Interpreter by File > Settings > Project: ... > Project Interpreter.
Match the version you utilize in PyCharm with the version you utilize in keras.
In your system terminal, run command which python, it will give you the interpreter you are using in your system. it should be the same one you are using in pycharm.
I'm designing some software that is specifically going to be used by people running Python 2.7 instead of 3. Unfortunately, I'm using a computer that has 3, and apparently there are some dependency issues when some of my colleagues are using Python 2.7 to run code. I'm hoping to run with 2.7 in the ipython notebook to fix my problem. Do I need to install a new version, and if so which one?
You can choose either using the Kernel tab --> change kernel and select python version, or when you open a new notebook under the file tab you can choose python version there.