Question

I'm trying to use pycurl on the Raspberry Pi. I've successfully installed pycurl using apt-get install python-pycurl and I've found a little script to use to see if it's working correctly:

import pycurl
c = pycurl.Curl()
c.setopt(c.URL, 'http://news.ycombinator.com')
c.perform()

When I run this script using sudo ./pycurltest.py I get an error:

./pycurltest.py: 1: ./pycurltest.py: import: not found
./pycurltest.py: 2: ./pycurltest.py: Syntax error: "(" unexpected

However, if use the python interpreter and use help(modules) I can see that pycurl is installed. When I try to run the same script in the interpreter it works and I get:

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

What am I missing here?

Était-ce utile?

La solution

The first line is misleading.

./pycurltest.py: 1: ./pycurltest.py: import: not found

It looks like the interpreter is suggesting that a blank import cannot be found, or it's finding:

import Pycurses#<---something else is here

Check that your .py script does not have any weird characters at the end of the line and that it has a proper newline character:

import pycurl

From the python docs:

A physical line is a sequence of characters terminated by an end-of-line sequence. In source files, any of the standard platform line termination sequences can be used - the Unix form using ASCII LF (linefeed), the Windows form using the ASCII sequence CR LF (return followed by linefeed), or the old Macintosh form using the ASCII CR (return) character. All of these forms can be used equally, regardless of platform. When embedding Python, source code strings should be passed to Python APIs using the standard C conventions for newline characters (the \n character, representing ASCII LF, is the line terminator).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top