LoginSignup
52
61

More than 5 years have passed since last update.

Pythonで自作モジュールを手軽に使う

Posted at

Target

自作モジュールを作ったがPATHが通せない人

Main

自作モジュールを/home/users/mymoduleに作ったとします。
この下に、test.pyがあるとします。

test.py
def print_test():
    print("testだよーん")

自作モジュールを利用したい先で、下のようなimportをします

main_python.py
import sys
sys.path.append("/home/users/mymodule")
import test

このプログラムの中でモジュールを呼び出すには、test.print_test()とするだけです。

自作モジュールを利用するたびに初めの3行が必要になります。
ですが、PYTHONPATHにつっこむより恐らく簡単で、確実。
pyenvなどなど環境によってモジュールの読み出しPATHが変わってしまうので地道且つ簡単な方法だと思います。

52
61
12

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
52
61