简体   繁体   English

为什么pycharm在os.system('python main.py')时使用不同的python

[英]why pycharm use different python when os.system('python main.py')

My environment is ubuntu 14, Python 2.7 . 我的环境是ubuntu 14,Python 2.7。 When I run a program directly and with function os.system(), it uses different python interpreter. 当我直接使用os.system()函数运行程序时,它使用不同的python解释器。 My code is as below: 我的代码如下:


# script.py
import tensorflow as tf
import os

print tf.__version__
print tf.__path__

command = 'python main.py'
os.system(command)

# main.py
import tensorflow as tf

print tf.__version__
print tf.__path__

when I run scripy.py, I got 当我运行scripy.py时,我得到了

1.2.1
['/home/Monday/Applications/anaconda2/lib/python2.7/site-packages/tensorflow']
0.12.1
['/usr/local/lib/python2.7/dist-packages/tensorflow']

when I run main.py, I got 当我运行main.py时,我得到了

1.2.1
['/home/Monday/Applications/anaconda2/lib/python2.7/site-packages/tensorflow']

I have set my Interpreter as below: 我的口译员设置如下:

Applications/anaconda2/lib/python2.7

PyCharm is running the Python in /home/Monday/Applications/anaconda2 but when you do os.system() it runs whatever Python is in your PATH environment variable. PyCharm在/home/Monday/Applications/anaconda2运行Python,但是当您执行os.system()它将运行PATH环境变量中的任何Python。 To fix this, you can change your PyCharm run settings to set the PATH environment variable to put the Anaconda Python first. 要解决此问题,您可以更改PyCharm运行设置,以将PATH环境变量设置为将Anaconda Python放在首位。

But better still, you should simply not invoke Python from Python. 但更好的是,您不应仅从Python调用Python。 Change main.py to be an importable module instead of a "main" script that you execute. main.py更改为可导入模块,而不是您执行的“ main”脚本。 Then it will be simple: just import main (or whatever it's called), and invoke functions on it. 这样就很简单:只需import main (或任何它调用的),然后在其上调用函数。 This is a more proper design, and is more efficient too. 这是一个更合适的设计,而且效率也更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM