How to call python from c++ - python

I'm Python new comer. I tried to call a python script from c++ like this (in Raspberry Pi).
std::string pythonCommand = "python Callee.py \""+a+"\" "+b;
int res = system(pythonCommand.c_str());
after running I got this.
python: can't open file 'Callee.py': [Errno 2] No such file or directory
But I can run Callee.py with command line successfully and both file stored in same directory.
What was I missing ?

You probably are running the python interpreter (and your python Callee.py command) in some strange directory (i.e. in some other directory than what you are expecting).
You could use getcwd(3) before your call to system(3) to find out your current working directory.
You might use the chdir(2) system call (before calling system) to change the directory to something appropriate. See perhaps this.
I recommend also reading Advanced Linux Programming
Read also about Extending and Embedding the Python Interpreter; but if you need to embed some interpreter consider also Guile & Lua.

you can try something like this
system("/work/test/pythonscript.sh")
and define inside this script how your python script is executed/called.
This way you dont trip over format errors (c_string() and "\r" or OS-dependent line endings)

Related

Run python script from another directory

I feel a little foolish that I don't know this, but I tried to do it today and was surprised when it didn't work....
I have a directory C:\test with a demo script, lets call it demo.py
If i am in C:\test then I can just do python demo.py. Easy
I could also use a relative path, so from C:\, it's python test\demo.py
What if C:\test is on the path?
I was expecting to be able to now do python demo.py from anywhere however...
python: can't open file 'demo.py': [Errno 2] No such file or directory
I feel foolish because I thought this was straightforward, but I have searched around and have not found a solution. Am I fundamentally misunderstanding something here about how the Python interpreter finds scripts to run? I don't think this is anything to do with PYTHONPATH, as I understood that to relate to loading of modules inside scripts.
This is on Windows 7, by the way.
The PATH is only used to search for commands. A first way is that a Python script can be used directly as a command and in that case the PATH will be used: just use demo.py instead of python demo.py.
It will rely on OS specific ways. On Windows, file type (given by the extension - here .py) can be given default application to process them, while on Unix-like, the first line of a script can declare the program that will process it.
Alternatively, python allows to launch a module that will be searched in the PYTHONPATH (not PATH) by using python -m module or for Windows py -m module.

Running Python scripts through the Windows Command Line

I've just started learning Python using Learning Python by Mark Luts. In his book he offers an example of a simple script that is called through the Windows shell. In the example, he calls is as follows:
C:\code> python script1.py
I've gone and modified the Environment Variables on my machine so that I can call
C:\User\Example> python
to open up the interpreter and I can also call something like
C:\User\Example> script1
to run a script that I've written and placed in my desired directory. My issue is that I can not call
C:\User\Example> python script1.py
in my command line the same way he does in the book. He's mentioned something about a PYTHONPATH Environment Variable, however, this variable isn't present on my machine. I only have 'path', 'TEMP', and 'TMP'. Particulary, when I try to make such a call I get the error
python: can't open file 'script1.py': [Errno 2] No such file or directory
What do I have to do in order to get this sort of command to work properly on the command line?
From the book (p. 44, 4th Ed):
Finally, remember to give the full path to your script if it lives in a different directory from the one in which you are working.
For your situation, this means using
C:\User\Example> python C:\User\Example\my_scripts\script1.py
You could write a batch file that looks for the script in a predefined directory:
#echo off
setlocal
PATH=C:\User\Example\Python36;%PATH%
SCRIPT_DIR=C:\User\Example\my_scripts
python %SCRIPT_DIR\%*
You are calling python from within the context of C:\User\Example, and passing it a name of a file you want to run through the intepreter (script1.py). It is clear that the PATH variable is setup correctly such that you can call python from anywhere on you computer, since we can see that it is running but not actually able to find your script.
However, you stated in the comment that your scripts are actually located in C:\User\Example\my_scripts. In other words, you are passing python a name of a file that doesn't exist!! (at least from the contect of C:\User\Example).
You need to be in the directory of the script in order for the python executable to be able to find it.
Alternatively, you can run the python command and give it more information as to where the script is. For instance, you could run python .\my_scripts\script1.py if you are running from within the contect of C:\User\Example and your scripts are in C:\User\Example\my_scripts.

Run Python file from matlab .m file

I am looking to run a file I created in python from a matlab script. I have checked that my python file works if I run it from the python interface. However I have not been able to get my python to run from matlab. The following is the code situation I am in.
In matlab., I have the following code:(My file name is pgcode.py)
! python pgcode.py
and interchangeably I have use this code as well:
system('python pgcode.py')
The error that results in matlab is:
"python: can't open file 'pgcode.py': [Errno 2] No such file or directory"
I have set my PATH directory and I really think this is an issue with setting the path so that I can find the file I have created but I haven't been able to figure out how to do this. I am using windows and Python 2.7.5. Any help is much appreciated. Thanks in advance!
There might be another way to do this, but here are two options.
First replace system('python pgcode.py') with system('pgcode.py'). Make sure that pgcode.py has execute permissions and in on your PATH. If you're using a unix/linux/mac type system, make sure pgcode.py has #!/usr/bin/env python as the first line, that's called a shebang.
Option two, is to use the full path when you call system(pathon /full/path/to/pgcode.py).
Hope that helps.
Your $PATH should control where python comes from, but I don't believe it will control where your pgcode.py comes from - at least, not in the way you're using it now.
You might want to either use a #!/usr/bin/env python and make your script executable, or be very mindful of what directory you're in when you try to python pgcode.py (you can prepend "pwd;" to your python command to see), or specify a full path to pgcode.py.
HTH

