在 Python 交互式控制台中执行 PyDev 项目?
我是 Python 和 PyDev 的初学者。我最近在 PyDev 中制作了“helloworld”程序。我的问题是:如何执行它或以交互式 Python 模式(在 Linux 终端中)打开它我尝试了很多命令,例如 ./hello.py
、import 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我现在明白你在问什么了。如果要从 shell 执行文件中的某些内容,最简单的方法是将其封装在类中。
尝试将代码更改为以下内容:
这将创建一个名为 Hello 的类,其中构造函数运行当前文件中的代码。然后,启动一个交互式 shell,该 shell 在与 hello.py 文件相同的目录中启动。以下是示例会话的粘贴:
它打印消息,等待输入,然后打印新创建的对象的字符串表示形式并返回到提示符。如果您想避免最后一次打印输出,只需将对象分配给一个变量,例如:
如果您想保留从命令行(而不是 shell)执行文件的能力,请将此代码添加到文件底部:
您可以从终端输入“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:
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:
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:
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:
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.
要配置 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).
要回答导入交互模式的第一个问题,只需使用
import filename
但不要包含 .py之类的东西
To answer your first question to import into interactive mode just use
import filename
but don't include the .pysomething like that anyway