I have a C++-program that allows me to run Python-scripts that C++ passes data to. If the Python-script fails the C++-program calls PyRun_InteractiveLoop to allow investigating the problem on an interactive shell. This works alright but I thought "Would be cool if IPython is installed I could use it in that case".
Now my problem is: all I find when looking for "ipython embedding" is instructions how to embed IPython into Python-programs (amongst others http://ipython.org/ipython-doc/dev/interactive/reference.html#embedding-ipython). I tried to reproduce these on the embedded regular python-shell I have but most of them fail in some way (usually because of a missing sys.argv .... that one I can solve).
Any suggestions how to do this? My first plan would have been to first import IPython via the C-API (that one I got covered). If that fails use the "regular" shell. Otherwise call IPython.embed() (or similar) via PyRun_InteractiveOneFlags
Have you considered using python debugger
>>> import pdb
>>> import yourmodule
>>> pdb.run('yourmodule.test()')
Related
Let me preface this post that I am incredibly new to python. I took a course in codeacademy but haven't actually used python on my own computer until now. Let me also explain to everyone that my coding experience is limited to VBA and excel. I have very little knowledge of how to use the command prompt, how libraries work, etc, etc.
I'm having some issues. I've changed my directory to recognize python when I type python in my command prompt. Then I can do simple things like
print "hello"
however if i write something like this
def firstfunction(t):
print t+5
return
firstfunction(5)
I would expect this to print the number 10, however I get the following error:
File"stdin", line 4
first function(5)
^
syntaxerror: invalid syntax
However if I use this in IDLE and run it it works fine.
Next I've saved some python programs I wrote. One I saved as Hello.py
i want to run this code from my python exe or from the command prompt but can't figure out how.
Please any answers helpful, if you use computer lingo, try and keep it as simple as possible. I'm super new!
Thanks
If the location of python is not included in your path, add it. But in any case, you should be able to run this successfully from the directory containing your python.exe.
e.g, if python lives in C:\Python27:
cd C:\Python27
python mypythonscript.py
When we run commands on power cli it displays the operations we have performed.
For example
Start-VM –VM “VM1”
simply starts the VM in the v center.
I want to write such code in Python that we can call these commands in code and store the output and display to the user.
Is there any way to link our Python code with power cli commands or we can say can we bind power cli code inside Python?
You want two things: dot source the script (which is (as far as I know) similar to python's import), and subprocess.call.
import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePowershell\";", "&hello"])
So what happens here is that we start up powershell, tell it to import your script, and use a semicolon to end that statement. Then we can execute more commands, namely, hello.
You also want to add parameters to the functions, so let's use the one from the article above (modified slightly):
Function addOne($intIN)
{
Write-Host ($intIN + 1)
}
and then call the function with whatever parameter you want, as long as powershell can handle that input. So we'll modify the above python to:
import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePowershell\";", "&addOne(10)"])
this gives me the output:
PowerShell sample says hello.
11
You'll need to edit the above to include the PowerCLI library, but it should work.
I'm using Enthought Canopy on Mac OSX Lion. I'm using this because of the ease at which modules and libraries can be downloaded and installed (had a lot of trouble downloading pandas and numpy through terminal due to a number of issues).
So, now I'm doing my coding for a project in Canopy, which is OK (still prefer Wing, though). The problem I have encountered is that I need to ask the user for input. When I do, for instance:
x = input('Enter your input here: ')
I get an EOF error as follows: EOFError: EOF when reading a line
I was looking around and believe that this may be something which Canopy does not support. Was wondering if this is, indeed, the case and if there is a solution/workaround to this problem?
I assume that you are entering this code in the IPython shell that is embedded in the Canopy editor. This is an upstream bug/deficiency in IPython's Qt console. Because the remote shell is not actually hooked up to a terminal, functions like input() and raw_input() need to be replaced to get the input from the GUI console widget instead. IPython (and thus Canopy) does replace raw_input() but does not replace input(). This code will work using Canopy's Python interpreter if you were to put it into a script and execute it from the command line, and it would also work in a terminal instance of IPython. It was most likely overlooked because it is usually considered a bad idea to use input().
Please use raw_input() instead and parse the string that you get. You can use eval() if you must, but I do recommend using a more specific parsing/conversion function.
I am new to python and wxpython, I am making a automated tool using python and for user interface wxpython and i use shell script.shell script can be called from the python. but now I am facing problem with the spinctrl value. whenever that spinctrl value changes it have to send that value into one txt.exe file which is written in BASH .{ if we run txt.exe file in command line it will ask for number then it will accept that value whenever we press enter}. but i am not able to understand that how to send value from spinctrl to txt.exe whenever i press "ok" button in GUI. please share your thoughts and knowledge.
Thank you
To call an executable in Python, you need to look at Python's subprocess module: http://docs.python.org/2/library/subprocess.html
If your exe doesn't accept arguments, then it won't work. If you created the exe yourself using something like PyInstaller or py2Exe, then you need to make it so your app can accept arguments. The simplest way is using sys.argv, but there are also the optparse and argparse libraries in Python as well.
I do not get a good feeling about your concept. It is hard to say, but I suspect you would benefit from talking with someone experienced about what you are trying to achieve and how it could best be done.
However, to get you started down your current path, I think you should take a look at wxExecute ( http://docs.wxwidgets.org/2.8/wx_processfunctions.html#wxexecute ). This will allow you to run your txt.exe utility from inside your GUI.
wxExecute is a wxWidgets utility. If you are working in python then there will be a more direct way to do this using a python utility - some-one else will have to advise on that. I suggest you edit your question for clarity so a python expert can help you.
I'm trying to embed the Python interpreter and need to customize the way the Python standard library is loaded. Our library will be loaded from the same directory as the executable, not from prefix/lib/.
We have been successful in making this work by manually modifying sys.path after calling Py_Initialize(), however, this generates a warning because Py_Initialize is looking for site.py in ./lib/, and it's not present until after Py_Initialize has been called and we have updated sys.path.
The Python c-api docs hint that it's possible to override Py_GetPrefix() and Py_GetPath(), but give no indication of how. Does anyone know how I would go about overriding them?
You could set Py_NoSiteFlag = 1, call PyInitialize and import site.py yourself as needed.
Have you considered using putenv to adjust PYTHONPATH before calling Py_Initialize?
I see it was asked long ago, but I've just hit the same problem. Py_NoSiteFlag will help with the site module, but generally it's better to rewrite Modules/getpath.c; Python docs officially recommend this for “[a]n application that requires total control.” Python does import some modules during initialization (the one that hit me was encodings), so, unless you don't want them or have embedded them too, the module search path has to be ready before you call Py_Initialize().
From what I understand Py_GetPath merely returns module search path; Py_GetProgramFullPath is self-describing; and Py_GetPrefix and Py_GetExecPrefix are not used by anyone, except some mysterious “ILU”.
The following functions can be called before calling Py_Initialize():
Py_SetProgramName()
Py_SetPythonHome()
Py_SetPath()
All of these affect the way Python finds modules. I recommend reading the documentation on these functions and playing around with them.