【问题标题】:Python 2.7 extremelly persisting errorPython 2.7 极其持久的错误
【发布时间】:2023-03-05 12:44:01
【问题描述】:

我有这个模块

import os
import sys

sys.path.append("C:\pysec-master")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pysec-master.settings")

from pysec.models import *

"""get a file from the index. it may or may not be present on our hard disk. if it's not, it will be downloaded
the first time we try to access it, or you can call .download() explicitly"""
filing = Index.objects.filter(form='10-K',cik=1090872).order_by('-date')[0]

print filing.name

"""initialize XBRL parser and populate an attribute called fields with a dict of 50 common terms"""
x = latest.xbrl()

print x.fields['FiscalYear']

print x.fields

"""fetch arbitrary XBRL tags representing eiter an Instant or a Duration in time"""
print 'Tax rate', x.GetFactValue('us-gaap:EffectiveIncomeTaxRateContinuingOperations','Duration')

if x.loadYear(1): 
    """Most 10-Ks have two or three previous years contained in them for the major values. This call switches the contexts
    to the prior year (set it to 2 or 3 instead of 1 to go back further) and reloads the fundamental concepts.
    Any calls to GetFactValue will use that year's value from that point on."""

    print x.fields['FiscalYear']

    print x.fields

    print 'Tax rate', x.GetFactValue('us-gaap:EffectiveIncomeTaxRateContinuingOperations','Duration')

我现在正在尝试运行它一周,每当我按 F5 运行它时,我都会收到此错误:

Traceback (most recent call last):
  File "C:\pysec-master\pysec\example.py", line 8, in <module>
    from pysec.models import *
  File "C:\pysec-master\pysec\models.py", line 4, in <module>
    from django.db import models
  File "C:\Python27\lib\site-packages\django\db\models\__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 17, in <module>
    from django.db.models.deletion import Collector
  File "C:\Python27\lib\site-packages\django\db\models\deletion.py", line 4, in <module>
    from django.db.models import signals, sql
  File "C:\Python27\lib\site-packages\django\db\models\sql\__init__.py", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "C:\Python27\lib\site-packages\django\db\models\sql\subqueries.py", line 12, in <module>
    from django.db.models.sql.query import Query
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 22, in <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "C:\Python27\lib\site-packages\django\db\models\sql\aggregates.py", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 49, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 132, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'pysec-master.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named pysec-master.settings

如果您需要项目中的任何其他模块,请告诉我,我将编辑我的问题并将其放入。项目的原始文件是 here

项目名为pysec-master,位于此目录:C:\pysec-master

项目的结构是这样的

C:\pysec-master(file)
|__C:\pysec-master\local_settings.py
|__C:\pysec-master\manage.py
|__C:\pysec-master\settings.py
|__C:\Python27\pysec(file)
   |__C:\pysec-master\pysec\__init__.py
   |__C:\pysec-master\pysec\example.py
   |__C:\pysec-master\pysec\models.py
   |__C:\pysec-master\pysec\xbrl.py
   |__C:\pysec-master\pysec\xbrl_fundamentals.py

我的环境变量是:

Path --> C:\Python27;C:\Python27\Scripts;C:\pysec-master

PYTHONPATH --> C:\pysec-master;C:\;

当我输入sys.path 我得到

['C:\\Python27\\Lib\\idlelib', 'C:\\pysec-master', 'C:\\pysec-master\\pysec', 

'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 

'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 

'C:\\Python27\\lib\\site-packages', 'C:\\pysec-master']

我已经检查过:

  • 要符合的环境变量
  • 设置DJANGO_MODULE_SETTINGS

那么问题是什么,我该如何解决?

【问题讨论】:

  • 您的C:\ 条目似乎没有从PYTHONPATH 传播到sys.path
  • 使用前不需要先导入设置文件吗?
  • 你的意思是我应该拥有C:` in both PYTHONPATH` 和sys.path
  • @TankorSmash 哇,你的意思是我应该像from pysec import settings 这样写吗??
  • @TankorSmash 每当我尝试在 GUI 中输入 from pysec import settings 时,它都会给我一个错误提示:cannot import name settings

标签: python django import path


【解决方案1】:

您的DJANGO_SETTINGS_MODULE 环境变量设置为pysec-master.settings,这是错误的,原因有两个:

  • pysec-master 不是有效的 Python 包名(不允许使用 -)。
  • 如果它是一个有效的包名,它不会作为一个包被找到(即使你的PYTHONPATH 上有C:)因为它没有__init__.py

对于您的配置,DJANGO_SETTINGS_MODULE 可能应该只是 settings

【讨论】:

    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多