0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

とにかくPythonでインポートできるようにしたい

Posted at

Pythonでインポートできなくて困っている

Pythonで開発していてパッケージを分けていると、少しディレクトリ構成を変更するとインポートできなくなって困ることがあります。
そんな時「モジュール探索パスを追加して絶対インポート」することで、カレントディレクトリがどこであっても正しくインポートできます。

既存のインポート文の前に以下を記述しましょう。

import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

参考

Pythonの相対インポートで上位ディレクトリ・サブディレクトリを指定

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?