How to colorise the Python repl - python

As we can see the difference between the images. In vim datatype and function are of different color. How can i bring color to my python repl.
I want to do it by myself. Please suggest me, how should I do this. Whether I should have to do something with stdout stream object or I have to change something in python interpreter and then build it from source (ohh I have to compile the python from source only to bring this change)
Sorry if there is some error in my question. Hopes you all will understand me.

You should probably have searched a little bit more first, because there are many answers here about this. Perhaps the best ones are:
bpython [NOT on Windows without hack.]
ipython
Change Python interactive prompt ">>>"
Some characters stick to my colorized prompt in Python cmd
Adding colors in python interpreter prompt, does not wrap properly
How to fix column calculation in Python readline if using color prompt

Related

Open Python interactive session and editor

I am looking for some pieces of advices in order to accomplish a tiny task regarding Python. If someone would ask to provide a pic of a 'started interactive session of Python with your favorite editor with a Python script', what would you show to this person? Should it be a void script? How do you interpreter 'started interactive session'? How about your own favorite editor (I mean that you would suggest for Windows 10)?
Sorry for the triviality of my question,but I have just started with beginners' Python course
Just to make sure I am on the right way, if I have to submit to someone else a started interactive session and your favourite editor with a Python script, will be it sufficient to show the following windows as in the picture?
If you want to do interactive things, you probably just want to use jupyter notebook: https://jupyter.org/install#jupyter-notebook
You can always just type python at your terminal prompt if you have python installed, this will start an interactive session in your terminal, but jupyter is definitely easier to use once you get it set up.
Edit: regarding favourite editor, this is very much opinionated but I love sublime text. https://www.sublimetext.com/
Note that you probably would use one or the other: you would use a text editor to write scripts that could be run in a terminal, for example
# hello_world.py
print("Hello, world!")
then in your terminal
python hello_world.py
whilst you would use a jupyter notebook for example for quick experimentation or for demonstrating usage of your software to others.
In terms of interactivity, you should really check out Jupyter Notebooks. It's industry standard for a lot of tasks, widely used and with great performance and support. Also, Jupyter has an in-built code editor than can be run via localhost in your browser.
However, for a code editor, I will never stop recommending VSCode. Huge game changer, light-weighted and with support for pretty much any language. Jupyter notebooks can be run directly from VSCode, and the latest features that Microsoft introduced in this regard make using Jupyter inside VSCode really easy and intuitive. Also, extensions are a positive point as well.
If you're only planning on programming with Python, maybe checking out PyCharm is also a good idea, but I have working mainly in Python for the past 3 years and I have never missed anything on VSCode, even though I've tried PyCharm several times.
The simplest answer: go with the IDLE IDE, it comes bundled with Python by default. It starts with an interactive session, so you just type at the prompt (>>>):
print ("Hello, world!")
and your task is done, if it prints out your "Hello, world!" line.
In general, you start your python interactive session (python REPL, python terminal, python console, there are several terms for the same thing) and, since it's an interpreted language, everything you type in, Python will interpret and execute, if valid. Nevertheless, except for atomic examples, you'll want to use a code editor or IDE.
As a beginner, I'd avoid jupyter for the time being, it could get cumbersome. Stick with the default IDLE editor (you open a new file and type away) until you feel confident, then make the switch toward some editor or IDE that you fancy - Visual Studio Code is popular and has python debugger, vim is old as Bible, can run on a calculator, but it's a modal editor, best used with touch typing, Notepad++ is also good for coding...

How to find out who called me

When using an IDE like PyCharm python is being called via pydevd.py (is parent?).
On command prompt like cmd.exe scripts can use the prompt color sequences e.g. "\033[0m".
Well, withing PyCharm it looks strange when using those.
The question is: if you can find out who called the script, if running on cmd line or not to be able to use those sequences properly.
Do you mean witch python interpreter called it? You can set it in Pycharm(and other IDEs) and often the ones running in command prompt are differently set in IDE.Would suggest getting it straight and choosing the right one in Interpreter settings.
This doesn't answer your question directly, but I believe it may answer the problem you're trying to solve.
I've had success using the colorama package in the past, which handles the details of how to set colours for you in a cross-platform way. This also appears to work correctly within PyCharm.
For example:
from colorama import init, Fore
init(autoreset=True)
print(Fore.RED + 'Hello world, but in red')

