Python relative import causes syntaxerror: invalid syntax - python

I'm trying to install this great python module Python-Chrono to my python environment, but it fails at least with python 2.4.3 and 2.6.6 with the following error message:
Traceback (most recent call last):
File "setup.py", line 30, in ?
import chrono
File "/home/janne/python-chrono-0.3.0/chrono/__init__.py", line 22
from . import calendar
^
SyntaxError: invalid syntax
The setup is using relative import mechanism and it should work just fine, but in my environment it causes this error.
Is there a way to get this fixed? Have you seen this kind of behaviour in your projects?

Python 2.4 doesn't support that syntax - it was introduced in Python 2.5.
(Are you 100% sure that it's failing with that message in 2.6?)

Related

Problem importing a specific library with python 2.7.10

I am trying to install the following package in my MacOS High Sierra 10.13.6:
https://github.com/Michalychforever/CLASS-PT
When i run the make file everything looks to work fine. It not only generate an executable, which i can run by terminal using the ./ command, but also a python wrapper. The problem is related only with the former.
When i try to import the library i get the following message:
import classy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/thiagomergulhao/Library/Python/2.7/lib/python/site-packages/classy.so, 2): Symbol not found: _zdotu_
Referenced from: /Users/thiagomergulhao/Library/Python/2.7/lib/python/site-packages/classy.so
Expected in: flat namespace
in /Users/thiagomergulhao/Library/Python/2.7/lib/python/site-packages/classy.so
I am using the built-in python version (i.e 2.7.10). I already installed the Xcode 10.1 together with command line commands. My .bash_profile is:
export PATH="/Users/thiagomergulhao/Library/Python/2.7/bin:$PATH"
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
Does anyone have any ideia what is causing this problem?

Invalid syntax on importing nltk in python 2.7

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.

Running python code give NameError: name 'ABCMeta' is not defined every time

I'm using Python 3.7.3 with PyCharm. When I tried to import abc package into my project I got this error followed with Python quit unexpectedly alert dialog:
Fatal Python error: init_sys_streams: can't initialize sys standard streams
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/io.py", line 52, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/abc.py", line 166, in <module>
NameError: name 'ABCMeta' is not defined
Abort trap: 6
At first I didn't pay much attention to the error and thought I made a mistake in the code. So I commented import abc line. But the error didn't disappear. Moreover, each time I was running python3 programs I was getting the same error. Even in terminal:
Previously I had Anaconda installed, but uninstalled it later.
In my project I'm using pipenv
I also have python 2 but I don't use it. I tried to run it and there was no such error.
Previously I tried to install vptyhon in many ways. I didn't know about pip then, so I could mess up the framework.
Removing /Library/Frameworks/Python.framework file and reinstalling python 3.7.3 resolved the error

Syntax Error using import ctypes

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

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

Categories

Resources