SyntaxError: invalid syntax when creating virtualenv - python

I wanted to create a virtualenv for python 2.7 (I'm using 3.7)
I run the cmd as admin (on windows 10) :
C:\WINDOWS\system32>virtualenv -p C:\Python27\python.exe py27
An I got the error:
Running virtualenv with interpreter C:\Python27\python.exe
Traceback (most recent call last):
File "c:\path\to\virtualenv.py", line 26, in <module>
import logging
File "C:\Python27\lib\logging\__init__.py", line 43, in <module>
import threading
File "C:\Python27\lib\threading.py", line 15, in <module>
from collections import deque
File "C:\Python27\lib\collections\__init__.py", line 55
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
^
SyntaxError: invalid syntax
When I Launch Python2.7 and try to import the logging module: It show the same error but When I retype it. It work just fine:
>>> import logging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\logging\__init__.py", line 43, in <module>
import threading
File "C:\Python27\lib\threading.py", line 15, in <module>
from collections import deque
File "C:\Python27\lib\collections\__init__.py", line 55
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
^
SyntaxError: invalid syntax
>>> import logging
>>> logging.warning('hello word')
WARNING:root:hello word
>>>
I can't figure out why it does that.
I have already configured a virtual env with python 3.6
Any help would be appreciated.

You have a version problem. This line
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
is being run under Python 2.7, but is Python 3.6 (or 3.7) and will give you a syntax error in earlier versions. In your case I would treat that Python 2.7 installation as irretrievably broken, because it is clear that the standard library isn't to be trusted to all be of a consistent version.

The problem was resolved deleting the c:\Python27 directory, Then Uninstalling python 2.7 using the setup tool, restarting the computer and reinstalling Python 2.7

Related

Failed to import dataclasses module

Today I installed python 3.7 from apt-get to try out the new dataclasses module. I installed it seperately because python3.6 wasn't upgradeable to 3.7.
When I type: python3.7 --version, it gives me: >>> Python 3.7.0a2 as my current version.
The problem is that I can't seem to import dataclasses.
my import statement is: from dataclasses import dataclass as instructed here
This is the error message it's giving me:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dataclasses'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dataclasses'
I even tried installing the dataclasses module with pip3: pip3 install dataclasses. Not sure if that's necessary though.
Any suggestion on what might be the problem?
[EDIT] Just tried it with a python3.6 console and it worked fine .. weird.
As suggested by #wim python3.7 -m venv venv_dir
This command will:
Use python3.7 to run the command
The -m flag tells the interpreter to run the next argument as a script
venv is a module, and because of the -m flag it will be run as a script
Finally, the venv_dir is given to the venv module as an argument which
this module will use to create a virtual environment directory at
Once this command is run now you'll have a nice sandbox for messing around/testing the dataclasses module.
To activate this virtual environment be sure to run source venv_dir/bin/activate before you begin. This command will run the script at venv_dir/bin/activate to set up the necessary environment variables and other things for you
To deactivate, simply run deactivate after activating

tox Invocation failed for Python 3

tox runs my Python 2.7 tests, but fails for Python 3.4. It gives an "Invocation failed" message. Excerpts:
py34 create: /home/josh/code/ezoutlet/.tox/py34
ERROR: invocation failed (exit code 1), logfile: /home/josh/code/ezoutlet/.tox/py34/log/py34-0.log
ERROR: actionid: py34
msg: getenv
cmdargs: ['/usr/bin/python', '-m', 'virtualenv', '--python', '/usr/bin/python3.4', 'py34']
And later
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in <module>
import base64
File "/usr/lib/python3.4/base64.py", line 9, in <module>
import re
File "/usr/lib/python3.4/re.py", line 336, in <module>
import copyreg
File "/usr/local/lib/python2.7/dist-packages/copyreg/__init__.py", line 7, in <module>
raise ImportError('This package should not be accessible on Python 3. '
ImportError: This package should not be accessible on Python 3. Either you are trying to run from the python-future src folder or your installation of python-future is corrupted.
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 53, in apport_excepthook
if not enabled():
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled
import re
File "/usr/lib/python3.4/re.py", line 336, in <module>
import copyreg
File "/usr/local/lib/python2.7/dist-packages/copyreg/__init__.py", line 7, in <module>
raise ImportError('This package should not be accessible on Python 3. '
ImportError: This package should not be accessible on Python 3. Either you are trying to run from the python-future src folder or your installation of python-future is corrupted.
Original exception was:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in <module>
import base64
File "/usr/lib/python3.4/base64.py", line 9, in <module>
import re
File "/usr/lib/python3.4/re.py", line 336, in <module>
import copyreg
File "/usr/local/lib/python2.7/dist-packages/copyreg/__init__.py", line 7, in <module>
raise ImportError('This package should not be accessible on Python 3. '
ImportError: This package should not be accessible on Python 3. Either you are trying to run from the python-future src folder or your installation of python-future is corrupted.
Running virtualenv with interpreter /usr/bin/python3.4
ERROR: InvocationError: /usr/bin/python -m virtualenv --python /usr/bin/python3.4 py34 (see /home/josh/code/ezoutlet/.tox/py34/log/py34-0.log)
You need to look at the details of the error message. Notice the command that failed (from the first excerpt):
cmdargs: ['/usr/bin/python', '-m', 'virtualenv', '--python', '/usr/bin/python3.4', 'py34']
You can run this manually with:
/usr/bin/python -m virtualenv --python /usr/bin/python3.4 py34
Assuming you see the same error, your issue is with virtualenv rather than tox.
I reviewed the virtualenv help page and it seems like this should work. Faced with a possible bug, a good first step is to upgrade Python, virtualenv, or other libraries.
In my case, upgrading virtualenv was the solution:
pip install --upgrade virtualenv
Interestingly, this solution worked on both Windows and Linux. Most likely, there was a bug fix between when I got virtualenv and the latest release.

How do we use Python generated protobuf codes in to python code?

I have installed protobuf in Python 3.4 and i pushed the compiled code pb_x_pb2.py into python34 folder. When i enter import pb_x_pb2.py it shows the following error.
>>> import pb_interface_pb2
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
import pb_interface_pb2
File "C:\Python34\pb_interface_pb2.py", line 5, in <module>
from google.protobuf import reflection
File "C:\Python34\lib\site-packages\google\protobuf\reflection.py", line 68, in <module>
from google.protobuf.internal import python_message
File "C:\Python34\lib\site-packages\google\protobuf\internal\python_message.py", line 848
except struct.error, e:
^
SyntaxError: invalid syntax
Protobuf doesn't really support python 3.x. For the imported libraries, try pip install protobuf-py3, a python 3 port of the package. You can also run the 2to3.py script from the Pythonxx\Tools\Scripts folder on the generated file.
Another option would be to download the newest version of the sources, and compile them yourself. I believe that the newest version is mostly compatible with both 2.x and 3.x

virtualenv on different Python version throws ImportError

I searched around and haven't seen this posted elsewhere.
My Windows machine uses Python 2.7 as the default Python. I ran virtualenv to create a virtual environment that uses Python 3.4:
C:\virtualenvs>virtualenv -p C:\Python34\python.exe mypy3
and got an error:
Running virtualenv with interpreter C:\Python34\python.exe
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\virtualenv.py", line 8, in <module>
import base64
File "C:\Python34\lib\base64.py", line 9, in <module>
import re
File "C:\Python34\lib\re.py", line 324, in <module>
import copyreg
File "C:\Python27\lib\site-packages\copyreg\__init__.py", line 2, in <module>
from future.utils import PY3
File "C:\Python27\lib\site-packages\future\utils\__init__.py", line 75, in <mo
dule>
import functools
File "C:\Python34\lib\functools.py", line 21, in <module>
from collections import namedtuple
File "C:\Python34\lib\collections\__init__.py", line 17, in <module>
from reprlib import recursive_repr as _recursive_repr
File "C:\Python27\lib\site-packages\reprlib\__init__.py", line 8, in <module>
raise ImportError('Cannot import module from python-future source folder')
ImportError: Cannot import module from python-future source folder
I needed to specify the full path of virtualenv:
C:\virtualenvs>C:\python34\Scripts\virtualenv.exe -p C:\Python34\python.exe
because I was effectively calling this:
C:\virtualenvs>C:\python27\Scripts\virtualenv.exe -p C:\Python34\python.exe
since C:\python27\Scripts is in my PATH. And the collision of Python 2.7 and 3.4 was causing the issue.

Jinja2.5 Syntax error on import

I am using Python 3.2.3. And I installed Jinja2.5 by downloading from this page: https://pypi.python.org/pypi/Jinja2/2.5.5
Then I used the setup.py to install it. This seemed worked like a charm. When I tested it by using this line:
from jinja2 import Template
I got the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/dist-packages/Jinja2-2.5.5-py3.2.egg/jinja2/__init__.py", line 37, in <module>
from jinja2.environment import Environment, Template
File "/usr/local/lib/python3.2/dist-packages/Jinja2-2.5.5-py3.2.egg/jinja2/environment.py", line 13, in <module>
from jinja2 import nodes
File "/usr/local/lib/python3.2/dist-packages/Jinja2-2.5.5-py3.2.egg/jinja2/nodes.py", line 18, in <module>
from jinja2.utils import Markup, MethodType, FunctionType
File "/usr/local/lib/python3.2/dist-packages/Jinja2-2.5.5-py3.2.egg/jinja2/utils.py", line 585, in <module>
from markupsafe import Markup, escape, soft_unicode
File "/usr/local/lib/python3.2/dist-packages/markupsafe/__init__.py", line 70
def __new__(cls, base=u'', encoding=None, errors='strict'):
^
SyntaxError: invalid syntax
This is on my Raspberry pi with Raspbian installed. I don't know why this error occures, because the docs say Jinja2.5 and Python 3.2 are compatible.
Can anyone help me out?
Thanks in advance!
Try typing python --version if the reply does not say Python 3. something then your default python is python 2 and so does not support u'' for unicode strings.
If this is the case you need to install jinja2 by using pip3 install jinja2 and run your scripts with python3 scriptname.py.
Other than that see: Syntax error in jinja 2 library basically python 3.2 is no longer supported so the choice is pick another library or upgrade python.

Categories