简体   繁体   English

执行python代码

[英]Executing python code

I am starting fresh with python and trying to execute a code from the python command window. 我重新开始使用python,并尝试从python命令窗口执行代码。 I wrote a file on Desktop\\practice\\new.py and lunched the python command window. 我在Desktop \\ practice \\ new.py上写了一个文件,并在python命令窗口中吃了午餐。

when I type 当我打字

C:\users\user\Desktop\practice\new.py

it gives me 它给我

SyntaxError: invalid syntax 

Executing from CMD worked, but from python window didnt! 从CMD执行工作,但从python窗口没有!

Any help? 有什么帮助吗?

EDIT2: when i put the compiled code in the directory and use the 'import' it runs, but when the compiled is not in the same directory it won't execute EDIT2:当我将已编译的代码放在目录中并使用“导入”时,它将运行,但是当已编译的代码不在同一目录中时,它将不会执行

EDIT: the file contains a simple print statement nd is sytax error free 编辑:该文件包含一个简单的打印语句nd是sytax错误无

Everything is explained in here: http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows 此处介绍了所有内容: http//docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows

The main point that when you launch python shell. 要点是启动python shell时。 Its like a live programming. 就像现场编程一样。 Try to type in it: 尝试输入:

>>> print 'hello world'

If you want to launch your file - run in cmd: python C:/users/user/Desktop/practice/new.py 如果要启动文件,请在cmd中运行: python C:/users/user/Desktop/practice/new.py

UPDATE: If you do want to run file from within python shell - it was answered here: How to execute a file within the python interpreter? 更新:如果您确实想从python shell中运行文件-此处已回答: 如何在python解释器中执行文件?

When you say you're using the "python command window" I'm guessing you mean IDLE...? 当您说您正在使用“ python命令窗口”时,我猜您的意思是IDLE ...? If so, rather than try to type a command to run a script you've already created as a file, just use File > Open to open that file and then press F5 to run it. 如果是这样,则不必尝试键入命令来运行已经创建为文件的脚本,而只需使用“文件”>“打开”来打开该文件,然后按F5键即可运行它。 Good luck! 祝好运!

The python command window is expecting python commands. python命令窗口需要python命令。 Try typing 'import system' or 'print 1+2'. 尝试输入“导入系统”或“打印1 + 2”。

If you want to run the code in another file you need to use 'import'. 如果要在另一个文件中运行代码,则需要使用“导入”。 Its easier if you start in the same directory, in which case just doing 'import new' will work. 如果从同一目录开始,则更容易,在这种情况下,只需执行“导入新文件”即可。

However, there's already a 'new' module in the python library, so the easiest thing to do is to rename your file something else... 但是,python库中已经有一个“新”模块,因此最简单的方法是将文件重命名为其他名称...

It is not working because you are entering the path like c:\\users\\user\\desktop\\practice\\new.py..... 它不起作用,因为您正在输入c:\\ users \\ user \\ desktop \\ practice \\ new.py .....之类的路径。

now try this way: c:/users/user/desktop/practice/new.py 现在尝试这种方式:c:/users/user/desktop/practice/new.py
I hope this will work for you ie just change '\\' to '/' have a try... 我希望这对您有用,即尝试将'\\'更改为'/'。

您可以这样运行文件:

execfile(r'C:\users\user\Desktop\practice\new.py')

Edit: read the comments below this answer before trying it! 编辑:尝试之前,请阅读此答案下方的评论!

Try this: 尝试这个:

import sys
sys.path.append("C:\users\user\Desktop\practice\")
import new #won't work - call it something other than new.py...

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

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