My python scripts run fine from IDLE, but when I try to run them from the command-line, things go wrong. First I had trouble importing pygame, but I added C:\Python27\Lib\site-packages to the PYTHONPATH environment variable and all was well, I thought. However, now when I attempt to run something from the command line, I get this:
C:\Users\Ian Sinke\Documents\Pong>python pong.py
'import site' failed; use -v for traceback
Traceback (most recent call last):
File "pong.py", line 3, in ?
import pygame
File "C:\Python27\Lib\site-packages\pygame\__init__.py", line 27, in ?
import sys, os, string
File "C:\Python27\Lib\os.py", line 63, in ?
import ntpath as path
File "C:\Python27\Lib\ntpath.py", line 401
backslash, dot = (u'\\', u'.') if isinstance(path, unicode) else ('\\', '.')
^
SyntaxError: invalid syntax
Any ideas?
This is not a localize problem; when I try to run another script from the command line, I get this:
C:\Users\Ian Sinke\Documents>python app.py
'import site' failed; use -v for traceback
Traceback (most recent call last):
File "app.py", line 4, in ?
import urllib2
File "C:\Python27\Lib\urllib2.py", line 92, in ?
import base64
File "C:\Python27\Lib\base64.py", line 346
with open(args[0], 'rb') as f:
^
SyntaxError: invalid syntax
and that syntax is definitely OK...
Pong.py begins like this:
#import sys
import math
import pygame
import time
from pygame.locals import *
# Helper functions
def centerdist(paddletop, balltop):
return balltop - paddletop - 30
# Constants
BLACK = 0, 0, 0
pygame.init()
This sounds to me like you've got two different versions of Python on your computer. One is a more recent version that accepts Python's version of the ternary expression, and one is an older version. When you use IDLE, the newer version is called. When you use the command line, the older version is called. You can confirm or disprove this hypothesis by running python -V from the command line.
To elaborate, support for conditional expressions was added in Python 2.5. So when you modified PYTHONPATH, you wound up running a newer python file (from 2.7, it sounds like) with an older version of python (2.4, according to your test).
Make sure your command-line python is at least version 2.5 because, before then, there was no ternary operator (http://marc-abramowitz.com/archives/2008/05/18/python-ternary-operator/).
Related
when I executed the below code in python 2.7 CLI
import nltk
it is showing the following error
SyntaxError:Invalid Syntax
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/nani/.local/lib/python2.7/site-packages/nltk/__init__.py", line 128, in <module>
from nltk.collocations import *
File "/home/nani/.local/lib/python2.7/site-packages/nltk/collocations.py", line 35, in <module>
from nltk.probability import FreqDist
File "/home/nani/.local/lib/python2.7/site-packages/nltk/probability.py", line 333
print("%*s" % (width, samples[i]), end=" ") ^
SyntaxError: invalid syntax
How to fix this?
nltk dropped support to Python2, Try to use older versions of nltk in which it supports python 2 and I found out that nltk 3.0 version supports python 2 [edited - Thanks to user2357112 supports Monica
]
So, Try to download and install previous versions of nltk with the command
pip install nltk==3.0
You can change the version number which is 3.0 in the above mentioned case and can install suitable version whichever you feels working.
It worked for me.If anyone facing same problem can try above mentioned method.
The code is using the print function, which in Python 2.7 has to be enabled with
from __future__ import print_function
However, this has to appear in the module being imported, not the code importing the module. nltk appears to assume it will be imported by a Python 3 interpreter.
I'm new to python and have installed Python2.6
directory address of python is "C:\Python26"
I tried to use import ctypes
I'm getting below errors
Traceback (most recent call last):
File "C:\Python26\Lib\msdecmiolib\__init__.py", line 22, in <module>
import ctypes
File "C:\Python26\Lib\ctypes\__init__.py", line 25
5DEFAULT_MODE = RTLD_LOCAL
^
SyntaxError: invalid syntax.
Something's wrong with your Python installation; that line in ctypes/__init__.py should simply read
DEFAULT_MODE = RTLD_LOCAL
It's hard to imagine how that could have happened other than accidentally -- maybe you opened the file to look at it and accidentally hit 5 before saving? Anyway, you can either fix it yourself and see what happens or if you'd prefer reinstall Python.
(Possibly upgrading to at least 2.7 while you're at it, which would be a good idea unless there's some peculiar reason you can't, although that's up to you.)
I read some articles about Vimscripting with python. I felt very interested. So I tried to figure out how to do vimscripting with python.
But when I tried to import vim, it showed that the module vim wasn't installed. So I searched
on the web, but still haven't find the module to install.
I also tried pip to search automatically, still failed.
Does anyone know where to find the module vim?
I am using fedora 17, and I have installed vim 7.3 and python 2.7.
Thanks in advance.
Here is the error message:
>>> import vim
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named vim
It doesn't seem like you need to import it, just make sure your vim is compiled with python support. Check this using vim --version | grep +python. Further, it seems that the vim module is imported automatically when in vim, rendering your import line unnecessary.
it do not work but this will work
:python from vim import *
:python command('echo "Hello World!"')
if you put vim.command(..
You get
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'vim' is not defined
Compile VI with python as described by hd1. You can find the documentation here:
http://vimdoc.sourceforge.net/htmldoc/if_pyth.html
If you want to test
export PYTHONPATH=$HOME/local/lib/python2.7/site-packages
Note that I installed all in local, this is why I use $HOME in PYTHONPATH
Run vi then, type
:python from vim import *
:python vim.command('echo "Hello World!"')
you should see "Hello World!" below.
Somehow my python is broken and emits the error:
jseidel#EDP15:/etc/default$ python -c 'import random'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.6/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
This is NOT the virtualenv error that is so commonly documented here and elsewhere: I don't use python directly, I have never setup a virtualenv explicitly, and there is no virtualenv directory or python script that I can find anywhere on my system.
I'm running Kubuntu 10.04 and until just recently my KPackageKit worked just fine and handled updates with no problem. Now it shows nothing... maybe because of this python error, maybe because of something else.
How do I go about finding the error and fixing python?
As suggested by #Armin Rigo, this worked for me:
1) Add a print 42 at the end of the /usr/lib/python2.6/os.py file.
2) If you see "42", then that is the correct os.py file and the urandom module is not include. Add the statement to include urandom (you can find a sample from another os.py file). This was what worked for me.
3) If you don't see "42", then that's not the os.py file that you're using. Find the random.py file that is crashing and insert import os; print os.__file__ to get more information about the failure.
I am trying to get Python to run to use with Blender. I have 64 bit Vista SP2. 2.6.7 Python. When I start python the command prompt tells me this
'imprt site' failed; use -v for traceback
Traceback <most recent call last>:
File "c:\Python26\Scripts\pypm-script.py", line 5, in <module>
Import Error: No module named pkg_resources
So, I opened pypm-script.py
#!python2.6.exe
# EASY-INSTALL-ENTRY-SCRIPT: 'pypm==1.3.4','console_scripts','pypm'
__requires__ = 'pypm==1.3.4'
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
load_entry_point('pypm==1.3.4', 'console_scripts', 'pypm')()
)
This is very frustrating, because have no idea how to read code or how to use Python! I hope this is easily fixable.
The file pypm-script.py (and pkg_resources) come from ActivePython. You probably must have installed a different Python on top of ActivePython. I can think of two ways to fix this problem:
Uninstall Python, remove C:\Python26 and install ActivePython 2.6 (or 2.7), or
Remove C:\Python26\Scripts\pypm* and C:\Python26\Lib\site-packages\pypm*