如下代码:

import os

from utils.file_reader import YamlReader

BASE_PATH = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
CONFIG_FILE = os.path.join(BASE_PATH, 'config', 'config.yml')
DATA_PATH = os.path.join(BASE_PATH, 'data')
DRIVER_PATH = os.path.join(BASE_PATH, 'drivers')
LOG_PATH = os.path.join(BASE_PATH, 'log')
REPORT_PATH = os.path.join(BASE_PATH, 'report') class Config:
def __init__(self, config = CONFIG_FILE):
self.config = YamlReader(config).data def get(self, element, index=0):
return self.config[index].get(element)
#coding = UTF-8

import os
import yaml
from xlrd import open_workbook from config.configg import CONFIG_FILE class YamlReader:
def __init__(self, yamlf):
if os.path.exists(yamlf): # 判断文件是否存在,如果文件不存在,则提示文件不存在并退出
self.yamlf = yamlf
else:
raise FileNotFoundError('文件不存在')
self._data = None @property
def data(self):
# _data只能在本类中调用
# 如果是第一次调用data,读取yaml文档,否则直接返回之前保存的数据
if not self._data:
with open(self.yamlf, 'rb') as f:
self._data = list(yaml.safe_load_all(f)) # load后是个generator,用list组织成列表
return self._data class SheetTypeError(Exception):
pass class ExcelReader:
"""
读取excel文件中的内容。返回list。 如:
excel中内容为:
| A | B | C |
| A1 | B1 | C1 |
| A2 | B2 | C2 | 如果 print(ExcelReader(excel, title_line=True).data),输出结果:
[{A: A1, B: B1, C:C1}, {A:A2, B:B2, C:C2}] 如果 print(ExcelReader(excel, title_line=False).data),输出结果:
[[A,B,C], [A1,B1,C1], [A2,B2,C2]] 可以指定sheet,通过index或者name:
ExcelReader(excel, sheet=2)
ExcelReader(excel, sheet='BaiDuTest')
"""
def __init__(self, excel, sheet=0, title_line=True):
if os.path.exists(excel):
self.excel = excel
else:
raise FileNotFoundError('excel文件不存在!')
self.sheet = sheet
self.title_line = title_line
self._data = list() @property
def data(self):
if not self._data:
workbook = open_workbook(self.excel)
if type(self.sheet) not in [int, str]:
raise SheetTypeError('Please pass in <type int> or <type str>, not {0}'.format(type(self.sheet)))
elif type(self.sheet) == int:
s = workbook.sheet_by_index(self.sheet) if self.title_line:
title = s.row_values(0) # 首行为title
for col in range(1, s.nrows):
# 依次遍历其余行,与首行组成dict,拼到self._data中
self._data.append(dict(zip(title, s.row_values(col))))
else:
for col in range(0, s.nrows):
# 遍历所有行,拼到self._data中
self._data.append(s.row_values(col))
return self._data if __name__ == '__main__':
y = CONFIG_FILE
reader = YamlReader(y)
print(reader.data)
def __init__(self, excel, sheet=0, title_line=True):
if os.path.exists(excel):
self.excel = excel
else:
raise FileNotFoundError('excel文件不存在!')
self.sheet = sheet
self.title_line = title_line
self._data = list() @property
def data(self):
if not self._data:
workbook = open_workbook(self.excel)
if type(self.sheet) not in [int, str]:
raise SheetTypeError('Please pass in <type int> or <type str>, not {0}'.format(type(self.sheet)))
elif type(self.sheet) == int:
s = workbook.sheet_by_index(self.sheet) if self.title_line:
title = s.row_values(0) # 首行为title
for col in range(1, s.nrows):
# 依次遍历其余行,与首行组成dict,拼到self._data中
self._data.append(dict(zip(title, s.row_values(col))))
else:
for col in range(0, s.nrows):
# 遍历所有行,拼到self._data中
self._data.append(s.row_values(col))
return self._data if __name__ == '__main__':
y = CONFIG_FILE
reader = YamlReader(y)
print(reader.data)

第二段代码运行时提示ImportError: cannot import name 'CONFIG_FILE'

在网上查了不少资料,最终确定是因为循环导入的原因,只要推迟进口就解决了,第二段代码修改如下:

#coding = UTF-8

import os
import yaml
from xlrd import open_workbook class YamlReader:
... ... if __name__ == '__main__':
from config.configg import CONFIG_FILE # 由最上面导入挪到此处就可以了
y = CONFIG_FILE
reader = YamlReader(y)
print(reader.data)

参考资料:https://stackoverflow.com/questions/1556387/circular-import-dependency-in-python

代码参考:http://blog.csdn.net/huilan_same/article/details/76572466

