在 Python 交互式控制台中执行 PyDev 项目?

发布于 2024-12-13 22:33:22 字数 708 浏览 0 评论 0原文

我是 Python 和 PyDev 的初学者。我最近在 PyDev 中制作了“helloworld”程序。我的问题是:如何执行它或以交互式 Python 模式(在 Linux 终端中)打开它我尝试了很多命令,例如 ./hello.pyimport hello.py,python hello.py,但我得到的唯一结果是 SyntaxError:无效语法,或其他一些错误。

我还有另一个问题。我有 Linux,并在终端中打开 Eclipse (sudo eclipse)。

在 PyDev 中,我首先转到

File => New => Python Project => HelloWorld (name of project) =>
right click the project => New => PyDev Module => hello (module name).

假设我在终端(运行 Python)中输入的名称是模块的名称,不是吗?不管怎样,我也尝试过使用项目的名称,但什么也没有。只是想知道。

而且,什么时候使用 chmod +x ?每次我在 PyDev 中编写它时,我都会在左侧看到一个 X,这意味着它不正确。像这样的东西:X chmod +x

I'm a begginer in Python and PyDev. I recently made the "helloworld" program in PyDev. My question is: how do I execute it or open it in the interactive Python mode (in Linux terminal) I tried many commands, like ./hello.py, import hello.py, python hello.py, but the only thing I got was SyntaxError: invalid syntax, or some other error.

I also have another question. I have Linux and I opened Eclipse in the terminal (sudo eclipse).

In PyDev, I first went to

File => New => Python Project => HelloWorld (name of project) =>
right click the project => New => PyDev Module => hello (module name).

It is assumed that the name I put in the terminal (running Python) is that of the module, no? Either way, I also tried with the name of the project and nothing. Just to know.

And, when do I use chmod +x? Every time I write it in PyDev, I get an X on the left, which means it's incorrect. Something like this: X chmod +x.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

成熟的代价 2024-12-20 22:33:22

我想我现在明白你在问什么了。如果要从 shell 执行文件中的某些内容,最简单的方法是将其封装在类中。

尝试将代码更改为以下内容:

#!/usr/bin/python

class Hello:
    def __init__(self):
        print "Hello, Interactive Shell World!"
        raw_input()

这将创建一个名为 Hello 的类,其中构造函数运行当前文件中的代码。然后,启动一个交互式 shell,该 shell 在与 hello.py 文件相同的目录中启动。以下是示例会话的粘贴:

>>> from hello import Hello
>>> Hello()
Hello, Interactive Shell World!

<hello.Hello instance at 0xb782686c>
>>> 

它打印消息,等待输入,然后打印新创建的对象的字符串表示形式并返回到提示符。如果您想避免最后一次打印输出,只需将对象分配给一个变量,例如:

>>> h = Hello()

如果您想保留从命令行(而不是 shell)执行文件的能力,请将此代码添加到文件底部:

if __name__ == '__main__':
    Hello()

您可以从终端输入“chmod +x”。在 hello.py 的目录中,运行:

chmod +x hello.py

这使您能够像 ./hello.py 而不是“python hello.py”一样运行文件。现在我想起来了,听起来你可能混淆了 python 交互式 shell 和 unix shell。您可以使用 unix shell 中的“python hello.py”轻松运行文件,但要从 python 交互式 shell 运行代码,您需要执行像我上面那样的操作。

I understand what you're asking now I think. If you want to execute something in a file from the shell, the easiest way is to encapsulate it in a Class.

Try changing your code to the following:

#!/usr/bin/python

class Hello:
    def __init__(self):
        print "Hello, Interactive Shell World!"
        raw_input()

This makes a class called Hello where the constructor function runs the code you have in your current file. Then, start an interactive shell started in the same directory as the hello.py file. Here is a paste from an example session:

>>> from hello import Hello
>>> Hello()
Hello, Interactive Shell World!

<hello.Hello instance at 0xb782686c>
>>> 

It prints the message, waits for input, then prints the string representation of the newly created object and returns to the prompt. If you want to avoid the last printout just assign the object to a variable like:

>>> h = Hello()

If you want to keep the ability to execute the file from the command line (rather than the shell) add this code to the bottom of the file:

if __name__ == '__main__':
    Hello()

You do "chmod +x" from the terminal. In the directory of the hello.py, run:

chmod +x hello.py

This gives you the ability to run your file like ./hello.py instead of "python hello.py". Now that I think of it, it sounds like you may be confusing the python interactive shell which a unix shell. You can run your file easily using "python hello.py" from a unix shell, but to run code from the python interactive shell, you will want to do something like I did above.

彼岸花ソ最美的依靠 2024-12-20 22:33:22

要配置 PyDev,请确保阅读其入门手册:http://pydev.org/manual_101_root.html

现在,您可能遇到语法错误,因为您使用的是 Python 3,其中 print 不再是关键字,而是一个函数(因此,您必须使用 print() 作为函数调用),并且还可以在请介意raw_input() 在 Python 3 上被重命名为 input())。

To configure PyDev, make sure you read its getting started manual: http://pydev.org/manual_101_root.html

Now, probably you're having a syntax error because you're using Python 3, where print is no longer a keyword, but a function (so, you have to use print() as a function call), and also have in mind that raw_input() was renamed to input() on Python 3).

骷髅 2024-12-20 22:33:22

要回答导入交互模式的第一个问题,只需使用 import filename 但不要包含 .py

C:\Users\CodeThis\Documents\py\search>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import aStar
>>> aStar.search((0,0),(5,9))
[(0, 0), (9, 0), (8, 0), (7, 0), (6, 0), (6, 9), (5, 9)]
>>>

之类的东西

To answer your first question to import into interactive mode just use import filename but don't include the .py

C:\Users\CodeThis\Documents\py\search>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import aStar
>>> aStar.search((0,0),(5,9))
[(0, 0), (9, 0), (8, 0), (7, 0), (6, 0), (6, 9), (5, 9)]
>>>

something like that anyway

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文