Referring to A Simple Example from python.org:
This is setup.py:
from distutils.core import setup
setup(name='foo',
version='1.0',
py_modules=['foo'],
)
And then it says to run this command:
python setup.py sdist
How/what/where is the parameter sdist being parsed?
The setup function parses the command line arguments: it reads sys.argv. See source code on GitHub.
You're certainly confusing between function arguments (the ones defined in the setup.py script) and the command line arguments: (which are setup.py sdist).
A piece of information is given in the run_setup function comment:
'sys.argv[0]' will be replaced with 'script' for the duration of the call. 'script_args' is a list of strings; if supplied,
'sys.argv[1:]' will be replaced by 'script_args' for the duration of the call.
Related
I often update my own libs using in my projects and i want to automate it so i wrote a script which calls command which builds a .whl lib by os.system and setup.py module gets build params by sys.argv but it returns as response:
invalid command name '1.0.9'
command i call: python ./LoggerLib/setup.py bdist_wheel 1.0.9
sys.argv: ['./LoggerLib/setup.py', 'bdist_wheel', '1.0.9']
The only way i know at this moment is to write the data to other file and read it in setup.py but it isn't elegant.
Do you know how to do it in another way?
I have a compile.py script:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("module1.pyx"))
that compiles my Cython code. The drawback is that I have to call it with a command-line parameter build:
python compile.py build
Instead, I would like to be able to call this compile.py directly from Sublime Text, as usual, with CTRL+B. To do that, it should work from:
python compile.py
Question: how to modify the above script so that it can be run with python compile.py?
Method #1:
Use script_args like this:
setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build'])
or
setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build_ext'])
(both work).
If you want the output files to be in the same directory, you can use:
setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build'],
options={'build':{'build_lib':'.'}})
or
setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build_ext'],
options={'build_ext':{'inplace':True}})
Method #2:
Add this on top:
import sys; sys.argv = ["", "build"]
It's a bit hack-ish but it works fine, and avoids to have to create a new build-system, like with Build and run with arguments in Sublime Text 2 (link kindly provided by #Melvin).
Python - When parsing the following command-line arguments...
sys.argv = ('%s %s build_ext --inplace' % ('python', sys.argv[0]))
...for Cython inside my buildscript "myscript_pyd_setup.py" for "myscript.pyd" that uses the "myscript.py" file I get the following error (e1):
python error 1: invalid command 'y'
The python command-line option python -- help revealed no corresponding option 'y'. This makes sense otherwise the error would not exist. Searching SO, Python docs and the web resulted in unrelated articles about Tkinter. As I do absolutely nothing with Tkinter, as far as I know it, I'm wondering if this error is from the python command-line interpreter at all?
My effort:
What I tried to accomplish is parsing arguments sys.argv.append('build_ext --inplace') to python command-line interpreter when running the "buildscript" from within the editor (Komodo edit 11.x) but it returns also with an error (e2) similar to the version from command-line or another editor.
python error 2: invalid command name 'build_ext --inplace'
Running "myscript_pyd_setup.py" with sys.argv.append('build_ext') builds the required *.pyd file just fine. What I don't understand is why its choking on --infile. This works for py2exe and pyinstaller.
The objective:
To run the setup-script of "myscript.py" from within the editor and not having to flip back-and-forth to the command-line editor for compiling *.py > *.pyd when I changed code inside "myscript.py" and want to see the result quickly.
Note: parsing command-line python myscript_pyd_setup.py build_ext --inplace works fine when sys.argv... is commented-out in the "buildscript"!
A third option was to use cythonize in combo with the "myscript.py" and "myscript.pyd" files but that showed a copyfile error for "myscript.pyd". Relevant but not for the above asked "error = y" question.
Any thoughts and help on how to automate this part to prevent RSI are more than welcome! Thx.
My "myscript.py" example code:
import sys, time, os
#...snippet...
def print_me():
text = "bar(man), yes, Hello, how do you do Mr. foo?"
return text
if __name__ == '__main__':
#...snippet...
print_me()
The myscript_pyd_setup.py:
# myscript.py
try:
from setuptools import setup
from setuptools import Extension
except ImportError:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
#from Cython.Build import cythonize
import sys
print 'sys.argv[0] : %s' % sys.argv[0]
#sys.argv = ('%s %s build_ext --inplace' % ('python', sys.argv[0]))
sys.argv.append('build_ext --inplace')
ext_modules = [Extension("myscript",['myscript.py'])]
#ext_modules = cythonize("myscript.py")
#setup: "name" and "cmdclass" are commented-out when using cythonize.
setup(
name= 'XYZ model class',
cmdclass = {'build_ext': build_ext},
include_dirs = [],
ext_modules = ext_modules)
sys.argv is a list of arguments. When you append 'build_ext --inplace', you literally append that as a single argument (as if you've passed it enclosed in quotes from the shell command line). That is what happened in the second case.
In the first one, you've assigned string back to sys.argv. But string is also a sequence, so your commands ran as if called with (argv[1:]): ['y', 't', `h`, ...]
In either case you ended up with an option/sub-command unknown the the argument parser.
I have a Python code that uses two command line arguments. I am using linux terminal for all command line tasks. Now I am trying to use Cython to speed up my Python code.
For that I have compiled the Python code to C using build_ext module by creating this setup.py file:
setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup (
cmdclass = {'build_ext': build_ext } ,
ext_modules = [
Extension ("myCode", ["myCode.py"]) ,
])
And then compiling my Python code into C using:
python setup.py build_ext -i
The following were generated:
[file]myCode.c
[file]myCode.so
[folder]build
--[folder]temp.linux-x86_64-2.7
----[file]myCode.o
I want to run the generated file with command line arguments.
Till now in Python I was using the usual command
>> python myCode.py arg1 arg2
I am very new to Cython, infact I started using it to address the inherent speed issue of Python after code level algorithm optimization. I need inputs on which files to run, and how to run the converted C code and with command line arguments. Thanks in advance.
As mentioned you compiled a Python module. So to call from Linux you have to write a .py script that imports your compiled module and does any calculations needed. Then you can run it with your typical Linux command.
I'm trying to cythonize some python modules (on windows) and want the resulting .c files stored in a different directory.
I tried something like this:
from Cython.Build import cythonize
module_list = cythonize(r'C:\myproject\module.py',
force=True,
build_dir=r'C:\myproject\compiled_modules')
The compilation is done but the file module.c is created in C:\myproject and not in C:\myproject\compiled_modules as expected. What am I doing wrong?
I'm not sure the cythonize function can be coerced into doing that. I would just use the command line:
/path/to/cython yourmod.py -o compiled_modules/yourmod.c
Edit:
This command can be called from a python script using the subprocess module:
import subprocess
subprocess.check_call(["/path/to/cython", "yourmod.py", "-o", "compiled_modules/yourmod.c"])
Edit 2:
According to this Sage developer, relocating the c/cpp files is not supported by cython, so it looks like either compiling from the command line or using a normal configuration and then moving the c/cpp files are your only options.