Why don't I have to add 'python' before invoking Python script?

I am new to programming, and Python is my first language.
I've added Python to my Path, but when I use the Command Prompt, I don't have to add python before myscript.py as opposed to many tutorials I've seen. Here is an example:
C:\User\MyName>Welcome.py
Welcome to Python
Python is fun
When I enter 'python', there is a subsequent error:
C:\User\MyName>python Welcome.py
python: can't open file 'Welcome.py': [Errno 2] No such file or directory
Do I really need the 'python'?
Thanks in advance!
If you followed the Python on Windows FAQ, it seems that the standard Python installer has already taken the liberty of associating .py files with an open command to ..\..\Python\python.exe "%1" %*.
How do I make Python scripts executable?
On Windows, the standard Python installer already associates the .py extension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as ‘foo.py’. If you’d rather be able to execute the script by simple typing ‘foo’ with no extension you need to add .py to the PATHEXT environment variable.
Who'd have thunk! This isn't the way it used to be four years ago when I first installed Python on my Windows machine.
Yes and no.
It really depends on how the script is written.
On most unix systems (Linux, Mac OS), you could include #!/bin/python to the top of (as the first line of) your script and therefore execute it by just calling the filename on the command line. That first line tells the shell that this file contains a python program. The shell then uses the python interpreter to execute the file (translation: it translates your $ Welcome.py to $ /bin/python Welcome.py <- note that python is being called explicitly and that it's the same path as what's on the first line of your file).
Presumably, the Windows OS can also be instructed in the same way, though I have never been able to do it myself, nor have I tried very hard (I moved away from windows about 5 years ago). This is why you'll need to explicitly call python.
Calling python tells the OS: "hey! open that program called python and tell it to run the file Welcome.py". This is exactly what the command /bin/python Welcome.py does on a unix system
When you install python on windows with a regular installer, .py files are associated with the python.exe you installed. When you type Welcome.py, Windows searches the local directory and then all paths in the PATH variable for a program called Welcome.py and runs it via python. Since this worked for you, it means that Welcome.py is somewhere on your path or in your local directory.
You can figure out your file associations with the assoc .py and ftype Python.File commands. The echo %PATH% and echo %PATHEXT% commands are also useful.
When you type python Welcome.py, Windows searches all paths in the PATH variable for a program that starts with 'python' and ends with an extension in PATHEXT. It finds 'python.exe' and runs it. Python in turn looks for a script called Welcome.py in the current directory. Since this didn't work for you, it means that Welcome.py is not in your local directory. It would have worked if you had given the right path to Welcome.py.
You can find out where Welcome.py is with the (not surprisingly) where Welcome.py command.
If you only have a single python installation, there is no need to call python myscript.py ....

Python to .bat conversion

I would like a user on Windows to be able to run my Python program, so I want to convert it to a .bat file. Is there a way to convert it? I've tried searching, but didn't find anything.
Unless your script is trivial it will not be possible to 'translate' it into a batch file. However two options exist:
Create a batch file to run the python script
Attempt to compile the script into an executable
The first option is trivial. Simply create a batch file as so:
#ECHO OFF
PATH_TO_PYTHON\python.exe PATH_TO_SCRIPT.py
If you are in a corporate environment you could put a python installation on a network and create a batch file to run the script from there. Otherwise you will need the user to install python. If python is on their path then the batch file can be simplified to:
#ECHO OFF
python PATH_TO_SCRIPT.py
Alternatively, there are options available that attempt to compile scripts into .exe files. I've never had any success with them, but py2exe seems the most common.
No, I don't think you can reasonably expect to do this.
Batch files are executed by the Windows command interpreter, which is way way more primitive.
Python is a full-blown programming language with a rich and powerful library of standard modules for all sorts of tasks. All the Windows command interpreter can do is act like a broken shell.
On the other hand, Python is available on Windows, so just tell the user to install it and run your program directly.
you can do it in 2 ways :
create a normal text file with an extension .bat and write
#ECHO OFF
"python.exe location" "your file.py location"
create a normal text file with an extension .bat and write
"python.exe location" "your file.py location"
pause
(sorry for my English Im from armenia)
After 10 years I finaly did that without py2exe and some other things
I used openai playground to do that and thats worked:
I just writed "convert python code into .bat file: [my code]"
Just create a batch file that contains this two lines:
yourfilename.py
pause
Then run the batch file by double-clicking it.

Categories