Running SPSS session action from an external Python script - python

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.

Related

Does a "terminal" always need to run with 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.

How to control FEA software like MSC NASTRAN using Python code?

I would like to run MSC NASTRAN using python. I have seen a similiar function in MATLAB using
system('nastran.exe file_name.bdf') #where file_name.bdf is the input file to Run using nastran.
Hence i tried below using python code, but it did not work,
import os
os.system('nastran.exe file_name.bdf')
Could you tell me where i going wrong?
Also, how to give the command line in NASTRAN thru python? Like for example memory allocation for the run, number of cores need to be used for run etc.
some NASTRAN command lines include,
1. scr=yes delete=f04,log,xdb pause=yes
2. mem=10gb bpool=3gb memorymaximum=14gb sscr=500gb sdball=500gb mode=i8
...etc.
I can't speak directly for MSC Nastran, its been a while since I've used it. But most modern FEA programs have an API (application program interface) to allow you to call commands from a external program like python or matlab.
Without an API, you may be limited to using python to start the program from the command line, which is what your code is trying to do. As for how to launch a program from within python, check out this question/answer:
How to run application with parameters in Python?
Easy way to run a MSC NASTRAN file is to create a .bat file and run it from python.
The format for .bat file is:
<nastran.exe location> <Python script file location> <Nastran command line>
An example can be :
C:\MSC.Software\MSC_Nastran\20141\bin\nastran.exe C:\py_nastran_run\example.bdf scr=yes old=no delete=f04,log,xdb
Then include the below line in python script,
status=subprocess.call("runBatch.bat")

Use a single iPython console to run files and parts of code in PyCharm

I started using Pycharm and managed to have the python prompt running after a file is run (link) and I found how to run pieces of code in the current iPython console (link).
For data science, however, it is very convenient to have a single iPython console/Python kernel where I can run files and code, just like Spyder does, and continue using the same started python kernel. For example to load a big amount of data with a script and write another script or pieces of code for plotting in different ways, exploring the data.
Is there a way in Pycharm to do it?
I think that it would imply generating automatically and running a line like:
runfile('C:/temp/my_project/src/console/load_data.py', wdir='C:/temp/my_project/src')
If the option does not exist, is it possible to make a macro or similar to do it?
The option "Single instance only" in Edit configurations doesn't help.

Python outputs only after script has finished in Komodo Edit

Forgive me if it's a silly question. I'm new to Python and scripting languages. Now I'm using Komodo Edit to code and run Python programs. Each time I run it, I have to wait until the program finished execution to see my "print" results in the middle. I'm wondering if it's possible to see realtime outputs as in a console. Maybe it is caused by some preference in Komodo?
Another question is that I know in the interpreter, when I store some variables it will remember what I stored, like in a Matlab workspace. But in Komodo Edit, every time the program runs from the beginning and store no temporary variables for debugging. For example if I need to read in some large file and do some operations, every time I have to read it in again which takes a lot of time.
Is there a way to achieve instant output or temporary variable storage without typing every line into the interpreter directly, when using other environments like Komodo?
The Python output is realtime.
If your output is not realtime, this is likely an artefact of Komodo Edit. Run your script outside of Komodo.
And Python, as any programming language, starts from scratch when you start it. How would it otherwise work?
If you want a interpreter-like situation you can use import pdb;pdb.set_trace() in your script. That will give you an interpreter prompt for debugging.

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