LoginSignup
5

More than 1 year has passed since last update.

posted at

updated at

pythonインタプリタ起動時の決まった処理の追加

環境

  • windows 10 pro
  • python 3.9.0

目的

pythonのインタプリタで起動するたびimport文を書くのが面倒であったため
起動と同時に自動でインポートをしてくれる設定の方法を調べました。

やりかた

homeのディレクトリ(C:\Users\<username>\)とかに以下のようなファイルを作成します

.pythonstartup.py
from datetime import datetime
date = datetime.now()

とか記述しておいて
環境変数に以下を設定

setx PYTHONSTARTUP C:\Users\<username>\.pythonstartup.py

あとは、コマンドプロンプトにpythonと打つだけでファイルの内容が実行されます。

>python
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
2020-12-23 20:35:52.167887
>>> 

pythonstartup.pyのファイルにいつもよく使うようなモジュールのインポート文を書いておくと便利です。
あとは環境変数でテスト中のモジュールとかのパスを設定してしまえばテストしかするときも便利です

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
What you can do with signing up
5