Im new to Python but Im getting on pretty well, however I cannot seem to import save programs into IDLE. Could someone assist me where that is concerned, please. This is one of the errors no matter how simple the program is:
>>> import dinner
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
import dinner
File "C:\Python25\dinner.py", line 1
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
^
SyntaxError: invalid syntax
This is the error you would get if your dinner.py program actually started with the line
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
But that's not a line of valid Python code, that's the message that the interpreter gives on startup. For example, mine says
~/coding$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
and it's then that I start typing things like print 2+2, etc. You can open dinner.py in IDLE and remove any parts which look like this welcome message at the start.
Incidentally, I see you're using Python 2.5.4. This is pretty old now, and a lot of neat things have been added since. I would suggest switching to 2.7.3 instead.
Related
I am trying to load sqlite 64 bit while running Python 2.7 64 bit. I can do this interactively, but, not from a script.
Interactive:
$ /c/Python27-64/python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>>
From this script, one single line, the same as was done from the python shell:
import sqlite3
Run from command line:
$ /c/Python27-64/python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
import sqlite3
File "c:\Python27-64\lib\sqlite3\__init__.py", line 24, in <module>
from dbapi2 import *
File "c:\Python27-64\lib\sqlite3\dbapi2.py", line 28, in <module>
from _sqlite3 import *
ImportError: DLL load failed: %1 is not a valid Win32 application.
The script is obviously finding a 32 bit dll. But why? What is the difference between interactive and from the single line script? How is the DLL search being modified?
In case anyone runs into this, the problem was the file _sqlite3.pyd in the directory I was running the script. Can someone explain why Python creates it's own version of the Windows dll? Is this simply wrapped so that Python can make calls into it? Perhaps a ctypes wrapper?
According to the Python documentation of the zlib module, decompressobj objects have a copy() function to clone the decompressor's state. This equivalent functionality is also mentioned for the compressing side on StackOverflow here.
However, when I try to use it, it simply doesn't exist:
C:\>C:\Python27\python.exe
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>> meow = zlib.decompressobj()
>>> purr = meow.copy()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: copy
Is this a bug in Python? I tried searching for this subject, but too many false hits get in the way.
Never mind. It's a known bug that was fixed in newer versions of 2.7. I just needed to check the bug list. For some reason, Google didn't index that bug page.
I've installed Python 3.3 on a Windows 7 machine. I wanted to try to the Bottle micro web
framework. I downloaded the bottle.py (latest version from their site) and put it in my app
folder.
When I do the first line in the tutorial, "from bottle import route, run, template"
I got this error message:
*C:\Dev>python
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from bottle import route,run,template
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".\bottle.py", line 564
raise exc_info[0], exc_info[1], exc_info[2]
^
SyntaxError: invalid syntax
>>>*
Any suggestions how I can fix this problem and continue?
Thanks
You need to follow the bottle installation instructions.
The latest version is published to PyPI, you have an old version of the code there still, one that is not compatible with Python 3.
The format
raise Exception, args
is not there anymore in Python 3+.
That is why the statement is throwing syntax error:
raise exc_info[0], exc_info[1], exc_info[2]
That was a bug introduced 5 days ago and fixed today. Just download bottle.py again. Or use a stable release as suggested by Martijn Pieters
I'm trying to use generateDS under windows, which uses os.tmpfile. Unfortunately, os.tmpfile doesn't work for me:
(oneclickcos) C:\Users\Marcin\Documents\oneclickcos\xsd>python
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.tmpfile()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 13] Permission denied
>>>
I've got all my temp directories set with full control for everyone, so that shouldn't be the problem.
What could be causing this?
Run the script as administrator (right click on the script and select 'run as administrator'), the script lacks the permissions to execute os.tmpfile().
Edit:
As I see you're using the interpreter, simply run the interpreter as administrator. If you're accessing it though a terminal, running the terminal as administrator should be sufficient.
As Griffin pointed out the problem is that the os.tmpfile() tries to create a file in the root directory. If you don't like to run the script as administrator you can use os.tmpnam() and handle the file yourself.
Warning: Use of tmpnam() is vulnerable to symlink attacks
Running python on Snow Leopard, and I can't import the 'time' module. Works in ipython. Don't have any .pythonrc files being loaded. Scripts that 'import time' using the same interpreter run fine. Have no idea how to troubleshoot this. Anyone have an idea?
[wiggles#bananas ~]$ python2.6
Python 2.6.6 (r266:84292, Sep 1 2010, 14:27:13)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "time.py", line 4, in <module>
t = now.strftime("%d-%m-%Y-%H-%M")
AttributeError: struct_time
>>>
[wiggles#bananas ~]$ ipython-2.6
Python 2.6.6 (r266:84292, Sep 1 2010, 14:27:13)
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: import time
In [2]:
Look for a file called time.py. It looks like Python is importing that, instead of the one from the standard library:
File "time.py", line 4, in <module>
The solution is to rename the file something other than "time.py".
By the way, you can find the path to the offending file by opening a Python REPL and typing.
In [1]: import time
In [2]: time.__file__
or
In [3]: time # This shows the path as part of the repr