How to run python script linking to .so file within Cygwin? - python

I am new to Python. I want to execute a python script inside which a shared library (*.so file) is included. I try execution on CYGWIN 64 (a Unix-environment on windows). It does not work and found this error: "OSEError: Exec format error". I think that the problem is from the *.so file. How can I execute my python script which uses the shared library (*.so file) within CYGWIN?

Related

jar or exe file from Sikuli python script

I just started doing some scripts with Sikuli in Python.
I would like to know if it's possible (and if yes, how) to create a jar or exe file from my python script (file .py and images).
I would like to easily run my program from other machines that do not have sikuli install (e.g., java -jar my_script.jar or my_script.exe) or give the utility to some colleagues, but I don't want that they see the source code.
Use a jar file, as Sikuli is written in Java and uses Jython. You need to do three things in order for this to work.
Get Sikuli jar:
First get the sikuli-java.jar file (step-by-step instructions here), which you can get by downloading sikuli-setup.jar, and run it using options 4 (only Java) and 6 (all OS compatible). This should download the sikuli-java.jar file.
Distribute your script itself as a jar:
Obfuscated Version:
You can compile your Python using Jython (full instructions here):
$JYTHON_HOME/jython $JYTHON_HOME/Lib/compileall.py python_src/
where python_src is the directory containing all your .py files.
Then you can call these compiled files (which look like myFile$py.class) from Java, using a wrapper class (see full instructions for an example).
This wrapper calls a single Python method, so you can just make that method be your main Python method that kicks off the rest of your program.
Compile the wrapper class:
javac -cp $JYTHON_HOME/jython.jar TestJ.java
Then jar all your .class files:
jar cvfe myapp.jar <packageName>.TestJ *.class
where <packageName> is the package for your wrapper TestJ.java class.
You'll need to add Jython to this jar as well. Just follow the first few steps of the "Python .py files in jar" section below, or download the standalone Jython jar and include it the same way as sikuli-java.jar in the "Combine the jars" section below.
Python .py files in jar:
Now you have to create the jar for your script itself. The full instructions are here, but if you want a summary:
Take the jython.jar file that you get when you install Jython and zip the Jython Lib directory into it, then zip your .py files in, and then add a __run__.py file with your startup logic (this file is treated specially by Jython and will be the file executed when you call the jar with java -jar).
There is also a more general guide from python.org if you prefer.
Note:
The full instructions above assume you have the zip program. If you don't, you can use jar uvf jythonlib.jar Lib instead of zip -r jythonlib.jar Lib (and do similar wherever zip is used). See here for more
Combine the jars
You can simply add sikuli-java.jar to the jar containing your scripts using jar as mentioned in the note above. You can also combine the two jar files (also using jar) if you prefer.
Either way, this should give you a single jar file that can run your Sikuli program across any system with a compatible JVM. Run it using
java -jar myapp.jar
Congratulations, you're done!
See the stack overflow guide or the full Python guide if you need to deal with command line arguments, or generally need more detail.
There are many ways to convert a .py to .exe and excetra
While I believe it is possible to change the extension, the process would be much easier and smoother if you use an extension to python, like Py2exe the download to which can be found with a simple google search (I have also included a link to their site below). The program will allow you to convert the program into a .exe and will run without needing a python installation.
Here is a list of reputable and good options: http://www.freehackers.org/Packaging_a_python_program
I would personally recommend Py2exe (http://www.py2exe.org/) and the tutorial to Py2exe can be found here (http://www.py2exe.org/index.cgi/Tutorial)

Run Python os.system from a sub package

I need to run a python file using an older python 2.6 version installed with an external software.
To do this I have resorted to using os.system such as
os.system('""path/old_python.exe" "file.py""')
(note that the odd no. of ('") is due to the path containing a space c:/program files (x86)/.. as I am running on windows.)
This code string works well if run from a root directory. However, I would like to place this os.system call in a module within a sub-package to my root and thereafter run it from a root module. So the hierarchy would look like this:
/root
call_os_module.py
/subpack1
os_module.py
file.py
If I run this I receive the error:
path/old_python.exe: can't open file 'file.py': [Errno 2] No such file or directory
I have added the full path to subpack1 to sys.path. However, I still receive the same error and os can't find the file. How do I solve this?
Have a look at these questions:
How in Python to run external Python script with the current Python interpreter?
Relative paths in Python
in general: https://stackoverflow.com/search?q=python+relative+path

Is it possible to compile python interpreter to static single exe (with eventual one dll or so) with most used packages?

I don't mean to compile code and interpreter as an exe as it is 99% questions there. I mean to build a static single python.exe to be able to execute any script by giving it as argument any *.py file.
I mean the same situation as it is with nodeJS when you download only single executable.
Or if it is not possible to single exe, maybe to just a few files instead of huge default package such as is with for example Sublime text where all python engine is in python33.dll and python3.3.zip all about 5mb, but there is no python exe to run code externally that is not as a plugin.
On Windows you need three files. You question lists two of them. The third is python.exe. On Windows the interpreter must be built separately from the actual executable so that C modules can access the functions within the interpreter.
The DLL must be built from the Python source so that all the C modules that come with Python can be built into it instead of being contained in the .pyd files that come with Python. The zip file is composed of all the Python modules in the standard library; see the zipimport documentation for more information about the import process.

Bundling Python program with a native binary

Simple question:
I have a python program which launches an executable.
The launcher uses optparse module to setup the run and lauch the binary through shell.
Is it possible to bundle the laucher and the binary into a single package?
Platform of interest is Linux.
using pyinstaller for example you could add the executable file as a resource file, it extracts the resources to a temp folder, from there you can access your resource files as you want

Compiling Python

How can I compile and run a python file (*.py extension)?
python yourfile.py
You have to have python installed first. It will automatically compile your file into a .pyc binary, and then run it for you. It will automatically recompile any time your file changes.
http://www.python.org/download/
Python compiles its files to bytecode before executing them. That means you have to have a Python interpreter installed on the target machine.
If you don't want to install Python on the target machine use py2exe, py2app or something similar.
If you just want to compile sources, without running them, you can do this
compileall.py <directory>
this command will compile python code in that directory recursively
compileall script is usually located in directory like
/usr/local/lib/python2.6
i.e. <prefix>/lib/python2.6 (or similar, depending on prefixes set a python configuration)
As Lulu suggests, you should make sure that resulting .pyc and .pyo files are executable by the users you care about.
compileall can also be used as a module
import compileall
compileall.compile_dir(path)
Python is an interpreted language, so you don't need to compile it; just to run it. As it happens, the standard version of python will compile this to "bytecode", just like Java etc. does, and will save that (in .pyc files) and run it next time around, saving time, if you haven't updated the file since. If you've updated the file, it will be recompiled automatically.
You can also run python with a -O flag, which will generate .pyo files instead of .pyc. I'm not sure it makes much difference. If speed is important, use psyco.
And yes, on Unix (including Linux, BSD, and Mac OS X, or in a unix shell on windows) you can use a shebang line at the top of the file to make the file automatically run using python. On windows, the equivalent is to associate .py files with python.exe, and then make sure your PATHEXT environment variable includes ".PY" extensions.
However, for windows, you more likely want to write a gui program in python (possibly using PyQT4 and ERIC4) which has a .pyw file as its main script, and has .pyw associated with pythonw (which comes with python on windows). This will let you run python scripts on windows just like other GUI programs. For publishing and distribution, you probably want to compile to an executable file using something like py2exe, as others mentioned.
To add to Paul McMillan's answer, if you are on Windows and you have Python installed, then any files ending with the extension ".py" should be associated with the python executable, allowing you to run it like so:
> myfile.py
In *nix, you can begin the file with #!/usr/bin/python and run it like so:
$ ./myfile.py
In *nix systems, if the first two characters of a file are #! then it will execute the file with the specified executable, which I set here to be /usr/bin/python.
If you want to transform a python source file into a double-clickable .exe on windows, you can use py2exe, which can help you build an easy to distribute package.
On most Unix-like systems, you can use the shebang to tell the operating system which interpreter should be called. You simply put
#!/path/to/python
in the first line of your file, where of course you have to replace "/path/to/" with the path you have on your system. In most cases this would be "/usr/bin/python" or "/usr/local/bin/python". On unix systems you could also look for the path with
"#!usr/bin/env python"
or invoke the command
which python
to find the path.
You can then run your program with the command
./yourprogram.py
If it tells you that you do not have permission to do so, you have to use the command
chmod a+x yourprogram.py
Answer for Windows
first you must install python
then set path variable
after that write your python program and save
think there is a python program that name "hello.py"
open cmd.exe
then goto the path that you saved your "hello.py" file,
and then type python hello.py and press enter key.
now the python code is automatically compile and show the result.

Categories