Python3.6 运行提示 ImportError: cannot import name 'CONFIG_FILE'的更多相关文章

  1. python 脚本中使用了第三方openpyxl 打包程序运行提示ImportError:cannot import name __version__

    最近写了一个脚本,脚本中使用了第三方openpyxl(openpyxl是使用 pip install openpyxl 下载的),先是使用py2exe打包程序,打包过程中提示很多文件没有包含,在没有仔 ...

  2. python import eventlet包时提示ImportError: cannot import name eventlet

    root@zte-desktop:/home/ubuntu/python-threads# cat eventlet.py #!/usr/bin python import eventlet from ...

  3. 关于python pip安装第三方库 jieba 中文分词工具后提示"ImportError: cannot import name 'Random'"报错问题

    具体错误提示如下: >>> import jieba Traceback (most recent call last): File "<stdin>" ...

  4. django 提示ImportError: cannot import name json_response

    from json_response import JsonResponse, json_response as json_resp 使用的语句如上,其实并不是没有安装,只是需要升级一下 pip in ...

  5. 解决ImportError: cannot import name HTTPConnection的方法

    在写python程序的时候,使用from httplib import HTTPConnection,在run的时候提示ImportError: cannot import name HTTPConn ...

  6. ImportError: cannot import name gof

    今天打开spyder说调试一个theano程序,但是import theano提示 ImportError: cannot import name gof 最后解决方案 pip install --u ...

  7. win7 32位 安装opencv-python后,运行时提示 "from .cv2 import *: DLL load failed: 找不到指定的模块" 的解决办法

    安装opencv后,运行一个测试程序提示"from .cv2 import *: DLL load failed: 找不到指定的模块".于是百度一下解决办法,结果试了N多方法后也没 ...

  8. python 运行脚本报错 from keyword import iskeyword as _iskeyword ImportError: cannot import name iskeyword,说明python环境坏了,得重装,尚不知具体原因,

    C:\Python27\Scripts>python task_test.pyTraceback (most recent call last):  File "task_test.p ...

  9. import cx_Oracle报错,提示importError: DLL load failed: 不是有效的Win32程序。

    问题说明1:WIN32,python是2.7版本,本地oracle client是32位的.import cx_Oracle报错,提示importError: DLL load failed: 该模块 ...

随机推荐

  1. [ROS]激光驱动安装

    参考资料: https://blog.csdn.net/hongliang2009/article/details/73302986 https://blog.csdn.net/bohaijun_12 ...

  2. 【LeetCode每天一题】Set Matrix Zeroes(设置0矩阵)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...

  3. canal 代码阅读

    涉及到有边界队列,无边界队列.poolSize.corePoolSize.maximumPoolSize 三者参数含义 If there are more than corePoolSize but ...

  4. Google搜索中的突变XSS-JavaScript Library Introduced XSS Flaw in Google Search

    前言2018年9月26日,开源Closure库(最初由谷歌创建并用于谷歌搜索)的一名开发人员创建了一个提交,删除了部分输入过滤.据推测,这是因为开发人员在用户界面设计方面出现了问题.但此次提交的开发人 ...

  5. Chrome Inspect调试微信出现空白页面的解决方法

    首先,需要打开手机的USB调试和微信的TBS 调试开关. 如果不打开TBS开关,Inspect时会检测不到任何微信的H5页面 使用微信扫码下方二维码,打开TBS调试开关: 普通网页: 小程序: 微信扫 ...

  6. LNMP分离式部署

    #### LNMP组合工作流程 在LNMP组合工作时,首先是用户通过浏览器输入域名请求Nginx Web服务,如果请求是静态资源,则由Nginx解析返回给用户:如果是动态请求(.php结尾),那么Ng ...

  7. Python学习笔记(Ⅱ)——循环/选择/函数

    一.循环结构 python中提供了for循环和while循环两种操作,没有do……while语句. 1.for循环: 与其他语言中for循环的常见的写法如for (int i=0;i<10;i+ ...

  8. 微信公众平台开发教程(一)_微信接入校验以及token获取

    微信公众平台是运营者通过公众号为微信用户提供资讯和服务的平台,而公众平台开发接口则是提供服务的基础. 接入微信公众平台开发,开发者需要按照如下步骤完成: 1.填写服务器配置 2.验证服务器地址的有效性 ...

  9. 使用cocos creator的过程中碰到的问题记录

    1>编辑器不能识别脚本里面@property类型,显示为null,脚本拖不上去 是@property的类循环引用导致的,可以改变组件类型到cc.Node解决 2> Cannot read ...

  10. 小程序wx:for Do not set same key \"NaN\" in wx:key.

    在使用wx:for的时候出现了Do not set same key \"NaN\" in wx:key. 去网上查看资料,说是使用wx:key 试了一下,没用 字面意思是不要设置 ...