Python - importing fastai results in SyntaxError - python

I am trying to setup an ML model using fastai and have to do the following imports:
import fastai.models
import fastai.nlp
import fastai.dataset
However, it gives me the following error by the fastai imports.
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fastai.nlp
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/fastai/nlp.py", line 172
if os.path.isdir(path): paths=glob(f'{path}/*.*')
^
SyntaxError: invalid syntax
Apparently, the character f in glob(f'{path}/*.*') is causing the error. I fixed the error by removing f, but it seems that there are lots of these errors in the fastai library.
My current thought is that I am using an incorrect python version. Could anyone give me some pointer?

Strings in the shape of:
f'{path}/*.*'
are called f-strings and were introduced in Python3.6.
That's why you get the SyntaxError - for versions lower than Python3.6 a SyntaxError will be raised, as this syntax just doesn't exist in lower versions.
So obviously fast-ai is programmed for Python3.6 or higher.
When you take a look at the installation issues (you have to scroll down a bit),
you can see under Is My System Supported? the first point:
Python: You need to have python 3.6 or higher
So I'm afraid updating your python is the easiest way to solve the problem!
If you like to learn more about f-strings you can take a look here: https://www.python.org/dev/peps/pep-0498/

Related

Python: undefined symbol: TLSv1_method when importing ssl

I'm running Python 3.5.6 on a distribution where TLS versions below 1.2 have been compiled out of OpenSSL by passing these options to ./configure: no-ssl no-tls1 no-tls1_1 no-ssl3-method no-tls1-method no-tls1_1-method. The OpenSSL version is 1.1.1d. Python 3 is built from source at distro build time and linked against the version of OpenSSL included in the distro.
Everything builds successfully, but when I try to import the ssl library in Python, I get the following error:
$ python3
Python 3.5.6 (default, Mar 23 2020, 05:11:33)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/ssl.py", line 99, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: /usr/lib/python3.5/lib-dynload/_ssl.cpython-35m-aarch64-linux-gnu.so: undefined symbol: TLSv1_method
I don't understand why this error occurs at runtime. The only reference I can find in the Python 3.5.6 code to TLSv1_method is line 3088 of _ssl.c:
ctx = SSL_CTX_new(TLSv1_method());
Using no-tls1-method does compile out the implementation of TLSv1_method, and that line in the Python code is not guarded by any #ifdef. But I'd expect that to cause a failure at link time for the _ssl.cpython-35m-aarch64-linux-gnu.so module, not at runtime when Python tries to import the module. What's going on here, and is there a way to fix it without patching Python? I cannot upgrade the version of OpenSSL or Python in use.
It seems that my confusion resulted from misunderstanding how _ssl.cpython-35m-aarch64-linux-gnu.so links to OpenSSL. I assumed that it was statically linked, but it's actually dynamically linked to libssl.so. This is why the error occurs at runtime when the shared object is loaded.
It seems, then, that the only way to fix this without updating Python is to patch the call to TLSv1_method to use the generic TLS_method instead. I'll leave this question open for a few days though in case anyone has a better suggestion.
Edit: I filed a Python bug for this issue. It should be fixed in a future release.

Python on Gentoo is missing sqlite

I use a Gentoo-based Docker image for CI with multiple versions of Python. Recently, I've started experiencing errors because one tool (coveralls) requires sqlite, which is missing. sqlite is part of the Python standard library.
This can be checked from the command line
>>> removing all .pyc files
>>> executing command
me#5b35f99c08af /source $ python
Python 3.6.9 (default, Dec 27 2019, 12:15:49)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sqlite3'
I couldn't find any notes in the Gentoo packaging database about this, but I'm not really familiar with it as an OS. I assume there must have been a problem building some relevant library.
Python has been installed like this:
RUN emerge -q -u dev-lang/python:3.6
But the error occurs for all the versions I'm currently testing with: >= 3.5. Any ideas as to what I'm doing wrong?

Python 3.x import error SyntaxError [duplicate]

This question already has answers here:
Invalid syntax (SyntaxError) in except handler when using comma
(5 answers)
Closed last month.
I'm using macOS Sierra. When importing builtwith I get these following error:
Daniels-MacBook-Pro:~ Daniel$ python
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul 2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import builtwith
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/danielotero/anaconda3/lib/python3.5/site-packages/builtwith/__init__.py", line 43
except Exception, e:
^
SyntaxError: invalid syntax
What I can do to import it correctly?
This is because the builtwith package you installed is developed by Python2, not Python3. So it uses print and Exception as Python2 does. It also uses urllib2 library which is seperated into two part of urllib library in Python3.
It's better to use Python2 (Python2.7) to finish the work or you have to modify the source code of builtwith, that is, change all print statement into print() function, change except Exception, e into except Exception as e, and change all urllib2 functions into functions in urllib.requests and urllib.error.
According to the module's issue tracker, it is not compatible with Python 3. The project owner says
This module was built with Python 2 in mind. Patches are welcome to also support Python 3, however would need to maintain backwards compatibility.
Since they don't appear to want to port it to Python 3 in order to remain backwards compatible, you should either use Python 2, look for another library, or attempt to port it yourself.

Compile Z3python?

I want to have Z3python on my Ubuntu 12.04 64-bit. I downloaded Z3 source, and compiled like below:
$autoconf
$./configure
$sudo make
$sudo make a
$sudo make o
Everything went well, but then I tried:
$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import z3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named z3
It seems Z3python is not installed yet? I followed exactly the instructions in README, but it seems to miss something regarding Python binding?
please ignore, what i got is the old version of Z3.
a recommendation: please provide the source code of the latest Z3 in the homepage. i couldnt find it anywhere, then wrongly got the old version. finally, i had to download the latest version from the SourceControl, which was a bit tricky.

Python 2.5.2 and Solaris 8 (gcc 3.4.2) build issues

I'm trying to build python 2.5.2 on Solaris 8 using gcc 3.4.2. I can't see any immediate errors in the ./configure step but, once built and i enter the python shell doing an import time errors with :
Python 2.5.2 (r252:60911, Nov 21 2008, 18:45:42)
[GCC 3.4.2] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named time
What am i doing wrong? From what i can see with a cursory google is that there might be an error with libstdc++.so, but i can't find any hard details.
Any suggestions would be most welcome.
Many thanks,
Al.
The time module is not built by default in Python, if you build from a source distribution you need to explicitly enable all the modules you want to compile.
Open up Modules/Setup.dist in the python source tree and comment out the line which says:
#time timemodule.c
To enable the build of time module. Also remember that you need to recompile Python for this to take an effect.

Categories

Resources