{"id":304,"date":"2019-11-29T12:39:02","date_gmt":"2019-11-29T03:39:02","guid":{"rendered":"http:\/\/localhost:8000\/?p=304"},"modified":"2021-01-23T12:26:03","modified_gmt":"2021-01-23T03:26:03","slug":"python-module-import","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/11\/python-module-import.html","title":{"rendered":"python\u306b\u304a\u3051\u308b\u89aa\/\u5144\u5f1f\u968e\u5c64\u306e\u30e2\u30b8\u30e5\u30fc\u30ebimport\u306e\u65b9\u6cd5"},"content":{"rendered":"

python\u3067\u306f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u5b9f\u884c\u30d5\u30a9\u30eb\u30c0\u304a\u3088\u3073\u305d\u306e\u914d\u4e0b\u306e\u30d5\u30a9\u30eb\u30c0\u3060\u3051\u304c\u30e2\u30b8\u30e5\u30fc\u30eb\u691c\u7d22\u5bfe\u8c61\u306e\u30d1\u30b9\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002
\n\u305d\u306e\u305f\u3081\u3001\u3042\u308b\u7a0b\u5ea6\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304c\u5927\u304d\u304f\u306a\u3063\u3066\u304d\u3066\u3001\u5171\u901a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u7279\u5b9a\u306e\u30d5\u30a9\u30eb\u30c0\u306b\u6574\u7406\u3057\u3066\u307e\u3068\u3081\u305f\u308a\u3059\u308b\u3068\u3001\u30d1\u30b9\u304c\u901a\u3089\u305a\u56f0\u3063\u305f\u308a\u3057\u307e\u3059\u3002
\n\u4eca\u56de\u306f\u3001\u5144\u5f1f\u968e\u5c64\u306e\u5171\u901a\u30e2\u30b8\u30e5\u30fc\u30eb\u306eimport\u65b9\u6cd5\u3092\u8aac\u660e\u3057\u307e\u3059\uff08\u3082\u3061\u308d\u3093\u89aa\u968e\u5c64\u3067\u3082\u540c\u3058\u65b9\u6cd5\u3067\u5bfe\u5fdc\u3067\u304d\u307e\u3059\uff09\u3002<\/p>\n

\u306f\u3058\u3081\u306b<\/h2>\n

\u30d5\u30a9\u30eb\u30c0\u69cb\u6210<\/h3>\n

\u4eca\u56de\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30d5\u30a9\u30eb\u30c0\u69cb\u6210\u3067main.py<\/code>\u306blogger.py<\/code>\u3092import\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n

\u2523 module\n\u2503\u3000\u2517 logger.py\n\u2517 business\n\u3000\u3000\u2517 main.py<\/code><\/pre>\n

logger.py\u306e\u5b9f\u88c5<\/h3>\n
import logging\n\nformatter = '%(asctime)s [%(levelname)s] %(message)s'\nlogging.basicConfig(level=logging.DEBUG, format=formatter)\n\ndef info(message: str, *args, **kwargs):\n    logging.info(message, *args, **kwargs)<\/code><\/pre>\n

main.py\u306e\u5b9f\u88c5<\/h3>\n
from module import logger\n\ndef main():\n    logger.info('module imported successfully.')\n\nif __name__ == "__main__":\n    main()<\/code><\/pre>\n

\u554f\u984c\u70b9<\/h3>\n

module<\/code>\u30d5\u30a9\u30eb\u30c0\u304c\u30e2\u30b8\u30e5\u30fc\u30eb\u691c\u7d22\u30d1\u30b9\u306b\u5165\u3063\u3066\u306a\u3044\u306e\u3067\u3001ModuleNotFoundError<\/code>\u304c\u767a\u751f\u3057\u307e\u3059<\/p>\n

$ python main.py\nTraceback (most recent call last):\n  File "main.py", line 4, in <module>\n    from module import logger\nModuleNotFoundError: No module named 'module'<\/code><\/pre>\n

\u2460\u30b7\u30f3\u30dc\u30ea\u30c3\u30af\u30ea\u30f3\u30af\u3092\u5229\u7528<\/h2>\n

\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u304c\u914d\u7f6e\u3055\u308c\u3066\u3044\u308bbusiness<\/code>\u30d5\u30a9\u30eb\u30c0\u306b\u3001module<\/code>\u30d5\u30a9\u30eb\u30c0\u3078\u306e\u30b7\u30f3\u30dc\u30ea\u30c3\u30af\u30ea\u30f3\u30af\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n

$ cd business\n$ ln -s ..\/module module\n$ ls -la \n-rw-r--r--  1 hoge  staff  147 11 29 09:44 main.py\nlrwxr-xr-x  1 hoge  staff    9 11 29 10:00 module -> ..\/module<\/code><\/pre>\n

\u3053\u306e\u72b6\u614b\u3060\u3068\u3001\u554f\u984c\u306a\u304f\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/p>\n

$ python main.py\n2019-11-29 10:02:19,624 [INFO] module imported successfully..<\/code><\/pre>\n

\u3053\u306e\u65b9\u6cd5\u306e\u7279\u5fb4\u306f<\/p>\n