Can't figure out how to use 2to3 Python converter

I've seen other answers for this on Stack, but they aren't helping.
I have a Python 2.7 script that I need converted to work with Python 3. It seems like using 2to3 shouldn't be this difficult, but I can't figure it out. In the Windows command line, I try typing "python C:\Python27\Tools\Scripts\2to3.py (path to my file)" and it says "Invalid syntax" and has a little carrot pointing to the C in the first file path.
I also try opening the IDLE shell and typing "$ 2to3 (path to my script)", which also says "Invalid syntax" and highlights the $. So I get rid of the $ and try again, and then it highlights the "2to3" with Invalid Syntax.
I've seen other answers as well, and none come even close to working. I feel like an idiot for not getting this. What am I missing? Can someone explain it like I'm five?
(I'm fairly new to programming, but I've been doing pretty well writing scripts in Python. I have no idea why THIS is the thing I'm struggling with.)
Does this work for you?
python C:\Python27\Tools\scripts\2to3.py -w <your_file>.py
This will make the necessary changes to make you <your_file>.py to make it compatible with python3 and overwrite the original file
See 2to3 documentation

How to use Python3.2 with Dreampie? [UBUNTU]

I have a question.
I try to learn Python and of all programmer's tools I found Dreampie the most suitable. But, my dreampie always automatically sets Python 2.7. I found this topic and following the instructions, I changed both subp_main.py and subp_main.pyc (based on the first). Yet, it's still Python 2.7.
Telling truth, I suppose there should be some way to choose between the two versions, but I did not found anything in the Net or in program options. According to this topic writing "dreampie python3" inside the program should work, but dreampie inform me that the syntax is invalid (?).
[I quess the solution is very easy, but none of searchable things would help...]
You need to open a Terminal window (or Run dialog Alt+F2) and type in the command as follows and press Enter:
dreampie python3
It's not python syntax but shell syntax so you are running the dreampie application with python3 as argument.

Vim for python and R scripting (in Screen or not)

I've been trying to find this information online but I'm not getting the answer.
I've used RStudio and Geany for editing files before. Now I'm trying to use ViM to edit python and R files (I know there's RPy, but nothing to do with my problem).
I would like to know how can I have 3 terminals (could also be vim buffers, or screen windows) with one running ViM and the others running R and Python. When I execute a Python script, the terminal (window or buffer) with python shows the output. The same when I run R scripts.
I would appreciate insight on this as this is something that's keeping me from using ViM regularly. I would also consider a solution with terminator terminal multiplexer or guake terminal. Any information about sending code for scripting from one instance to another is welcome.
Are you looking for a way to have a REPL inside Vim? If so, Vim wasn't really designed with that in mind, though there are some plugins that try. Conque is an example.
Some things I use to have a quicker code/run/test iteration with Python:
IPython's %edit feature, which starts editing a script with $EDITOR and will run the script after you exit.
vim-ipython which can send/execute/recieve code via an IPython interpreter.
tmux which allows you to have multiple shells side by side, but with little interaction between them.
Vim-slime is a general-purpose solution to this I'm pretty happy about, it will send blocks of code to any tmux pane, meaning it works for any language.
https://github.com/jpalardy/vim-slime
Your requirements for online information may not have been spelled out in enough detail, since I seem to find a wealth of information on using ViM as an IDE for both R and Python:
R:
http://www.r-bloggers.com/r-with-vim/
http://www.vim.org/scripts/script.php?script_id=2628
http://www.vim.org/scripts/script.php?script_id=1048
Python:
http://wiki.python.org/moin/Vim
http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/
Have a look at vim-ipython, a plug-in for Vim.
You need to download the source (linked above), and run the Vim command :source path/to/file/ipy.vim.
Start by running a new IPython session (e.g. using IPython qtconsole or IPython notebook) and then type :IPython into Vim. Your Vim is now connected to the IPython instance you just opened.
You can press F5 to run the whole python script in your Vim, or Ctrl+s to run the current line. Ctrl+s will also run whatever is selected if you're in visual (i.e. 'select') mode.

Categories