I want to invoke those C function or executables in python. Is that possible.
Yes, you can write C code that can be imported into Python as a module. Python calls these extension modules. You can invoke it from Python directly, an example from the documentation:
Python Code
import example
result = example.do_something()
C Code
static PyObject * example(PyObject *self)
{
// do something
return Py_BuildValue("i", result);
}
If I want the C code to be a library, which means I use it with #include and linkage of the *.o likely in python, how to do it or is that possible.
You build it as a shared library *.dll or *.so You can also investigate using distutils to distribute your module.
If I write the C code into executable, which means it becomes a command, can I invoke it in python directly?
If you write a *.exe then you are doing the opposite (invoking Python from C). The method you choose (exe vs shared library) depends on if you want a "C program with some Python" or a "Python program with some C".
Also, I heard that python code can be compiled, does that mean we can execute the code without the source file? Are the output files binary files? Does it improve performance?
Python reads *.py files and compiles to *.pyc bytecode files when you run it. The bytecode is then run in the Python virtual machine. This means "executing the same file is faster the second time as recompilation from source to bytecode can be avoided." (from the Python glossary) So if you haven't edited your *.py files, it will run the *.pyc. You can distribute *.pyc files without *.py files, however they are not encrypted and can be reverse-engineered.
Answer from Jacqui Gurto on Stack OverflowI want to invoke those C function or executables in python. Is that possible.
Yes, you can write C code that can be imported into Python as a module. Python calls these extension modules. You can invoke it from Python directly, an example from the documentation:
Python Code
import example
result = example.do_something()
C Code
static PyObject * example(PyObject *self)
{
// do something
return Py_BuildValue("i", result);
}
If I want the C code to be a library, which means I use it with #include and linkage of the *.o likely in python, how to do it or is that possible.
You build it as a shared library *.dll or *.so You can also investigate using distutils to distribute your module.
If I write the C code into executable, which means it becomes a command, can I invoke it in python directly?
If you write a *.exe then you are doing the opposite (invoking Python from C). The method you choose (exe vs shared library) depends on if you want a "C program with some Python" or a "Python program with some C".
Also, I heard that python code can be compiled, does that mean we can execute the code without the source file? Are the output files binary files? Does it improve performance?
Python reads *.py files and compiles to *.pyc bytecode files when you run it. The bytecode is then run in the Python virtual machine. This means "executing the same file is faster the second time as recompilation from source to bytecode can be avoided." (from the Python glossary) So if you haven't edited your *.py files, it will run the *.pyc. You can distribute *.pyc files without *.py files, however they are not encrypted and can be reverse-engineered.
You don't necessary need to extend Python (which is not trivial, btw), but can use foreign function interface such as ctypes.
How is python able to use c code?
How to import a C file into python - Stack Overflow
How can a Python package use C, like NumPy?
Auto-import PS modules in VS code
Sure, the powershell extension for vs code opens a terminal and that loads the profile script. I believe it uses a different profile from the default though. You can always find your terminal session profile with the $profile variable.
More on reddit.comI was always curious how python modules in c work. I know how to write and use a python c module, but i dont know how it works internally. Can anyone put me in the right path where can i start, an article or sthm or try to explain. And are there any simple tutorials to implement that sort of thing. Like calling .so files from your language etc...