使用pip/其他安装程序安装后在python模块中加载文本文件 [英] Load text file in python module after installation using pip/other installer

查看:160
本文介绍了使用pip/其他安装程序安装后在python模块中加载文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使我编写的程序易于潜在的雇主/员工访问.为了...展示我的技能..或其他任何东西.我不是计算机科学家,而且我之前从未编写过要安装的python模块,因此在这方面我是新手.

My goal is to make a program I've written easily accessible to potential employers/etc. in order to... showcase my skills.. or whatever. I am not a computer scientist, and I've never written a python module meant for installation before, so I'm new to this aspect.

我已经编写了一种机器学习算法,并将参数拟合到我本地拥有的数据中.我想分发带有默认"参数的算法,以便下载程序可以开箱即用"使用它进行分类,而无需训练集.我已经编写了一些方法来保存参数以从文本文件中加载参数/从文本文件中加载参数,这些方法已在平台上得到证实.我可以简单地要求用户下载我单独提到的文件,并使用我创建的loadParameters方法来手动加载参数,但是对于希望评估我的人,我想使安装过程尽可能地容易.

I've written a machine learning algorithm, and fit parameters to data that I have locally. I would like to distribute the algorithm with "default" parameters, so that the downloader can use it "out of the box" for classification without having a training set. I've written methods which save the parameters to/load the parameters from text files, which I've confirmed work on my platform. I could simply ask users to download the files I've mentioned seperately and use the loadParameters method I've created to manually load the parameters, but I would like to make the installation process as easy as possible for people who may be evaluating me.

我不确定如何打包文本文件,以便可以将它们自动加载到我拥有的对象的__init__方法中.

What I'm not sure is how to package the text files in such a way that they can automatically be loaded in the __init__ method of the object I have.

我已将算法和文件放在github 此处上,并编写了setup.py脚本,以便可以使用如下pip从github下载它: pip install --upgrade https://github.com/NathanWycoff/SySE/tarball/master 但是,这似乎并不会安装包含我需要的数据的文本文件,而只会安装包含我的代码的__init__.py python文件.

I have put the algorithm and files on github here, and written a setup.py script so that it can be downloaded from github using pip like this: pip install --upgrade https://github.com/NathanWycoff/SySE/tarball/master However, this doesn't seem to install the text files containing the data I need, only the __init__.py python file containing my code.

所以我想这个问题可以归结为:我如何强制pip除了下载__init__.py中的模块以外,还要下载其他文件?还是有更好的方法来加载默认参数?

So I guess the question boils down to: How do I force pip to download additional files aside from just the module in __init__.py? Or, is there a better way to load default parameters?

推荐答案

是的,还有更好的方法,如何使用python包分发数据文件.

Yes, there is a better way, how you can distribute data files with python package.

首先,阅读有关正确的python包结构的知识.例如,不建议将代码放入__init__文件中.他们只是标记目录为python包,此外您还可以在其中执行一些import语句.因此,最好将SySE类放入(例如)该目录中的文件syse.py中,并且在__init__.py中,您可以from .syse import SySE.

First of all, read something about proper python package structure. For instance, it's not recommended to put a code into __init__ files. They're just marking that a directory is a python package, plus you can do some import statements there. So, it's better, if you put your SySE class to (for instance) file syse.py in that directory and in __init__.py you can from .syse import SySE.

到数据文件.默认情况下,setuptools将仅分发*.py和其他几个特殊文件(READMELICENCE等).但是,您可以告知setuptools您要使用该软件包分发一些其他文件.使用setup的kwarg package_data,有关此处的更多信息.另外,请不要忘记将所有数据文件都包含在MANIFEST.in中,有关更多信息,请参见此处.

To the data files. By default, setuptools will distribute only *.py and several other special files (README, LICENCE and so on). However, you can tell to setuptools that you want distribute some other files with the package. Use setup's kwarg package_data, more about that here. Also don't forget to include all you data file into MANIFEST.in, more on that here.

如果正确地完成了上述所有操作,则可以使用包pkg_resources在运行时发现数据文件. pkg_resources处理所有可能的情况-您的包可以通过多种方式分发,可以从pip服务器安装,也可以从转轮安装,如鸡蛋,...更多关于

If you do all the above correctly, than you can use package pkg_resources to discover your data files on runtime. pkg_resources handles all possible situations - your package can be distributed in several ways, it can be installed from pip server, it can be installed from wheel, as egg,...more on that here.

最后,如果您的软件包是公开的,我只能建议将其上传到 pypi (以防万一)它不是公开的,您可以运行自己的pip服务器).在此注册并上传您的包裹.您只能做pip install syse来从任何地方安装它.很有可能是分发包裹的最佳方法.

Lastly, if you package is public, I can only recommend to upload it on pypi (in case it is not public, you can run your own pip server). Register there and upload your package. You could than do only pip install syse to install it from anywhere. It's quite likely the best way, how to distribute your package.

需要大量的工作和阅读,但是我敢肯定,您会从中受益.

It's quite a lot work and reading but I'm pretty sure you will benefit from it.

希望获得帮助.

这篇关于使用pip/其他安装程序安装后在python模块中加载文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