Django错误:ModuleNotFoundError:没有名为'ofac'的模块

时间:2018-06-18 09:04:05

标签: python django

我有一个我必须要做的旧项目。在运行项目时,我得到了No module error

我没有在虚拟环境中运行项目。我正在我的MAC上运行它。我正在运行Django 2或更多。

请参阅下面的文件夹结构图。希望能帮助到你。

enter image description here

Traceback (most recent call last):
  File "/Users/Dropbox/ofac_project/ofac_sdn/import_save/import_all_sdn.py", line 9, in <module>
    django.setup()
  File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'ofac'

这是我想要运行的代码:

import csv, sys, os

# project_dir = "/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/import_save/"
project_dir = "/Users/Dropbox/ofac_project/ofac_sdn/import_save/"
sys.path.append(project_dir)
os.environ['DJANGO_SETTINGS_MODULE']='ofac.settings'

import django
django.setup()

from ofac_sdn.models import Ofac_Sdn
from ofac_sdn.models import Ofac_Add
from ofac_sdn.models import Ofac_Alt
from ofac_sdn.models import Ofac_Sdn_Comments


file    = 'new_sdn.csv'
file1   = 'new_add.csv'
file2   ='new_alt.csv'
file3   ='new_sdn_comments.csv'

data = csv.reader(open(file),delimiter=",")
for row in data:
    if row[0] !="Number":
        post = Ofac_Sdn()
        post.number = row[0]
        post.name = row[1]
        post.b_i=row[2]
        post.programe=row[3]
        post.more_info=row[4]
        post.vessel_call_sign=row[5]
        post.vessel_type=row[6]
        post.vessel_dwt=row[7]
        post.tonnage=row[8]
        post.vessel_flag=row[9]
        post.vessel_owner=row[10]
        post.dob_aka=row[11]
        post.save()

data1 = csv.reader(open(file1),delimiter=",")
for row in data1:
    if row[0] !="Number":
        post = Ofac_Add()
        post.number_id = row[0]  #am pus un _id la foreign key k asa stokeaza django foreign key-ul
        post.n = row[1]
        post.adresa=row[2]
        post.oras=row[3]
        post.stat=row[4]
        post.s=row[5]
        post.save()

data2 = csv.reader(open(file2),delimiter=",")
for row in data2:
    if row[0] !="Number":
        post = Ofac_Alt()
        post.number_id = row[0]
        post.ceva = row[1]
        post.aka=row[2]
        post.name_of_aka=row[3]
        post.a=row[4]
        post.save()

data3 = csv.reader(open(file3),delimiter=",")
for row in data3:
    if row[0] !="Number":
        post = Ofac_Sdn_Comments()
        post.number_id = row[0]
        post.more_info = row[1]
        post.save()

1 个答案:

答案 0 :(得分:2)

看起来您正在向项目路径添加错误的目录。要使用ofac.settings,您需要将项目目录设置为Users/Dropbox/ofac_project(包含manage.pyofac的目录。

project_dir = "/Users/Dropbox/ofac_project"
sys.path.append(project_dir)
os.environ['DJANGO_SETTINGS_MODULE']='ofac.settings'

import django
django.setup()