0

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?

1 Answer 1

0

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).

2
  • I'm actually completely embarrassed to admit this, but the problem was actually that I was missing the #!/usr/bin/env python at the top of my script. Your answer made me go back and look more closely at what I was doing. Total rookie mistake on my part. Thanks.
    – Robottamer
    Sep 17, 2013 at 7:11
  • That's interesting. I've never had an issue with a missing hash-bang in a .py script. I know this because I always forget to add them.
    – gregb212
    Sep 17, 2013 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.