Run iPython notebook with a specific version (2.7 instead of 3) - python

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.

Related

Problems with importing netcdf4 and xarray when debugging - in eclipse + pydev on windows

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).

The same program written in Jupyter notebook with Pyhon 3.7 doesn't work somewhere else

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.

VSCode issue with Python versions and environments from Jupyter Notebooks

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.

Cannot get python IDLE 2.7, 3.3 to run - OS X

I am running a MacBook Pro with 8gb of memory and a 2.5GHz processor. I run OS X Mavericks, but this problem has happened on other operating systems as well. when I try to open the editor, or even open a python file, this happens:
This has been a problem since I got into python. I have tried as many versions as I can, and the same screen pops up. Please help
I recently also upgraded a friends iMac to OS X 10.9 Mavericks and her Python to 2.7.6. If you mount the python-2.7.6-macosx10.6.dmg disk image there's a text file named ReadMe.txt. If you ahem, actually read it, right near the beginning it says:
**** IMPORTANT ****
Update your version of Tcl/Tk to use IDLE or other Tk applications
To use IDLE or other programs that use the Tkinter graphical user
interface toolkit, you may need to install a newer third-party version
of the Tcl/Tk frameworks. Visit
http://www.python.org/download/mac/tcltk/ for current information
about supported and recommended versions of Tcl/Tk for this version of
Python and of Mac OS X.
Here's what http://www.python.org/download/mac/tcltk currently looks like (my highlighting):
To summarize: You need to download and install ActiveTcl 8.5.15.1 (which is mislabeled ActiveTcl 8.5.15.0) from ActiveState's webpage. After doing that you should be able to run IDLE:
I haven't actually tried to do it yet, but from what I read it sounds like the same process applies to Python 3.3.3 and its IDLE app.
I was having the exact same problem.
I was finally able to install Python (and IDLE) via MacPorts by also installing a package called "pyXX-tkinter" -- where XX is the version, e.g. py34-tkinter for Python 3.4.

installing IPython with two versions of Python (Windows)

Is it possible to have IPython (0.12) installed on the same
system with two different versions of Python (v 2.7 and 3.2)? Currently I have both versions of Python running happily on my system.
I am using two machines, one with Windows 7 64-bit, the other XP SP2 32-bit, and both have Python 2.7.2 and 3.2 installed. I have been using IPython with Python 2.7 w/o any problems for a while on both.
Now I would like to have 3.2 available too with IPython, but when I try to run the binary windows IPython installer I get this error toward the end of the install:
*** run_installscript: internal error 0xFFFFFFFF ***
and no shortcuts/entries into Start Menu etc are created.
(I ran the install as adminstrator under Win 7) I am running into the same problem with XP and Win 7.
After poking around the net and SO for a while I saw references for
getting setuptools. Unfortunately, there's no version for Python 3.x
but it was suggested to use distribute.py instead which I ran and
installed. I am not sure how to proceed next. And I'm not even sure it
is possible to do this (though I think it is :)
Any suggestions?
UPDATE: There was a problem with import readline not working with Python 3.2.3, but I was able to fix it by downloading pyreadline-2.0-dev1.win32.exe which works with Python 3.2.3 .. the above problem remains unfortunately.
Ok, here's my solution to get IPython 0.12.1 to work with both Python ver
2.7.x and 3.2.x on the same system (ie. under Windows 7 64 bit and XP SP2 32 bit). It's not
perfect, but works, and hopefully will help others. Where I am showing directories they refer to XP (but should be adjusted accordingly for Windows 7)
Steps for both:
Install both versions of Python via the binary windows installer provided at
Python.org
Install this version of pyreadline pyreadline-2.0-dev1.win32.exe which works
for both versions of Python (I could not get the widely available version 1.7 to work with Python 3.2.x)
I am not sure if distribute.py is necessary, I'd skip it on the
first try.
Download ipython-0.12.1.zip file and extract it. Place it under your Programs directory and cd into it (I'll call this the ipython_dir) [I tried the binary installer numerous times without success]
For Python 3.2 (easier)
In the ipython_dir:
c:\python32\pyton.exe setup.py install
will create ipython3.exe in c:\python32\Scripts that can be used to
fire up iPython
For Python 2.7 (a bit odd)
The above method failed. However, it is possible to start IPython from the ipython_dir with
c:\python27\python.exe ipython.py
I can't explain why this is so, but since I use Console2 I can hide
these two ways of starting IPython.
Summary:
To start IPython with v 2.7.x of Python:
c:\python27\python.exe "C:\Program Files..\ipython_dir\ipython.py"
To start IPython with v 3.2.x of Python:
c:\python32\Scripts\python3.exe
That was a lot of work and took most of the day, so I hope posting the steps above
will save someone else a lot of time (and help me remember what I have to do next time I struggle with this)
Thanks everyone for their suggestions.
Addendum: None of the above resulted in shortcuts or entries in the Start/Program Menus. Shortcuts can be created manually, or in my case aren't necessary since I'm using a special shell (Console2) to run IPython.
The first question in the iPython FAQ titled "Running IPython against multiple versions of Python" may help.

Categories

Resources