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))
Related
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.
Sorry, If I'm a little bit unclear.
I'm writing a module with python, and its basic functions must be run or shut, which runs or terminates other specified python scripts correspondingly.
Run is realised by os.popen(), but I did not manage to find any possible realisation for shut command. Is there any built-in function in python, which allows you to terminate other scripts?
I have a Python 2.7 script that among others contains the following piece of code:
import spss
columns = []
spss.StartDataStep()
dataset = spss.Dataset()
for column in dataset.varlist:
columns.append(column.name)
spss.EndDataStep()
print columns
When running this code inside a SPSS syntax (so between BEGIN PROGRAM. and END PROGRAM), it runs as expected and I end up with the variables in the active dataset.
However, when running the same code as part of a script (so from Utilities > Run script...) will return me no results.
It looks as if the SPSS session context is not taken into consideration when running a script.
Is there a way around this problem, or am I doing something wrong?
I don't want to run my code as part of Syntax file, I just want to use vanilla Python scripts.
This is, unfortunately, a complicated issue. I don't think Statistics is working as documented. I will take this up with Development.
It appears that in V24, when you run a Python script via Utilities > Run Script (which is the same as issuing the SCRIPT command), your script is connected to the Statistics Viewer process but not to the Statistics backend (the spssengine process), which is where the data live. There are typically three processes running - the stats.exe process, the spssengine process, and, for Python code, the startx process. Your script can issue commands via the spss.Submit api and can use other spss apis, but they go against a new copy of the backend, so the expected backend context is not present.
To get around this, you can run a trivial program like
begin program.
import ascript
end program.
where ascript.py is a Python module on the Python search path. (You could put these lines in an sps file and use INSERT to execute it, too.)
Another way to approach this would be to run Statistics in external mode. In that mode, you run a Python program that uses SPSS apis but the Python program is on top, and no Statistics user interface appears. You can read about this in the Python scripting help.
An advantage of external mode is that you can use your favorite Python IDE to build and debug your code. That's a big advantage if you are basically a Python person. I use Wing IDE, but any Python IDE should work. You can also set up an alternative IDE as your default by editing the clientscriptingcfg.ini file in the Statistics installation directory. See the scripting help for details. With a tool like Wing, this lets you debug your scripts or other Python code even if run within Statistics.
I wrote a Python program which will be executed on both the Primary Production server, as well on the Disaster Recovery server. There is a slight difference in behavior when the program is run on the Disaster Recovery server.
Therefore the program needs to determine which server it is running on.
We have many other ksh programs running on these servers, which have the same requirement: run on both servers, but there could be a slight difference on the DR server. All of these scripts 'dot' in an environment file, then check " if environment variable $DR_SITE equal 1" to determine if its running on the DR server.
I want to use the existing environment file from my Python program - to determine if it is running on the DR server. I can not just read the this environment file, it is actually a ksh script that itself has some logic prior to setting the DR_SITE variable.
Which brings me to the original question:
How do you 'dot' in (or execute an environment file as described above in python, in order to inherit the environment variables set by the ?
For example, in ksh I would execute this:
. /path/env.set
I tried this, but it did not seem to work (I printed out the DR_SITE value before calling the os.system call, and after, it did not change):
os.system(". /appl/gfpd2/current/D2soe_set")
You could write a ksh script that sources and executes the environment setter and then invokes the Python program;
#!/usr/bin/env ksh
. /path/env.set
exec python /path/your_script.py
Exec is used to save some memory.
(I'm omitting the passing of variables to the Python script since I'm not familiar with ksh.)
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.