1
0

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.

sys.path.append('..')で自作ライブラリがimportできない時の対処法

Posted at

#対処法

親ディレクトリに自作ライブラリが入ったディレクトリがあり、そこからimportしたい時

..
|--xx
|  |-xxx.py
|
|-sample
   |-samp.py  

よくある記事ではこのようにしているがエラーが出てしまう場合がある↓

import sys
sys.path.append("..")
from sample import samp

#実行結果
%python3 xxx.py
ImportError: cannot import name 'samp' from 'sample'

そういう時、以下のようにするとうまく行く

import sys
sys.path.append("../sample")
from sample import samp

sys.pathに絶対パスや相対パスなど色々追加して試したが、結局どれもダメで
自作ライブラリのディレクトリを直接追加した時だけうまくいった

誰かの参考になれば嬉しいです

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?