4
5

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 5 years have passed since last update.

[Python3] 環境変数の追加と呼び出し

Last updated at Posted at 2018-05-30

最近twitterAPIを利用したプログラムを書いてみました.その時に参考にしたプログラムでは,キーをベタ打ちしていたのですが,どうやらセキュリティの観点からはキーを環境変数にいれ,よびだすほうが良いみたいです.たしかに,うっかりキーの書いてあるソースコードをgithubなどに上げてしまった時は面倒なことになります.

環境変数の追加

Ubuntuであればbashrcに下記のように追記すればよいです.研究室の計算機はなぜかtcshを使用しているので,ついでに書いておきます.

bashrc
export KEY=***********
tcshrc
setenv KEY *********** 

Pythonでよびだす

Pythonで環境変数を呼び出すにはosモジュールを使用します.

Python3
import os

print(os.environ.get('KEY'))

これで環境変数KEYが呼び出せました.

参考

環境変数の取得 (environ) | Python-izm
シェルの基本操作法(後編3:シェル変数と環境変数)

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?