LoginSignup
0
1

More than 3 years have passed since last update.

pycharmでpython+subprocessが上手く動かないときの対処方法

Posted at

subprocessを使ってCLIコマンドを実行するpythonプログラムがterminal/コマンドプロンプトからは動くのにpycharmから実行すると動かない、という場合の対処方法をメモしておきます。

試行環境

Windows10
PyCharm2018.1.1 community edition
python3.6

起こったこと

CLIで動作するkaggle APIをpythonのsubprocessを使って操作するスクリプトを作成してpycharmで実行しようとしたんですがエラーが出て動きませんでした。

python
import subprocess

text = subprocess.check_output(['kaggle', 'competitions', 'list'], shell=True)
text = text.decode('cp932')
print(text)
pycharmで実行した結果
'kaggle' �́A�����R}���h�܂��͊O���R}���hA
����”\ȃv���O�����܂��̓ob�` t@C���Ƃ��ĔF������Ă��܂���B
Traceback (most recent call last):
  File "E:/python/code/test2.py", line 3, in <module>
    text = subprocess.check_output(['kaggle', 'competitions', 'list'], shell=True)
  File "E:\Anaconda3\envs\tf\lib\subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "E:\Anaconda3\envs\tf\lib\subprocess.py", line 438, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['kaggle', 'competitions', 'list']' returned non-zero exit status 1.

Process finished with exit code 1

ところがコマンドプロンプトから実行すると正常に動きます。

コマンドプロンプトで実行した結果
ref                                            deadline             category            reward  teamCount  userHasEntered  
---------------------------------------------  -------------------  ---------------  ---------  ---------  --------------  
digit-recognizer                               2030-01-01 00:00:00  Getting Started  Knowledge       2526            True  
titanic                                        2030-01-01 00:00:00  Getting Started  Knowledge      12367            True  
house-prices-advanced-regression-techniques    2030-01-01 00:00:00  Getting Started  Knowledge       4798           False  
imagenet-object-localization-challenge         2029-12-31 07:00:00  Research         Knowledge         55           False  
competitive-data-science-predict-future-sales  2019-12-31 23:59:00  Playground           Kudos       4488            True  
ashrae-energy-prediction                       2019-12-19 23:59:00  Featured           $25,000        365           False  
Kannada-MNIST                                  2019-12-17 23:59:00  Playground       Knowledge        403           False  

何が問題だったか

pycharmのシェルから実行したいファイルにPATHが通っていないのが原因でした。

コマンドプロンプトからconda activateコマンドで環境を有効にすると使いたい環境にPATHが通るんですが、pycharmはPATHを通すのではなくて直接python.exeを実行するので同じ仮想環境を使っていてもPATHは通っていません。

対処方法

Pycharm → Run → Edit Configuration → Environment variableと入っていって、PATHに実行したい環境と同じPATHを記入します。実行したい環境のPATHは以下で確認できます。

コマンドプロンプト
echo %path%

image.png
image.png

結果

Pycharmで実行した結果
ref                                            deadline             category            reward  teamCount  userHasEntered  
---------------------------------------------  -------------------  ---------------  ---------  ---------  --------------  
digit-recognizer                               2030-01-01 00:00:00  Getting Started  Knowledge       2526            True  
titanic                                        2030-01-01 00:00:00  Getting Started  Knowledge      12367            True  
house-prices-advanced-regression-techniques    2030-01-01 00:00:00  Getting Started  Knowledge       4798           False  
imagenet-object-localization-challenge         2029-12-31 07:00:00  Research         Knowledge         55           False  
competitive-data-science-predict-future-sales  2019-12-31 23:59:00  Playground           Kudos       4488            True  
ashrae-energy-prediction                       2019-12-19 23:59:00  Featured           $25,000        365           False  
Kannada-MNIST                                  2019-12-17 23:59:00  Playground       Knowledge        403           False  

やったね!

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