LoginSignup
34
31

More than 5 years have passed since last update.

【Python】自作モジュールへのパスの通し方

Last updated at Posted at 2018-08-02

方法1..bash_profileにパスを記入

複数の実行ファイルで使いたいときに有用

.bash_profile
export PYTHONPATH='<自作モジュールの置いてあるディレクトリパス>'

.bash_profile はログイン時にのみ実行される。ログイン状態で設定を反映される場合はsource .bash_profileをコマンド入力

方法2.デフォルトのパッケージディレクトリに配置

複数の実行ファイルで使いたいときに有用

import sys
print(sys.path)

で表示されるディレクトリ下に自作パッケージを配置する

方法3 実行ファイルごとにパスを追加

少数の実行ファイルで使いたいときに有用
自作モジュールをimportする前に以下2行を追加

import sys
sys.path.append("<自作モジュールの置いてあるディレクトリパス>")
34
31
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
34
31