使用命令行参数创建python模块

时间:2022-06-11 02:49:44

I've uploaded a python module to pypi and locally installed the module with:

我已经将一个python模块上传到pypi并在本地安装了模块:

pip install microarray_quantilenorm

pip install microarray_quantilenorm

I'm trying to run the module on commandline with pass-through arguments.

我正在尝试使用传递参数在命令行上运行该模块。

When I call the python script directly from site-packages, everything works fine.

当我直接从site-packages调用python脚本时,一切正常。

python /site-packages/microarray_quantilenorm/microarray_quantilenorm.py *csv

But calling:

python -m microarray_quantilenorm.__main__ *csv

OR:

python -m microarray_quantilenorm *csv

Results in:

/anaconda/bin/python: No module named microarray_quantilenorm.__main__

I cannot track down to whether there is an issue in setup.py, my __init__.py or elsewhere. Thanks.

我无法追踪setup.py,my __init__.py或其他地方是否存在问题。谢谢。

1 个解决方案

#1


The whole point of __main__.py is that it's the file that's run when the package is run as a module. In other words, just do python -m microarray_quantilenorm, not python -m microarray_quantilenorm.__main__.

__main__.py的重点是,当程序包作为模块运行时,它是运行的文件。换句话说,只需要做python -m microarray_quantilenorm,而不是python -m microarray_quantilenorm .__ main__。

However, usually, there's an even better solution. Assuming you're using setuptools rather than trying to do the whole mess manually, use a console_scripts entry point. This will make the setup process automatically create a script named microarray_quantilenorm and install it in your bin or scripts directory that does the right thing to load the package and run its main code (or, if you prefer, some other code within it).

但是,通常情况下,还有更好的解决方案。假设您正在使用setuptools而不是尝试手动完成整个混乱,请使用console_scripts入口点。这将使安装过程自动创建一个名为microarray_quantilenorm的脚本,并将其安装在bin或scripts目录中,该目录可以正确加载包并运行其主代码(或者,如果您愿意,还可以运行其中的其他代码)。

#1


The whole point of __main__.py is that it's the file that's run when the package is run as a module. In other words, just do python -m microarray_quantilenorm, not python -m microarray_quantilenorm.__main__.

__main__.py的重点是,当程序包作为模块运行时,它是运行的文件。换句话说,只需要做python -m microarray_quantilenorm,而不是python -m microarray_quantilenorm .__ main__。

However, usually, there's an even better solution. Assuming you're using setuptools rather than trying to do the whole mess manually, use a console_scripts entry point. This will make the setup process automatically create a script named microarray_quantilenorm and install it in your bin or scripts directory that does the right thing to load the package and run its main code (or, if you prefer, some other code within it).

但是,通常情况下,还有更好的解决方案。假设您正在使用setuptools而不是尝试手动完成整个混乱,请使用console_scripts入口点。这将使安装过程自动创建一个名为microarray_quantilenorm的脚本,并将其安装在bin或scripts目录中,该目录可以正确加载包并运行其主代码(或者,如果您愿意,还可以运行其中的其他代码)。