Does a "terminal" always need to run with python - python

Hi im a bit new to python but i want to learn more my question is when you build a web application and you are going to use python to do the data handling and calculations does this mean in order for that to be used a terminal,in this case lets say on windows will have to run and that basically listens if and when something was triggered or executed on the python program/script

This article summarizes how different types of Python run. By default, on many systems, CPython is present. When it interprets py files initially, it may create pyc files, which can then run on the CPython virtual machine, described here. The virtual machine and interpreter are also running when you run python on your terminal, if you are using this. As described in the first article however, CPython isn't the only way to run Python.

Related

How to intercept running python script and execute command within its context?

Here is the situation (as an example) - I ran a ML learning python script (which I wrote) for a long time but I didn't add functionality to save its weights.
My question in this situation is if it's possible to somehow intercept the running python interpreter and execute a command within the program context.
For example since I have model_seq global variable inside the running program - I would like to execute:
model_seq.save_weights("./model_weights")
Inside the process.
I've heard this is somewhat possible with gdb.
(Personally I know this can be done for sure with a C program and gdb - but since Python is compiled and interpreted the steps are a bit unclear for me (and I'm not sure if I would actually need a special python3 build or the default ubuntu one I have running will work))

change python interpreter during runtime

there are two python environment 3.6 and 3.8 in my program.I can not unified the environment because there are different sdk come from different vendor.
what can I do call the two sdk at the same time.
I have try to change the environment by source different shell scripts.it can not work.
I didn't come up with a nested way to change interpreter during runtime, before and after Googling. A naive way could be splitting your program into two parts, with each part requiring only one interpreter.
Then you can write a bash script to run these two parts sequentially or in whatever way you wish.
A possible demo could be:
#!/bin/bash
<path-to-1st-python-env> part1.py
<path-to-2nd-python-env> part2.py

How to create an executable on Mac to run python scripts (bash help)

Sorry if this is an easy to answer question, but I have been stressing myself out all day over this simple problem. I have never used a Macbook before, and am unfamiliar with the inner-workings. I wrote a series of six python scripts that are meant to be run in series, and have easily been accomplishing this with a batch files on my PC. However, I have been developing this program for a Mac user, and have no clue how to accomplish the same thing.
I have successfully managed to get python installed as well as all of the necessary packages, and the scripts can be run one-by-one, so the infrastructure is there.
On windows, I have been accomplishing this with the following batch script:
#echo off
python outputnotion.py
python addData.py
python listAppender.py
python inputgsheets.py
ECHO Timing out for 30 seconds to allow Google Sheets to compute values
timeout /t 30 /nobreak
python outputgsheets.py
python inputnotion.py
pause
I have no idea how to replicate this on mac, or if it's even possible. The person who will be using this code is not as familiar with python or running the scripts, so the simpler the solution the better.
Thank you so much, as I have been scratching my head all day over this seemingly simple issue.
Have you heard of the makefile? It works on UNIX-based systems, such as the MacOS. I use them mostly to store snippets of bash scripts I use frequently.
Make a file named Makefile
Within Makefile, write something like this:
scripts:
python outputnotion.py;
python addData.py;
python listAppender.py;
python inputgsheets.py;
## This is a comment
## Sleep for 30 seconds
sleep 30;
#echo Timing out for 30 seconds to allow Google Sheets to compute values;
python outputgsheets.py;
python inputnotion.py;
## Invoke a pause
read -p "Press [Enter] key to continue...";
Now, using your bash terminal in the directory where Makefile resides, type the following to execute your scripts:
$ make scripts
A shell script just runs builtin commands and external programs. You certainly can write a string of invocations to python scripts, not just compiled programs. The shell is really a full programming language, with variables, control structures and all. A bit quirky, but I've seen largeish scripts doing complex tasks (not that I'd recommend doing so, there are better tools).

How can I make my program utilize tab completion?

I've noticed that some programs (e.g. hg) allow the user to tab-complete specific parts of the command. For example, if, in an hg repository working directory, I type:
hg qpush --move b8<TAB>
It will try to complete the command with any mercurial patches in my patch queue that start with "b8".
What I'd like to do is imitate this behavior in my program. That is, I have a series of commands that depend on files within a certain directory, and I'd like to be able to provide tab completion in the shell. Is there an API for providing this on Ubuntu Linux (preferably using python, as that's what my script is written in)?
To do this, you need to write tab-completion modules for your shell. The default shell in most Linux distributions is bash, so you should write a completion script (typically a shell script). Once you've written your script, add it to /etc/bash_completion.d/. This should be distributed with your program (for Linux distributions, included in the package).
Debian Administration has a guide for writing your completion scripts. For using completion on a Mac, see https://trac.macports.org/wiki/howto/bash-completion.
For examples of completion files, take a look at the bash-completion project from Debian (also on Github). See also https://unix.stackexchange.com/questions/4738/an-easy-bash-completion-tutorial.
If you use zsh, hack.augusto linked to the documentation for writing completions.
This is what the readline module is for.
Actually, readline is a common C library so it has bindings in many languages. And I can say, I've had tons of fun with it.
Enjoy B)
You might want to try the zsh shell, it has a great completion system with support for tons of applications.
The completion system is written with the shell language, but if you really want to use python you can run the interpreter from inside your completion function. The down side it that if you want to write completion for your own software, you will need to do some reading (user manual and the manpage for instance).
Take a look at the source of the 'cmd' module in the Python library. It supports command completion.

python programming query

I'm new to Python programming.
My question is where to write python programs in linux--
in terminal or
any writing pad like gedit etc
Should we write one program again and again for running or we should call the program and run it.
After you install Python (it's installed by default on most Linux-es), you have two options:
Terminal. Just type python <ENTER> and you will be taken to the Python interpreter interactive prompt. For anything but the most basic stuff, however, you may want to intall IPython - an alternative interactive prompt that's much better than the default one.
In a file. Save your code into a .py file and run it with python myfile.py
But first and foremost, start by learning Python - the official tutorial is a great place to start. Among other useful information, its chapter 2 discusses how to use the Python interpreter for beginners.
Stackoverflow also has a lot of great resources for learning Python. This question, for example, and many others.
If you are learning, or you are evaluating expressions, you could run Python in terminal, or use IDLE.
But if you are writing large chunks of code, then you should consider using an IDE.
You could either use Geany, or use Eclipse with PyDev. I prefer Eclipse myself.
For running, you can run it using the command python program.py, or just add the line
#!/bin/python
to the beginning of your program, grant it execution permission using chmod, and run it with ./program.py command.

Categories

Resources