Ipython change interpreter? - python

How does one change Python interpreter used by Ipython? Ipython is great environment for use in Python scripting, with it's powerfull autocomplete and other neat features. However under Archlinux python defaults to python 3. How it is possible to change Ipython to use /usr/bin/python2 as interpreter for Ipython. I don't need pernament solution that would affect other programs, I want to use python2 interpreter on demand: like being able to do one session of Ipython in python2 second one in python3. Is there proper way to do it.
Ipython website states, that it is able to work with even jython and other version of python, but it seems, that it does not state how to change interpreter on which ipython runs. At least I couldn't find anything. Any ideas?

Usually distrib ship with ipython for default system Python ipython3 for Python 3 and so I suppose ipython2 for Python2 when ipython default to Python3.
If you want something quick for python2, clone source, cd into it, then.
$python2 ipython.py --Usual.options

Related

Can I ask a fundamental question about PyCharm base interpreter?

I am trying to use Python3 with PyCharm.
So I installed Python(3.10.1) and PyCharm(2021.3.1)
But when I try to create a new project, PyCharm want me to choose 'base interpreter'
and I don't know which one to choose.
Here is the list of base interpreter:
I want to know what is difference between /usr/bin/python3 and Library/Frameworks/Python.framework/Versions/3.10/bin/python3
Usually the files under /usr/bin/ are symlinks to actual files, which means they are not real files but links to the actual file.
To see it for yourself go to the /usr/bin/ directory with a terminal and execute:
ls -a
I suppose macOS already ships with python and by installing Python 3.10 you added that last element to the list, which is also linked by /bin/usr/python3.
I'd go with /usr/bin/python3, more portable.
You can choose the one interpreter for the version of python you want. In your terminal, you can run python --version or python3 --version and it will display the version of python, Like this:
$ python --version
Python 2.7.16
$ python3 --version
Python 3.9.1
Performing which python, will give you the path to that python binary, like the following (although, this is where we are leaving "Python" and talking more about the OS and PATH.
$ which python3
/usr/local/bin/python3
For the other versions of Python you have installed, you can check their version by doing something like:
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 --version
You'll find that you may choose different versions of python for different projects. In the future, you may consider using venv to setup virtual python environments per project.
If you're just getting started with Python, I'd suggest sticking with the version you installed 3.10.1, and choosing that interpreter.
The base interpreter is the python.exe file that will run everything you do inside that project.
I would suggest you use Anaconda as a package manager and then create a virtual environment with the version of Python you need. Just follow these steps:
Getting started with Anaconda: https://docs.anaconda.com/anaconda/user-guide/tasks/pycharm/
Choosing an Anaconda virtual environment with Pycharm:
https://docs.anaconda.com/anaconda/user-guide/tasks/pycharm/
A package manager like Anaconda is very useful when working with Python.
Otherwise:
In your list you have many different versions of python. You should choose the base interpreter based on the python version you want to use.
Always select the latest version.
Since I use Python 3.9, it will be:
/usr/local/bin/python3.9
Based on your image, it would be:
/usr/local/bin/python3
The interpreter is what version your PyCharm project uses. It appears you have python2 and python3, so if you were writing in python2, you would use the python2 interpreter.
I would select the latest version, which in your case would be:
/usr/local/bin/python3

How to change which Python is used when opening platformio-ide-terminal in Atom on macOS

I am trying to use the "platformio-ide-terminal" package in Atom, on macOS, to open terminals within Atom.
The issue is, the terminal in Atom does not use my Anaconda Python, and thus does not have access to packages I installed using Anaconda. Typing
which python
returns
/usr/bin/python
in platformio-ide-terminal and
/Users/.../anaconda3/bin/python
in the terminal.
Typing
which python3
in platformio-ide-terminal returns
/Users/.../anaconda3/bin/python3
I am very troubled about all these different Pythons coexisting on my computer, and a general explanation about how this works would also be very welcome.
I would also like to use the same version of Python in Atom. How can I do this?
I solved it by using IDEs rather than text editors. Pycharm offers a lot of clarity on the version of Python you are using: you can choose the exact directory with the version of Python you want to use (by setting an interpreter), and then run / debug the project from Pycharm using this version of Python. Then, I deleted other directories with unused versions of Python to save the space.

Will installing Anaconda3 change Mac OS X default Python version to 3.4?

I'm planning to install Anaconda3 for Python 3.4. Since by default, Mac OS X uses Python2, will install Anaconda3 change the default Python version for the system? I don't want that to happen since Python3 can break backwards compatibility. If it does change the default Python version, how can I avoid that?
Apple has a built-in system for managing multiple versions of software, and switching between them. But you don't even need to worry about that, because Anaconda installations are self-contained: Everything lives under the top Anaconda installation directory (probably /Applications/anaconda). The only effect outside this directory is that during installation, Anaconda will offer to modify the PATH variable in your .bashrc. If you agree, it will add one line at the end of your .bashrc, something like this:
PATH="/Applications/anaconda/bin:$PATH"
As you can see, Anaconda puts itself first in the system path. This means that typing python at the shell prompt will launch python 3, which may not be what you want. I run Anaconda 3.4 like this and have had absolutely no problems with my system, but I did need to modify my own executable python2 scripts that launched python like this:
#!/usr/bin/env python
This is a nice way to find python wherever it is, but in this case it will find python 3-- oops! Changing the above to #!/usr/bin/python or to #!/usr/bin/env python2 ensures that they continue to work correctly. In my experience this was not necessary with any of the system's own scripts; everything is already set up to find the right python.
Alternative 1: You could decline the PATH modification, and use Anaconda via the launcher. In that case there is no change to the rest of your execution environment. The launcher will start a special bash prompt with the anaconda environment activated, but execution in normal shells is completely unaffected. If you will continue to program a lot in python 2, this may be for you.
Alternative 2: A minimal-impact alternative is to put the anaconda directory last in your path:
PATH="$PATH:/Applications/anaconda/bin"
This ensures that non-anaconda binaries take precedence over anaconda, so python will start good old /usr/bin/python (that is, python 2). You can start anaconda's variant by typing python3, idle3, etc. I did not have IPython before I installed anaconda, so typing ipython finds the anaconda version.
No it won't, you can have multiple python installs, once you don't remove your system python or manually change the default you will be fine.

make python script use 2.7.8 instead of 2.6

I have 2 versions of python installed on my server.
Python 2.6 in /usr/bin/python
Python 2.7.8 in /usr/src
Python 2.7.8 install guide
How do I make python scripts use 2.7.8? instead of 2.6?
how do I set up an alias?
Thanks
Write:
#!<path to your python interpreter>
as the first line of the script your run. Then just execute it.
Other option:
<path to your python interpreter> your_script.py
There are multiple ways of doing this. First, I would suggest you check your $PATH environment variable to see if it holds the path to both of your python versions. If it doesn't, you can either add the path or simply make a symlink to it from /usr/bin/ and call it python2.7, for example.
Then you can use #Pavel's suggestion of specifying the path to the interpreter in the shebang of the file.
If you followed my previous instructions, your shebang should look something similar to the following.
#!/usr/bin/env python2.7
You could also use the second way #Pavel showed you, by simply calling the interpreter first in the terminal and pointing it to the script.
/usr/bin/python2.7 my_script.py
The best option you have, which I recommend, is using virtualenv as suggested before, but only if you want it for specific scripts. If you want it globally, I would suggest you read further.
Finally, if you want all your scripts to run under python 2.7 then I suggest you remove python 2.6 and install the 2.7 version from the package manager, the right way, and save yourself a lot of hassle.

What is the difference between Python and IPython?

What exactly is the difference between Python and IPython?
If I write code in Python, will it run in IPython as is or does it need to be modified?
I know IPython is supposed to be an interactive shell for Python, but is that all? Or is there a language called IPython? If I write something under IPython, will it run in Python, and vice-versa? If there are differences, how do I know what they are? Will all packages used by Python work as is in IPython?
ipython is an interactive shell built with python.
From the project website:
IPython provides a rich toolkit to help you make the most out of using Python, with:
Powerful Python shells (terminal and Qt-based).
A web-based notebook with the same core features but support for code, text, mathematical expressions, inline plots and other rich media.
Support for interactive data visualization and use of GUI toolkits.
Flexible, embeddable interpreters to load into your own projects.
Easy to use, high performance tools for parallel computing.
Note that the first 2 lines tell you it helps you make the most of using Python. Thus, you don't need to alter your code, the IPython shell runs your python code just like the normal python shell does, only with more features.
I recommend reading the IPython tutorial to get a sense of what features you gain when using IPython.
IPython is a powerful interactive Python interpreter that is more interactive comparing to the standard interpreter.
To get the standard Python interpreter you type python and you will get the >>> prompt from where you can work.
To get IPython interpreter, you need to install it first. pip install ipython.
You type ipython and you get In [1]: as a prompt and you get In [2]: for the next command. You can call history to check the list of previous commands, and write %recall 1 to recall the command.
Even you are in Python you can run shell commands directly like !ping www.google.com.
Looks like a command line Jupiter notebook if you used that before.
You can use [Tab] to autocomplete as shown in the image.
IPython is basically the "recommended" Python shell, which provides extra features. There is no language called IPython.
Even after viewing this thread, I had thought that ipython was a synonym for the python shell, in other words that typing python at the command line put one into ipython mode.
It is in fact, as referenced above, a very cool interactive shell (command line program) that can be installed from iPython.org or simply by running
pip install ipython
or the more extensive:
pip install ipython[notebook]
from the command line.
There are few differences between Python and IPython but they are only the interpretation of few syntax like the few mentioned by #Ryan Chase but deep inside the true flavor of Python is maintained even in the Ipython.
The best part of the IPython is the IPython notebook. You can put all your work into a notebook like script, image files, etc. But with base Python, you can only make the script in a file and execute it.
At start, you need to understand that the IPython is developed with the intention of supporting rich media and Python script in a single integrated container.
Compared to Python, IPython (created by Fernando Perez in 2001) can do every thing what python can do. Ipython provides even extra features like tab-completion, testing, debugging, system calls and many other features. You can think IPython as a powerful interface to the Python language.
You can install Ipython using pip - pip install ipython
You can run Ipython by typing ipython in your terminal window.
From my experience I've found that some commands which run in IPython do not run in base Python. For example, pwd and ls don't work alone in base Python. However they will work if prefaced with a % such as: %pwd and %ls.
Also, in IPython, you can run the cd command like: cd C:\Users\... This doesn't seem to work in base python, even when prefaced with a % however.

Categories