Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python can't handle importing via command-line

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()
like image 338
ian93 Avatar asked May 10 '12 19:05

ian93


People also ask

How do you import a command in Python?

The Python import statement imports code from one module into another program. You can import all the code from a module by specifying the import keyword followed by the module you want to import. import statements appear at the top of a Python file, beneath any comments that may exist.

How do you pass a file path as a command line argument in Python?

To use it, you just pass a path or filename into a new Path() object using forward slashes and it handles the rest: Notice two things here: You should use forward slashes with pathlib functions. The Path() object will convert forward slashes into the correct kind of slash for the current operating system.

What is import cmd in Python?

The import command in Python is used to get access to other modules. Modules are the same as a code library in Java, C, C++, or C#. A module typically involves a set of functions and variables.


1 Answers

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

like image 113
senderle Avatar answered Oct 06 '22 00:10

senderle