Mac OSX 上的 Pythonpath

发布于 2024-11-30 00:48:00 字数 1439 浏览 1 评论 0 原文

我通读了 Add to python path mac os x 我想这样做这是一个好主意,但 IDLE 仍然会给我一个简单调用 open(filename, mode) 的语法错误,所以我进一步查看,发现我可以这样做http://developer.apple.com/library/mac/#qa/ qa1067/_index.html 并在 .MacOSX 文件夹中设置一个environment.plist,所以我在我的主目录中执行了此操作,但仍然没有任何更改...我现在迷路了:-)

这就是我在 .bash_profile 中添加为 python 路径的内容,以及在environment.plist 中添加的相同路径(不带 :$PYTHONPATH):

PYTHONPATH="/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7:$PYTHONPATH"
export PYTHONPATH

编辑: 这就是我得到语法错误...在解释器中工作正常

import xml.etree.ElementTree as et 
import json

app = Bottle()

@app.route('/proPass', method ='POST')

#here happens here, need it further down in the code... which is not really relevant 
f = open('/Users/mohi/Desktop/proPass_project/server_service/systems.xml', 'rw')

def getData():
    timestamp = request.POST.get('timestamp', '').strip()
    data = request.POST.get('data', '').strip()

    if timestamp:
        processData(data, timestamp)
run()

错误:

    File "proPass_script.py", line 9
    f = open('/Users/mohi/Desktop/proPass_project/server_service/systems.xml', 'rw')
    ^
    SyntaxError: invalid syntax

I read through Add to python path mac os x and I figured doing that is a good idea, but still IDLE gives me a syntax error for a simple call of open(filename, mode), so I looked a little bit further and I found that I am able to do as stated in http://developer.apple.com/library/mac/#qa/qa1067/_index.html and set up an environment.plist in a .MacOSX folder, so I did that in my home dir and still no changes ... I am now lost :-)

Thats what I added as my python-path in .bash_profile and the same path in my environment.plist (without the :$PYTHONPATH):

PYTHONPATH="/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7:$PYTHONPATH"
export PYTHONPATH

EDIT:
Thats where I get the syntax-Error... works fine in the interpreter

import xml.etree.ElementTree as et 
import json

app = Bottle()

@app.route('/proPass', method ='POST')

#here happens here, need it further down in the code... which is not really relevant 
f = open('/Users/mohi/Desktop/proPass_project/server_service/systems.xml', 'rw')

def getData():
    timestamp = request.POST.get('timestamp', '').strip()
    data = request.POST.get('data', '').strip()

    if timestamp:
        processData(data, timestamp)
run()

The error:

    File "proPass_script.py", line 9
    f = open('/Users/mohi/Desktop/proPass_project/server_service/systems.xml', 'rw')
    ^
    SyntaxError: invalid syntax

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

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

发布评论

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

评论(1

灰色世界里的红玫瑰 2024-12-07 00:48:00

PYTHONPATH 不会影响您是否收到 SyntaxError - 仅影响 ImportError。因此,如果您收到 SyntaxError,则说明您的代码存在另一个问题。请发布代码,我们会指出。

编辑:您的错误在这一行:

@app.route('/proPass', method ='POST')

@ 指定一个装饰器,它仅在函数定义 (def)、类定义 (< code>class),或其他装饰器。

它在 open 行的第一个字符上显示错误,因为它需要那里的函数或类定义。

有关装饰器的更多信息,请参阅函数定义的文档。

PYTHONPATH doesn't effect whether or not you get a SyntaxError -- only an ImportError. So, if you're getting a SyntaxError, you've got another problem with your code. Please post the code and we'll point it out.

Edit: Your error is on this line:

@app.route('/proPass', method ='POST')

The @ designates a decorator, which is only valid on the line immediately before a function definition (def), a class definition (class), or another decorator.

It shows the error on the first character of the open line because it's expecting a function or class definition there.

See the docs for function definitions for more info on decorators.

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