Python C-API equivalent of "python -m module_name" (python 2.7) - cpython

Having looked the Python C-API documentation, I cannot figure out a way to execute a python script given just the name of the module (from c or c++ code).
Is there an equivalent C-API function for python -m module_name?
I am using Python 2.7 embedded in a C++ executable.

This is done with the standard library runpy module (documentation; code). You just need to call the (undocumented) function runpy._run_module_as_main(module_name).
The C API code used by the Python interpreter just imports that module, gets the _run_module_as_main attribute and calls it (i.e. uses runpy as a Python module).

Related

How to run a julia script through terminal

I have a script main.jl which prints a simple "Hello world" string:
println("Hello world!")
However, when trying to run the script through the terminal like this:
julia> main.jl
I get the error:
ERROR: type #main has no field jl
All the information I can find online suggests calling the script like I do to run it. I have assured that I'm in the correct directory - what am I doing wrong?
You are trying to run the file from the Julia REPL (as indicated by the julia> prompt at the beginning of the line). There, you have to include the file as #AndreWildberg mentions. This will run the commands from the file as if you had typed them into the REPL.
The information you found online might have been about running Julia from "normal" terminal, aka a console shell like bash on Linux. There, running julia main.jl will run the program, although the REPL method above is usually preferred for working with Julia.
(question about calling the script with arguments asked in the comment):
First of all, I'll mention that this is not the usual workflow with Julia scripts. I've been writing Julia code for years, and I had to look up how to handle command-line arguments because I've never once used them in Julia: usually what's done instead is that you define the functions you want in the file, with maybe a main function, and after doing an include, you call the main function (or whichever function you want to try out) with arguments.
Now, if your script already uses command-line arguments (and you don't want to change that), what you can do is assign to the variable that holds them, ARGS, before the include statement:
julia> push!(empty!(ARGS), "arg1")
1-element Vector{String}:
"arg1"
julia> include("main.jl")
Here we empty the ARGS to make sure any previous values are gone, then push the argument (or arguments) we want into it. You can try this out for educational purposes, but if you are new to the language, I would suggest learning and getting used to the more Julian workflow involving function calls that I've mentioned above.
The julia> prompt means your terminal is in Julia REPL mode and is expecting valid Julia code as input. The Julia code main.jl would mean that you want to return the value of a field named jl inside a variable named main. That is why you get that error. The Julia code you would use to run the contents of a file is include("main.jl"). Here the function include is passed the name of your file as a String. This is how you should run files from the REPL.
The instructions you read online are assuming your terminal is in shell mode. This is usually indicated by a $ prompt. Here your terminal is expecting code in whatever language your shell is using e.g. Bash, PowerShell, Zsh. When Julia is installed, it will (usually) add a julia command which works in any shell. The julia command by itself will transform your terminal from shell mode to REPL mode. This julia command can also take additional arguments like filenames. If you use the command julia main.jl in this environment, it will run the file using the Julia program and then exit you back to your terminal shell. This is how you should run files from the terminal shell.
A third way to run Julia files would be to install an IDE like VSCode. Then you can run code from a file with keyboard shortcuts rather than by typing commands.
See the Getting Started Documentation for more information.
Adding to Sundar R's answer, if you want to run script which takes commandline arguments from REPL, you can check this package: https://github.com/v-i-s-h/Runner.jl
It allows you to run you script from REPL with args like:
julia> #runit "main.jl arg1 arg2"
See the README.md for detailed examples.

what is Cpython is this single module or complete Python

I like to know what is CPython.
What I understood is
Its flavor of python, (correct me if its wrong) so basically systems programming made easy in Python language but again I could not find system calls code in CPython implementation for mmap or etc. does CPython has sockets, listen, accept, send, and recvfrom system calls too for Cpython user developer.
This is the link of Cpython https://github.com/python/cpython so if I install it then will my version already python version Python 3.9.7 (default, Sep 10 2021, 14:59:43) [GCC 11.2.0] on linux will it continue working or do I need to make changes to that.
Can I install any other module and run it in Cpython application?
Thanks for any info
CPython is the “official,” or reference implementation of Python. If you are installing python from python.org you are running Cpython implementation. You can confirm this via platform module.
>>> import platform
>>> platform.python_implementation()
'Cpython'
CPython contains complete implementation of the language(Including standard library/compiler/Byte Code Interpreter etc). If you want to understand how the source code is laid out you can refer Your Guide to the CPython Source Code.
Additional reference:
Python vs Cpython

Calling an external command in Julia

How can I call an external command (as if I had typed it into the Windows command prompt or Unix shell) from within a Julia program? I know this is possible with other languages but I'm not sure how to do it in Julia.
According to the Julia docs,
The command is never run with a shell. Instead, Julia parses the command syntax directly, appropriately interpolating variables and splitting on words as the shell would, respecting shell quoting syntax. The command is run as Julia's immediate child process, using fork and exec calls.
A simple example is as follows:
julia> testcommand = `echo HelloWorld`
`echo HelloWorld`
julia> typeof(testcommand)
Cmd
julia> run(testcommand);
HelloWorld
See the docs linked above for a deeper dive of the low-level details occurring of what is going on here.

Is it possible to read Doc Strings of a Python file w/o using python shell?

I was wondering if there is any way we can read doc strings present in a python file from command line, prior to importing it, or w/o using any Ipython or Python kernel, just like 'man' command does.
You can execute some python code from the command line, so given a module named my_module.py, and a function named my_function, the following:
python -c "from my_module import my_function;print my_function.__doc__"
will print out the docstring

using a foreign library in sbcl - uffi or cffi?

I am an struggling with using a C++ library I have just sucessfully
compiled on ubuntu in sbcl.
I have tried to use the .h file parser 'ah2cl' but from the
documentation it is not clear if I require UFFI or CFFI (is there a
difference?). My attempts to use the dummy test library and the
dummy header file provided with 'ah2cl' have failed. I get a message
about a missing CALLBACK package from sbcl. But does sbcl not support
callback natively ?
Is there another .h file parser that I should use for this
kind of task ?
You might want to try CFFI's groveller, which parses header files to produce the corresponding CFFI bindings. BTW, UFFI is basically deprecated in favour of CFFI for a while now.

Resources