Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建python库:如何编写_Python - Fatal编程技术网

创建python库:如何编写

创建python库:如何编写,python,Python,我编写了一个小型Python库,目前托管在。如您所见,该库名为pygpstools,由5个文件组成: gpstime.py→ 班级 satellite.py→ 班级 大地测量学.py→ 具有一些大地测量方法的模块 almanacs.py→ 带有一些历书方法的模块 constants.py→ 一些常数 我想在自述中使用它。例如: from pygpstools import GPSTime GPSTime(wn=1751, tow=314880) 或: 但是在使用命令python setup

我编写了一个小型Python库,目前托管在。如您所见,该库名为
pygpstools
,由5个文件组成:

  • gpstime.py
    → 班级
  • satellite.py
    → 班级
  • 大地测量学.py
    → 具有一些大地测量方法的模块
  • almanacs.py
    → 带有一些历书方法的模块
  • constants.py
    → 一些常数
我想在自述中使用它。例如:

from pygpstools import GPSTime
GPSTime(wn=1751, tow=314880)
或:

但是在使用命令
python setup.py install安装了我的库之后,当我尝试像这样访问
GPSTime
类时,我得到了
ImportError

我想问题出在
\uuuu init\uuuu.py
文件中。当我在PythonIRC频道上问到这一点时,有人告诉我,让它为空就行了。但我已经研究过了,它似乎只告诉Python它是一个模块,但它不足以允许我正在寻找的这种导入,就像其他任何库一样

因此,我尝试(当前未在bitbucket更新)将其用作
\uuuu init\uuuuu.py

__title__ = 'pygpstools'
__version__ = '0.1.1'
__author__ = 'Roman Rodriguez'
__license__ = 'MIT'
__copyright__ = 'Copyright 2013 Roman Rodriguez'


import almanacs
import constants
import geodesy
import gpstime
import satellite
但仍然不起作用:
ImportError
for
GPSTime


我缺少什么?

GPSTime
,例如,它位于模块
GPSTime
中,因此它的实际(相对)名称是
GPSTime.GPSTime
。因此,当您在
\uuuu init\uuuu
中导入
gpstime
时,实际上是在提供名称
gpstime
,该名称包含对您的类型的引用,即
gpstime.gpstime

因此,您必须使用pygpstools import gpstime中的
,然后使用
gpstime.gpstime
作为类型名称

显然,这不是您想要的,因此您希望在
\uuuu init\uuu
模块中“收集”所有类型。您可以通过直接提供它们来做到这一点:

from almanacs import *
from constants import *
from geodesy import *
from gpstime import GPSTime
from satellite import * 

我现在使用
*
导入任何内容,因为我没有仔细查看文件中的实际类型。不过,您应该指定它。还建议您在
\uuu init\uuuuu
中定义一个
\uuu all\uuuuuu
列表,以便您可以控制在从pygpstools import*
GPSTime
写入
时导入的名称,例如,它位于模块
GPSTime
中,因此它的实际(相对)名称是
GPSTime.GPSTime
。因此,当您在
\uuuu init\uuuu
中导入
gpstime
时,实际上是在提供名称
gpstime
,该名称包含对您的类型的引用,即
gpstime.gpstime

因此,您必须使用pygpstools import gpstime中的
,然后使用
gpstime.gpstime
作为类型名称

显然,这不是您想要的,因此您希望在
\uuuu init\uuu
模块中“收集”所有类型。您可以通过直接提供它们来做到这一点:

from almanacs import *
from constants import *
from geodesy import *
from gpstime import GPSTime
from satellite import * 

我现在使用
*
导入任何内容,因为我没有仔细查看文件中的实际类型。不过,您应该指定它。还建议您在
\uuu init\uuuuuuuuuuu
中定义一个
\uuuuu all\uuuuuuuuuu
列表,以便您可以控制在从pygpstools import*
写入
时导入哪些名称,这些名称是相对导入的。它们在python3中不起作用,并且每当您使用具有不同工作目录的库时。从中使用
。导入历书来解决这个问题。@Bakuriu在python2.7中也可以吗?无论如何,我已经尝试了2.7版本,当我尝试使用“from pygpstools import GPSTime”时,我也遇到了同样的问题,我误解了你的错误。我认为错误是在导入
gpstime
模块时引起的,而不是导入
gpstime
类。为此,请使用:
从.gsptime导入*
中的
从.gsptime导入*
,或
从.gsptime导入gsptime
。这些是相对导入。它们在python3中不起作用,并且每当您使用具有不同工作目录的库时。从中使用
。导入历书来解决这个问题。@Bakuriu在python2.7中也可以吗?无论如何,我已经尝试了2.7版本,当我尝试使用“from pygpstools import GPSTime”时,我也遇到了同样的问题,我误解了你的错误。我认为错误是在导入
gpstime
模块时引起的,而不是导入
gpstime
类。要执行此操作,请使用:
从.gsptime导入*
中的
从.gsptime导入
,或
从.gsptime导入gsptime