python 3.9 and rdflib keep changing slashes in a url - python

I am completely new in Python . I am using 3.9.2 and rdflib . I try to execute the first example,
from rdflib import Graph
g = Graph()
g.parse('http://dbpedia.org/resource/Semantic_Web')
for s, p, o in g:
print(s, p, o)
I create a py file , paste it in and then using windows 10 cmd, I cd to my pyhton file and do python rdftest.py.
No matter how I set the url, I get
Traceback (most recent call last):
File "D:\BACKUP\programming\rdftrest.py", line 3, in <module>
g.parse('http://dbpedia.org/resource/Semantic_Web')
File "C:\Users\spiro\AppData\Local\Programs\Python\Python39\lib\site-packages\rdflib\graph.py", line 1188, in parse
source = create_input_source(
File "C:\Users\spiro\AppData\Local\Programs\Python\Python39\lib\site-packages\rdflib\parser.py", line 281, in create_input_source
) = _create_input_source_from_location(
File "C:\Users\spiro\AppData\Local\Programs\Python\Python39\lib\site-packages\rdflib\parser.py", line 312, in _create_input_source_from_location
if path.exists():
File "C:\Users\spiro\AppData\Local\Programs\Python\Python39\lib\pathlib.py", line 1407, in exists
self.stat()
File "C:\Users\spiro\AppData\Local\Programs\Python\Python39\lib\pathlib.py", line 1221, in stat
return self._accessor.stat(self)
OSError: [WinError 123] The filename, directory name, or volume label syntax is
incorrect: 'http:\\dbpedia.org\\resource\\Semantic_Web'
How can I fix that? Thank you

It seems to be a bug in rdflib version 6.0.0. I confronted the same problem and the solution was to downgrade the version to 5.0.0. Then it seems to work fine.
Looks like the parse() function is messed up: it uses pathlib when it sees URL and urllib when it sees the file path.

The issue seems to be related to how your Python is set up.
On Mac and Windows I can just:
install Python 3.9 (or 3.8, 3.7 or 3.6)
install rdflib 6.0.0
C:\Users\nick\AppData\Local\Programs\Python\Python39\python.exe -m pip install rdflib
run the script using Python with rdflib now installed
C:\Users\nick\AppData\Local\Programs\Python\Python39\python.exe rdftest.py
This works fine on up-to-date Windows 10 on both the Command Prompt and on Powershell.
So this issues seems to be to do with your Python installation or Windows configuration, not rdflib per se.

Related

Unable to run Python interpreter in terminal, through Anaconda3

When I try the "python" or "python3" command to run the interpreter, this is the error am getting.
[sidgupta234#sidgupta234-Lenovo-G580 Downloads]$ python
Failed to import the site module
Traceback (most recent call last):
File "/usr/lib/python3.5/site.py", line 580, in <module>
main()
File "/usr/lib/python3.5/site.py", line 566, in main
known_paths = addusersitepackages(known_paths)
File "/usr/lib/python3.5/site.py", line 287, in addusersitepackages
user_site = getusersitepackages()
File "/usr/lib/python3.5/site.py", line 263, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/lib/python3.5/site.py", line 253, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/lib/python3.5/sysconfig.py", line 595, in get_config_var
return get_config_vars().get(name)
File "/usr/lib/python3.5/sysconfig.py", line 538, in get_config_vars
_init_posix(_CONFIG_VARS)
File "/usr/lib/python3.5/sysconfig.py", line 410, in _init_posix
from _sysconfigdata import build_time_vars
File "/usr/lib/python3.5/_sysconfigdata.py", line 6, in <module>
from _sysconfigdata_m import *
ImportError: No module named '_sysconfigdata_m'
Output to which python
[sidgupta234#sidgupta234-Lenovo-G580 Downloads]$ which python
/usr/bin/python
Output to python -V
[sidgupta234#sidgupta234-Lenovo-G580 Downloads]$ python -V
Python 3.5.2 :: Continuum Analytics, Inc.
Could you tell me the reason of this error and how can I remove it?
I found this link while looking for the solution, but it didnt make any difference.
I guess configuring_apache_django_anaconda is relevant, if you look at the troubleshooting section.
This means that apache is using Python 2 instead of Python 3 to run a program that is designed for Python 3 only, and fails because Python 2 does not have the _sysconfigdata_m module. The solution is to add the following file to your apache envvar file (/etc/apache2/envvar):
export PATH=/opt/anaconda3/bin:$PATH
So maybe check which version of python anaconda is running?
Also these two might be related:Linux - Weird Python Output
ubuntu anaconda suggested fix
I would suggest you print your PATH variable to see if you have several python3 entries in there, and try to specify explicity which one to run.
That is an strange situation you have gotten yourself into, and if Continuum had any part of it (where I'm an engineer) we'd like to understand what we did that led to it so we can avoid it in the future.
Where did you try installing Anaconda? Did you set any environment variables?
The "easiest-to-resolve" cause of this problem is that you have set some environment variables that are mixing system Python libraries and Anaconda Python libraries. You should look at the output of:
env | grep -i anaconda
and see if anything comes up that specifies a path to Anaconda. The only one that should appear is an entry in PATH. If there are any LD_LIBRARY_PATH, PYTHONPATH, PYTHONHOME or similar environment variables that point to Anaconda then that is going to be a problem.
The only other thing I can think of is that you specified /usr as the install path for Anaconda. If that is the case you are probably in for a world of pain: you have just replaced your system Python with Anaconda. You should investigate how to force-reinstall whichever Python package comes with your *nix distribution, but even that might be tricky: tools such as yum are written in Python and will need a working system Python interpreter to work. But RPM is binary (if you're on a RedHat or derivative distro), so maybe you can wget or curl the necessary system Python packages and force-install them to try and fix things.
Let us know if that is enough information for you to fix things or at least identify the source of the problem a little better.

Error Import Impyla library on Windows

I'm having trouble with using impyla library on windows
I installed impyla library
pip install impyla
Error occured when I tried to import impyla libary in python code
from impala.dbapi import connect # error occured
from impala.util import as_pandas
conn = connect(host='10.xx.xx.xx', database='xx_xx', port=21050)`
Traceback (most recent call last): ...
File "D:/test/test.py", line 14, in
from impala.dbapi import connect
File "C:\Anaconda3\lib\site-packages\impala\dbapi.py", line 28, in
import impala.hiveserver2 as hs2
File "C:\Anaconda3\lib\site-packages\impala\hiveserver2.py", line 32, in
from impala._thrift_api import (
File "C:\Anaconda3\lib\site-packages\impala_thrift_api.py", line 73, in
include_dirs=[thrift_dir])
File "C:\Anaconda3\lib\site-packages\thriftpy\parser__init__.py", line 30, in load
include_dir=include_dir)
File "C:\Anaconda3\lib\site-packages\thriftpy\parser\parser.py", line 496, in parse
url_scheme))
thriftpy.parser.exc.ThriftParserError: ThriftPy does not support generating module with path in protocol 'c'
when I tried to print include_dir, which was
D:/test\thrift
I just cannot import libray at all
help me
I had the same problem with thriftpy, the problem on windows is an absolute path is like C:\foo\bar.thrift
But, the way the thrift library parses the file, it detects the C: as if it were a protocol like http: or https:
Its pretty easy to workaround you just have to strip the first two characters from the path with a slice like path[2:]
Just slice when you call thriftpy.load or in the library file
File "C:\Anaconda3\lib\site-packages\thriftpy\parser__init__.py", line 30
path = "C:\foo\bar.thrift"
thrift.load(path[2:], module_name, include_dirs=include_dirs,
include_dir=include_dir)
OR
You can go a bit deeper and make the same change that I already submitted as a patch on the github page... perhaps it will be incorporated in the next version of thrift.
File "C:\Anaconda3\lib\site-packages\thriftpy\parser\parser.py", line 488
- if url_scheme == '':
+ if len(url_scheme) <= 1:
My justification of why this change is valid is in the pull request. If its incorporated then you wont have to worry about making the same change again when you update the library. If not then just strip the two characters again.
Update:
Thriftpy Version 1: the parser fix is now on line 547: elif len(url_scheme) <= 1:
Thriftpy Version 2: the fix has already been merged.
I was encountering the same error with impyla on an Anaconda Python 3.6 distribution on Windows. Instead of installing using pip, I was able to get it working using:
conda install -c anaconda impyla
https://anaconda.org/anaconda/impyla

Python 2.7.5 installation on Ubuntu, MAXREPEAT

This has been asked a couple of times but the answers do not make any sense at least to me. I have Ubuntu 12.04 and had to upgrade Python from default (2.7.3?) to 2.7.5 to get libraries I can use to compile modwsgi.
I obtained 2.7.5 sources, configured with --enable-shared, made and installed. A few obsolete sun audio etc libraries could not be built but otherwise the installation went through. Except that it doesn't work:
visti#ubuntu:~$ python
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site.py", line 548, in <module>
main()
File "/usr/local/lib/python2.7/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/usr/local/lib/python2.7/site.py", line 266, in addusersitepackages
user_site = getusersitepackages()
File "/usr/local/lib/python2.7/site.py", line 241, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/local/lib/python2.7/site.py", line 231, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/local/lib/python2.7/sysconfig.py", line 516, in get_config_var
return get_config_vars().get(name)
File "/usr/local/lib/python2.7/sysconfig.py", line 449, in get_config_vars
import re
File "/usr/local/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/usr/local/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/usr/local/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/usr/local/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
The suggested solution here ubuntu ImportError: cannot import name MAXREPEAT was to reconfigure "virtualenv", whatever it is. /usr/local/bin has an ancient script named virtualenv but it tries to run python as well and doesn't work.
Someone suggested the problem might be related to using old Python modules as MAXREPEAT appeared in 2.7.4. To counter this I removed everything related to Python with rm -rf (/usr/bin/python*, /usr/lib/python*, /usr/local/lib/python*, /usr/local/bin/python*, did make clean, make, make install to 2.7.5.
Still the same result. Now I don't have a working version of Python anymore. Is there any way to fix this or should I try reinstalling Ubuntu Python packages if they have 2.7.5 available?
Hannu
Just battled the same problem. Ended up deleting the entire /usr/local/lib/python2.7 install path just to get back to the previous working version.
Also be sure to remove /usr/local/lib/python2.7-config, then reinstall python. Worked for me anyway.
This usually happens when you mix and match libpython.so versions with python interpreter versions. If nuking your install paths didnt help, check these environment variables.
$PYTHONHOME $PYTHONINCLUDE $PYTHONLIB $PYTHONLIBSTATIC $PYTHONPATH
Make sure what they point to were all deleted as well. And then reinstall.
You can also try and install python to a home directory, by configuring the -prefix variable to point to a home dir.
make configure -prefix=$HOME/mypython`
Then make sure you set those above variables to the new home directory python resides in.
The way I fixed this was by searching my system for other instances of the sre_constants.py file. Then see if the location of those files is referenced in your user or system $PATH. If so, remove that location configuration from your $PATH. Rerun python to see if that fixed it.
In my case, it seemed that Python is referencing the wrong location which does not does not support MAXREPEAT.

no module named requests

I will first state I have searched for this problem, and found the exact same problem here ( ImportError: No module named 'requests' ) but that hasn't helped me.
I am using macports on osx (mountain lion). I have successfully installed and run a few scripts without any issues.
from the macports page, I have installed requests via the method it detailed and as far as I can tell, it has installed successfully:
daves-mbp:~ Dave$ port search requests
arpwatch #2.1a15 (net)
Monitor ARP & RARP requests
http_ping #29jun2005 (net, www)
Sends HTTP requests every few seconds and times how long they take
httping #2.0 (net, www)
Ping-like tool for http-requests
py-requests #1.2.3 (python, devel)
Python HTTP for Humans.
py26-requests #1.2.3 (python, devel)
Python HTTP for Humans.
py27-requests #1.2.3 (python, devel)
Python HTTP for Humans.
py31-requests #1.2.3 (python, devel)
Python HTTP for Humans.
py32-requests #1.2.3 (python, devel)
Python HTTP for Humans.
py33-requests #1.2.3 (python, devel)
Python HTTP for Humans.
webredirect #0.3 (www)
small webserver which redirects all requests
Found 10 ports.
I have python 2.7, so I installed it via:
daves-mbp:~ Dave$ sudo port install py27-requests
Password:
---> Computing dependencies for py27-requests
---> Fetching archive for py27-requests
---> Attempting to fetch py27-requests-1.2.3_0.darwin_12.noarch.tbz2 from http://jog.id.packages.macports.org/macports/packages/py27-requests
---> Attempting to fetch py27-requests-1.2.3_0.darwin_12.noarch.tbz2.rmd160 from http://jog.id.packages.macports.org/macports/packages/py27-requests
---> Installing py27-requests #1.2.3_0
---> Activating py27-requests #1.2.3_0
---> Cleaning py27-requests
---> Updating database of binaries: 100.0%
---> Scanning binaries for linking errors: 100.0%
---> No broken files found.
daves-mbp:~ Dave$
I think that looks good. Using macports is there something else I have to do before using it? I thought the python setup.py install (in the aforementioned post) may have solved my problem, however, when I search for requests in my filesystem, the only reference is burried in a path (that macports says is a store for user installed modules. And besides, there is no setup.py within that or it's parent directory.
I have restarted my terminal window (that fixed another problem earlier), but it made no difference here.
Any help is appreciated
edit:
which python reports /opt/local/bin/python
the first lines of the python interpreter DID report:
daves-mbp:~ Dave$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
but now I have done something and it's responding with new errors:
daves-mbp:~ Dave$ python
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 548, in <module>
main()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 266, in addusersitepackages
user_site = getusersitepackages()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 241, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 231, in getuserbase
USER_BASE = get_config_var('userbase')
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.py", line 516, in get_config_var
return get_config_vars().get(name)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.py", line 449, in get_config_vars
import re
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
In trying to sort this out, I have broken python, and eventually I got it going again.
I think initially I had not run one of the port select --set... commands. Once I realised this might be the case, I did so, but that produced the errors at the top. MAXREPEATS, a circular reference perhaps? No idea.
I have read here (macports didn't place python_select in /opt/local/bin) and here (How do I uninstall python from OSX Leopard so that I can use the MacPorts version?) about the --set command not working and to try sudo port select python python26 (i used python27) instead.
I checked the PATH and python didn't appear, so I updated that as well.
I got my python interpreter back and low-and-behold imports requests now works.
I think at the end of it all, there were two errors:
I used --set instead of the newer command, and
my path wasn't set
edit: Actually, after more debugging, I found the error was on the first line of my script, I had defined which python to use (which was the default apple one, which doesn't include the module). Once I updated the shebang line, it worked.

installing mysql for python 2.7

D:\PythonPack\MySQL-python-1.2.3>python setup.py install
Traceback (most recent call last):
File "setup.py", line 15, in <module>
metadata, options = get_config()
File "D:\PythonPack\MySQL-python-1.2.3\setup_windows.py", line 7, in get_confi
g
serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_ke
y'])
WindowsError: [Error 2] The system cannot find the file specified
I am trying to install mysqldb for python in windows7 and run into that error message
Do you have an idea how I can make it work ? I already have pythonsetuptool installed
Actually there are many ways and i tried them for few hours and realize its time waste process bcoz MYSQLdb is vary from version to version i.e 2.6,2.7 and 3.2 but all those are confuse and time waste processes,
Instead best way is to get precompiled executables from : http://www.lfd.uci.edu/~gohlke/pythonlibs .
here choose version wise links like python 2.7 and 32bit so that you won't get any errors and now run the downloaded file, finally set path in environment variables.
Now open python IDLE editor or from command prompt c:>python
import MySQLdb
if u don't get any errors then it's working fine...enjoy with u r work
Just install with the exe from here.
And maybe you can find more information about this in the previous asked questions like this.

Categories