This question already has answers here:
How do I script a "yes" response for installing programs?
(6 answers)
Closed 5 years ago.
I've come across a program run by Python2, which asks many questions of yes/no type.
I want to answer "yes" to all of them, but it's really difficult because there are literally hundreds of them (it's basically a parser of code, which asks about every found variable).
So, is there any possibility, how to force Python answer "yes" automatically?
I think about something similar to apt-get -y install. And I'm interested in answers about both Python2 and Python3.
I'm using Lubuntu 16.10 and my default Shell is GNU bash, version 4.3.48.
Try using yes:
yes | python ./script.py
If you have a more complex state to manage during interaction, there is also expect.
yes emits y by default, but you can customize it by providing an argument (e.g. yes yes), thanks #tobias_k. If you need a portable way (in Python), go with the suggestion from Jean-François Fabre (or just hack the script).
Related
This question already has answers here:
How can I run a Python project on another computer without installing anything on it?
(6 answers)
Closed 9 months ago.
Problem:
I need to send someone (who has little to no computer knowledge) a python program, but I don't even know if he has python installed let alone all the dependencies.
Question:
Assuming he doesn't have python, how should I go about sending the application so that It is as straightforward as possible for him?
What I've tried:
Initially, I thought of using venv and sending him the whole thing, but there has got to be a better solution, as my code only uses two of the built-in libraries.
In my research, I came across Docker, but I think he would need to have that installed. I also saw pipenv but it wouldn't work without python on his PC.
You have multiple ways to do so:
Create an executable file, which is quite a long process but can be useful is you project contains a lot of files and multiple dependencies
Send them the python code (I recommend it if your project fits in one .py file). The person will need to install python and possibly a few pip libraires, but it's not extremely complicated in my opinion, with clear instructions.
Finally, you can go on repl.it and create a repl, which is simply a way to execute code on your browser. I think this is the best option for both large and small projects, except if it contains a lot of odd dependencies, and I'm not sure if repl.it supports graphical interfaces either. Anyway, you should take a look, it might be perfectly fit your needs
This question already has an answer here:
How to make programs like nano/pico in linux
(1 answer)
Closed 1 year ago.
I would like to write a python app that manages the ssh screen in a fashion similar to the nano editor on a RPI. Accept input and place output data at fixed locations of the ssh window. Does someone know how this is done?
They draw their interface using ANSI sequences. Python has a built in module to this called curses, which is a binding for the ncurses library.
You can check out the documentation and the tutorial.
If you are willing to use libraries, there are higher level ones like urwid.
This question already has answers here:
How to drop into REPL (Read, Eval, Print, Loop) from Python code
(7 answers)
Closed 6 years ago.
How would you make the IDLE shell, using nothing but Python code? Now, I understand the beggingins, like a simple exec(raw_input('>> ')), but how would you get if and else statments, or for and while loops to work?
while 1==1:
is considered invalid syntax. How would you prevent that?
The suggested thread isn't nearly the same thing, as all of the answers but one answer how to run a python program from a python program. One of the answers points kinda of like what I'm asking, but it would still fail in an if and else statement, or a while or for loop.
The source code for IDLE is in the Python standard library, under the idlelib package. You can look at the source to see how they implement everything.
idlelib is a lot of code, so it might be overwhelming to try to go through it. The code module provides a Python implementation of Python's interactive mode; you could go through the source code for that to get an idea of how you could do things.
This question already has answers here:
Emacs python autocompletion
(3 answers)
Closed 9 years ago.
i'm new to emacs, and I want to use it for programing in python. Most important thing for me is autocomplete. I want a step by step tutorial for add a python autocomplete plug-in for emacs 24.
sorry for my bad English.
This Python-focused Emacs resource discusses installing and configuring auto-complete and other useful configuration for Python work in Emacs:
http://www.jesshamrick.com/2012/09/18/emacs-as-a-python-ide/
You will find all information here:
http://www.emacswiki.org/emacs/?action=browse;oldid=PythonMode;id=PythonProgrammingInEmacs
This question already has answers here:
How are you planning on handling the migration to Python 3?
(7 answers)
Closed 9 years ago.
So with the final releases of Python 3.0 (and now 3.1), a lot of people are facing the worry of how to upgrade without losing half their codebase due to backwards incompatibility.
What are people's best tips for avoiding the many pitfalls that will almost-inevitably result from switching to the next-generation of python?
Probably a good place to start is "use 2to3 to convert your python 2.x code to 3.x" :-)
First, this question is very similar to How are you planning on handling the migration to Python 3?. Check the answers there.
There is also a section in the Python Wiki about porting applications to Python 3.x
The Release Notes for python 3.0 contains a section about porting. I'm quoting the tips there:
(Prerequisite:) Start with excellent test coverage.
Port to Python 2.6. This should be no more work than the average por
from Python 2.x to Python 2.(x+1).
Make sure all your tests pass.
(Still using 2.6:) Turn on the -3 command line switch. This enables warnings about features that will be
removed (or change) in 3.0. Run your
test suite again, and fix code that
you get warnings about until there are
no warnings left, and all your tests
still pass.
Run the 2to3 source-to-source translator over your source code tree.
(See 2to3 - Automated Python 2 to 3
code translation for more on this
tool.) Run the result of the
translation under Python 3.0. Manually
fix up any remaining issues, fixing
problems until all tests pass again.
It is not recommended to try to write
source code that runs unchanged under
both Python 2.6 and 3.0; you’d have to
use a very contorted coding style,
e.g. avoiding print statements,
metaclasses, and much more. If you are
maintaining a library that needs to
support both Python 2.6 and Python
3.0, the best approach is to modify step 3 above by editing the 2.6
version of the source code and running
the 2to3 translator again, rather than
editing the 3.0 version of the source
code.
I write a free book about this. You can read it here:
http://python3porting.com/
In short:
Make sure all your third party libraries are available for Python 3.
Prepare your code by removing common ambiguities:
Use // if you really want integer division.
Make sure you flag binary files with the 'b' flag when you open them, to clearly
indicate if you mean the data to be binary or not.
The higher your test coverage is, the better.
Make sure it runs without warnings under "Python 2.7 -3".
Now run 2to3.
Fix any bugs.
That's it, more or less.
Without a really compelling reason to upgrade, I would stick with what works. I looked at upgrading the scripts I use daily and it was too much work for no benefit that I could see.
"If it ain't broke, don't fix it!"