0

import cannot find same file.Now,I have a,b,c files in folder A. File a read b&c ones.First,I wrote in file a like

import b
import c
   ・
  ・
  ・   
x = b_method()
y = c_method()

But,error happens "No module named b".Next,I wrote in file a like

from A import b
from A import c
   ・
     ・
     ・   
x = b_method()
y = c_method()

But,when I run this file,terminal said

ImportError: No module named 'A'     

So,how can I read file b&c in file a? How can I fix this? I added

import os
import sys
file_dir = os.path.dirname(__file__)
sys.path.append(file_dir) 

to file a,but it does not work.

My project structure is

test   (parent app)
|--- A
      |
      ---- a
      ---- b
      ---- c

Traceback says

Traceback (most recent call last):
  File "a.py", line 10, in <module>
    from A import b
ImportError: No module named 'A'

When i wrote import b, Traceback says

Traceback (most recent call last):
  File "a.py", line 58, in <module>
    xxx = b.parse(user, id)
NameError: name 'b' is not defined

Whole a.py is

import sys
import os
import b
import c


LOG_FILENAME = 'excel.log'
file_dir = os.path.dirname(__file__)
sys.path.append(file_dir)

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

handler = logging.handlers.RotatingFileHandler(
    LOG_FILENAME, maxBytes=2000000, backupCount=5, encoding='utf-8')
handler.setFormatter(
    logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s'))
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)

console = logging.StreamHandler()
console.setLevel(logging.INFO)
logger.addHandler(console)

# READ
def read_config():
    f = codecs.open('config.json', 'r', 'utf-8')
    text = f.read()
    config = json.loads(text)
    return config

try:
    logger.debug('start.')
    logger.info("--------------------------------")
    logger.info("Excelファイル読込処理を開始します。")
    logger.info("--------------------------------")
    config = read_config()
    output_dir = config['output_dir']

    logger.info("クライアントのデータを読み込みます。")
    book_name = config['client']['file_name']
    regions = config['client']['regions']
    #clientsが空だと、jsonがoutputされない
    xxx = b.parse(user, id)
    yyy = c.parse(name,mail)
    logger.info("--------------------------------")

# ERROR
except:
    logger.exception('exception occurred.')
    print(input("error"))
    sys.exit(-1)

logger.debug('end.')
print(input("finish"))
sys.exit(0)
user8504021
  • 303
  • 5
  • 23

2 Answers2

0

Make sure of two things: -- The files you are trying to import are in the same folder/directory as the file you are working in -- The files are something you can use with python (a .py file is what you should be using)

After making sure of that try this on idle:

>>> import b
>>> import c
>>> c
>>> b

After doing that python should give you a message telling you about the specific modules

0

Making an empty file __init__.py in the directory and Python can identift directory from which you're allowed to import. Then you use

import b
import c
GAVD
  • 1,977
  • 3
  • 22
  • 40
  • thx,ur comments.I added __init__.py into folder A,but same error happened when i wrote from A import b & import b.How can I do for next? – user8504021 Aug 23 '17 at 07:01