【问题标题】:Is it Possible to compile a Python Source file in Eclipse via Command Prompt?是否可以通过命令提示符在 Eclipse 中编译 Python 源文件?
【发布时间】:2013-02-02 00:36:24
【问题描述】:

我已经能够在 Eclipse 中编译一个完美运行的 C 程序。我已经安装了 python 解释器并且已经在 Windows 中设置了环境变量。

这些是 C 的代码。如何修改它以编译 .py(Python) 源文件。谢谢

public class C_Compile {
public static void main(String args[]){

    String ret = compile();
    System.out.println(ret);

}
    public static String compile()
    {
        String log="";
        String myDirectory = "C:\\";
         try {
             String s= null;
           //change this string to your compilers location
             Process p = Runtime.getRuntime().exec("cmd /C gcc Hello.c", null, new java.io.File(myDirectory));

         BufferedReader stdError = new BufferedReader(new 
              InputStreamReader(p.getErrorStream()));
         boolean error=false;

         log+="";
         while ((s = stdError.readLine()) != null) {
             log+=s;
             error=true;
             log+="\n";

         }
         if(error==false) log+="Compilation Successful !!!";

     } catch (IOException e) {
         e.printStackTrace();
     }
         return log;
    }


  public int runProgram() 
    {
        int ret = -1;
       try
         {            
             Runtime rt = Runtime.getRuntime();
             Process proc = rt.exec("cmd.exe /c start a.exe");
             proc.waitFor();
             ret = proc.exitValue();
         } catch (Throwable t)
           {
             t.printStackTrace();
             return ret;
           }
       return ret;                      
    }}

【问题讨论】:

  • 好的。你到底想做什么?写个运行python程序的eclipse插件?
  • 你不需要编译python源文件,它们是在运行时解释的
  • @yurib 你可以将它们编译成字节码。
  • @Cubic 和上面的程序一样,我只想在控制台中获取源代码结果。
  • @Cubid 你可以但很少需要。 (每当您将代码部署在某个程序本身没有写入权限的地方时,例如出于安全原因。)

标签: java python eclipse cmd


【解决方案1】:

默认情况下,eclipse 没有 python 运行时。 Pydev 是一个流行的 eclipse 插件,它为你提供了这个功能。

引自Pydev的网站:

PyDev 是 Eclipse 的 Python IDE,可用于 Python、Jython 和 IronPython 开发。

但是,如果您希望从命令提示符处运行它,并且设置了 python 环境变量,则可以使用命令python <path>,其中 path 是 python 源文件的相对/绝对路径。

【讨论】:

  • 我正在寻找的不是在 windows 中打开 cmd 并输入命令 python ,有没有办法在 eclipse 中执行此操作?
  • @user2026254 - 查看 Pydev 的安装手册 pydev.org/manual_101_install.html。这是一个可以做到这一点的eclipse插件。
猜你喜欢
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 2012-05-02
  • 1970-01-01
  • 2016-06-06
  • 2023-03-22
  • 1970-01-01
相关资源
最近更新 更多