I'm trying to load an ontology that imports some other ontologies. These other ontologies are located on my machine and not on the web. If I try my_ontology.load() it tries to load these other ontologies from their URI. But that fails.
How can I tell owlready2 where these ontologies are located?
The traceback looks like this:
Traceback (most recent call last):
File "~Software/PyOntologyChecker/app.py", line 12, in <module>
covid_onto = get_ontology_from_file(COVID_ONTOLOGY)
File "~Software/PyOntologyChecker/app.py", line 8, in get_ontology_from_file
return get_ontology("file://" + file_path).load()
File "~.local/lib/python3.8/site-packages/owlready2/namespace.py", line 813, in load
imported_ontologies = [self.world.get_ontology(self._unabbreviate(abbrev_iri)).load() for abbrev_iri in self.world._get_obj_triples_sp_o(self.storid, owl_imports)]
File "~.local/lib/python3.8/site-packages/owlready2/namespace.py", line 813, in <listcomp>
imported_ontologies = [self.world.get_ontology(self._unabbreviate(abbrev_iri)).load() for abbrev_iri in self.world._get_obj_triples_sp_o(self.storid, owl_imports)]
File "~.local/lib/python3.8/site-packages/owlready2/namespace.py", line 773, in load
except: raise OwlReadyOntologyParsingError("Cannot download '%s'!" % f)
owlready2.base.OwlReadyOntologyParsingError: Cannot download 'http://ontofox.hegroup.org/CHEBI_module11.owl'!
Assuming you have a local copy of CHEBI_module11.owl dependence (ex. SOME_DIR/HEBI_module11.owl), add its location into onto_path (as stated in the docs):
onto_path.append("SOME_DIR")
before doing my_ontology.load(). That tells Owlready2 to look for files in SOME_DIR. The path can be relative or absolute (like c:/data/ontologies).
The current Python's working directory is not scanned by default but can be set so with onto_path.append(".").
If a required ontology is described by URI not ending with a usual filename, like "http://rdf.webofcode.org/woc/", the local filename (woc in this case) should not have an extension.
Related
I have this code :
import gensim
filename = 'GoogleNews-vectors-negative300.bin'
model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)
and this is my folder organization thing :
image of my folder tree that shows that the .bin file is in the same directory as the file calling it, the file being ai_functions
But sadly I'm not sure why I'm having an error saying that it can't find it. Btw I checked, I am sure the file is not corrupted. Any thoughts?
Full traceback :
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/run.py", line 1, in <module>
from serv import app
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/__init__.py", line 13, in <module>
from serv import routes
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/routes.py", line 7, in <module>
from serv.ai_functions import checkplagiarism
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/ai_functions.py", line 31, in <module>
model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/gensim/models/keyedvectors.py", line 1629, in load_word2vec_format
return _load_word2vec_format(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/gensim/models/keyedvectors.py", line 1955, in _load_word2vec_format
with utils.open(fname, 'rb') as fin:
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/smart_open/smart_open_lib.py", line 188, in open
fobj = _shortcut_open(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/smart_open/smart_open_lib.py", line 361, in _shortcut_open
return _builtin_open(local_path, mode, buffering=buffering, **open_kwargs)
FileNotFoundError: [Errno 2] No such file or directory: 'GoogleNews-vectors-negative300.bin'
The 'current working directory' that the Python process will consider active, and thus will use as the expected location for your plain relative filename GoogleNews-vectors-negative300.bin, will depend on how you launched Flask.
You could print out the directory to be sure – see some ways at How do you properly determine the current script directory? – but I suspect it may just be the /Users/Ile-Maurice/Desktop/Flask/flaskapp/ directory.
If so, you could relatively-reference your file with the path relative to the above directory...
serv/GoogleNews-vectors-negative300.bin
...or you could use a full 'absolute' path...
/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/GoogleNews-vectors-negative300.bin
...or you could move the file up to its parent directory, so that it is alonside your Flask run.py.
I've got some data I need to load using ytree which can be done via:
import ytree
a = ytree.load('ctrees.h5')
However, doing this gives the following error:
OSError: file does not exist: ctrees.h5.
With traceback:
Traceback (most recent call last):
File "C:\Users\Documents\untitled1.py", line 3, in <module>
a = ytree.load('ctrees.h5')
File "C:\Users\anaconda3\lib\site-packages\ytree\data_structures\arbor.py", line 1089, in load
raise IOError("file does not exist: %s." % filename)
OSError: file does not exist: ctrees.h5.
The same happens if I try a = ytree.load("ctrees.h5"). I can see that the file exists, I have changed the working directory to the correct one and my script is saved in the same directory. I also tried specifying the full path to the file with no joy.
I'm using Python 3.7 and ytree 2.3 on Windows 10
Any help appreciated.
EDIT (RESOLVED):
Fixed the issue by specifying path at top of script and reading file as:
PATH='C:\\path\\to\\file\\directory\\'
a = ytree.load(PATH + "ctrees.h5")
I have a simple program that takes input from the user and then does scraping with selenium. Since the user doesn't have Python environment installed I would like to convert it to *.exe. I usually use cx_freeze for that and I have successfully converted .py programs to .exe. At first it was missing some modules (like lxml) but I was able to solve it. Now I think I only have problem with docx package.
This is how I initiate the new document in my program (I guess this is what causes me problems):
doc = Document()
#then I do some stuff to it and add paragraph and in the end...
doc.save('results.docx')
When I run it from python everything works fine but when I convert to exe I get this error:
Traceback (most recent call last):
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "tribunalRio.py", line 30, in <module>
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\api.py", line 25, in Document
document_part = Package.open(docx).main_document_part
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\package.py", line 116, in open
pkg_reader = PackageReader.from_file(pkg_file)
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file
phys_reader = PhysPkgReader(pkg_file)
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\phys_pkg.py", line 31, in __new__
"Package not found at '%s'" % pkg_file
docx.opc.exceptions.PackageNotFoundError: Package not found at 'C:\Users\tyszkap\Dropbox (Dow Jones)\Python Projects\build\exe.win-a
md64-3.4\library.zip\docx\templates\default.docx'
This is my setup.py program:
from cx_Freeze import setup, Executable
executable = Executable( script = "tribunalRio.py" )
# Add certificate to the build
options = {
"build_exe": {'include_files' : ['default.docx'],
'packages' : ["lxml._elementpath", "inspect", "docx", "selenium"]
}
}
setup(
version = "0",
requires = [],
options = options,
executables = [executable])
I thought that explicitly adding default.docx to the package would solve the problem (I have even tried adding it to the library.zip but it gives me even more errors) but it didn't. I have seen this post but I don't know what they mean by:
copying the docx document.py module inside my function (instead of
using Document()
Any ideas? I know that freezing is not the best solution but I really don't want to build a web interface for such a simple program...
EDIT:
I have just tried this solution :
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
doc = Document(find_data_file('default.docx'))
but again receive Traceback error (but the file is in this location...):
Traceback (most recent call last):
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "tribunalRio.py", line 43, in <module>
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\api.py", line 25, in Document
document_part = Package.open(docx).main_document_part
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\package.py", line 116, in open
pkg_reader = PackageReader.from_file(pkg_file)
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file
phys_reader = PhysPkgReader(pkg_file)
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\phys_pkg.py", line 31, in __new__
"Package not found at '%s'" % pkg_file
docx.opc.exceptions.PackageNotFoundError: Package not found at 'C:\Users\tyszkap\Dropbox (Dow Jones)\Python Projects\build\exe.win-a
md64-3.4\default.docx'
What am I doing wrong?
I expect you'll find the problem has to do with your freezing operation not placing the default Document() template in the expected location. It's stored as package data in the python-docx package as docx/templates/default.docx (see setup.py here: https://github.com/python-openxml/python-docx/blob/master/setup.py#L37)
I don't know how to fix that in your case, but that's where the problem is it looks like.
I had the same problem and managed to get around it by doing the following. First, I located the default.docx file in the site-packages. Then, I copied it in the same directory as my .py file. I also start the .docx file with Document() which has a docx=... flag, to which I assigned the value: os.path.join(os.getcwd(), 'default.docx') and now it looks like doc = Document(docx=os.path.join(os.getcwd(), 'default.docx')). The final step was to include the file in the freezing process. Et voilà! So far I have no problem.
This code is just for testing purposes ( learning here )
Checks for files on a directory, if they end with .rar it prints its name, decompresses the file and calls another function that prints the name again.
The code :
import os, patoolib
path = "/root/tree/down/"
def cutext(f):
print (f)
def unrar():
for f in os.listdir(path):
if f.endswith('.rar'):
print(f)
patoolib.extract_archive(f, outdir="/root/tree/def")
cutext(f)
unrar()
The output error ( reads the file but does not decompresses it ) :
root#debian:~# python autotube.py
Ravi Shankar - Shankar Family & Friends (2010) [FLAC].rar
Traceback (most recent call last):
File "autotube.py", line 17, in <module>
unrar()
File "autotube.py", line 14, in unrar
patoolib.extract_archive(f, outdir="/root/tree/def")
File "/usr/local/lib/python3.4/dist-packages/patoolib/__init__.py", line 676, in extract_archive
util.check_existing_filename(archive)
File "/usr/local/lib/python3.4/dist-packages/patoolib/util.py", line 390, in check_existing_filename
raise PatoolError("file `%s' was not found" % filename)
patoolib.util.PatoolError: file `Ravi Shankar - Shankar Family & Friends (2010) [FLAC].rar' was not found
patoolib.util.PatoolError: file `Ravi Shankar - Shankar Family & Friends (2010) [FLAC].rar' was not found
This suggests the program can not find the file. You are doing listdir() on "/root/tree/down/" but when you're passing the file to extract_archive, you're just passing the file name f which does not include the full path. So if there's a file /root/tree/down/myarchive.rar, you're just passing myarchive.rar.
So the program tries to find it in the same directory (current working directory) but can not find it since it's not here.
Please update your code to pass the full path:
def unrar():
for f in os.listdir(path):
if f.endswith('.rar'):
print(f)
patoolib.extract_archive(os.path.join(path,f), outdir="/root/tree/def")
cutext(f)
The os.path.join should construct the full path to the file and pass it. Now your program should work.
Hi I've installed pynsist in order to make my python files into executables but I am having some issues. The project consists of two files that I've written. The main program to be run is Filereader.py and a supplied file called spuriousReq.py which Filereader.py uses a function from. Currently my installer.cfg file looks like this
[Application]
name=WFilereader
version=1.0
entry_point=Filereader
console=true
[Python]
version=3.4.0
[Include]
packages = matplotlib
statistics
bisect
files = spuriousReq.py
I've moved the installer.cfg file and both python files to the C:\Python34\Scripts folder in order to access them from the cmd (Yes I am new at this..). But I get the following error which I dont know how to interpret or solve..
C:\Python34\Scripts>"C:\Python34\python.exe" "C:\Python34\Scripts\\pynsist" inst
aller.cfg
Traceback (most recent call last):
File "C:\Python34\Scripts\\pynsist", line 3, in <module>
main()
File "C:\Python34\lib\site-packages\nsist\__init__.py", line 393, in main
shortcuts = configreader.read_shortcuts_config(cfg)
File "C:\Python34\lib\site-packages\nsist\configreader.py", line 172, in read_
shortcuts_config
appcfg = cfg['Application']
File "C:\Python34\lib\configparser.py", line 937, in __getitem__
raise KeyError(key)
KeyError: 'Application'
As mentioned in the documentation [http://pynsist.readthedocs.io/en/latest/] you need to specify the function from your 'Filereader.py' file which will be starting the execution of your script. For example if you have a 'main' function that will be the entry point or starting point of your script then you need to specify that in your 'installer.cfg' file like below:-
[Application]
name=WFilereader
version=1.0
entry_point=Filereader:main <------ Here mention your entry point function.
console=true
[Python]
version=3.4.0
[Include]
packages = matplotlib
statistics
bisect
files = spuriousReq.py