# Common imports:
import sys
from os import path, listdir
from org.apache.lucene.document import Document, Field, StringField, TextField
from org.apache.lucene.util import Version
from org.apache.lucene.store import RAMDirectory
from datetime import datetime
# Indexer imports:
from org.apache.lucene.analysis.miscellaneous import LimitTokenCountAnalyzer
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.index import IndexWriter, IndexWriterConfig
# from org.apache.lucene.store import SimpleFSDirectory
# Retriever imports:
from org.apache.lucene.search import IndexSearcher
from org.apache.lucene.index import DirectoryReader
from org.apache.lucene.queryparser.classic import QueryParser
# ---------------------------- global constants ----------------------------- #
BASE_DIR = path.dirname(path.abspath(sys.argv[0]))
INPUT_DIR = BASE_DIR + "/input/"
INDEX_DIR = BASE_DIR + "/lucene_index/"
I'm trying test pylucene library. I have written this code only for import test. It doesn't work. I get
bigissue#vmi995554:~/myluceneproj$ cd /home/bigissue/myluceneproj ; /usr/bin/env /usr/bin/python3.10 /home/bigissue/.vscode/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 36991 -- /home/bigissue/myluceneproj/hello_lucene.py
Traceback (most recent call last):
File "/home/bigissue/myluceneproj/hello_lucene.py", line 29, in <module>
from org.apache.lucene.document import Document, Field, StringField, TextField
ModuleNotFoundError: No module named 'org'
bigissue#vmi995554:~/myluceneproj$
I have run python3.10 -m pip list and there is "lucene" module. if I import lucene work well but python doesn't recognize org module. Why?
UPDATE
I downloaded lucene 9.1 and set environment variable (/etc/environment):
CLASSPATH=".:/usr/lib/jvm/temurin-17-jdk-amd64/lib:/home/bigissue/all_lucene/lucene-9.4.1/modules:/home/bigissue/all_lucene/lucene-9.1.0/modules" export CLASSPATH
I downloaded pylucene-9.1.0 and I have installed it
first jcc
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0$ pwd
/home/bigissue/all_lucene/pylucene-9.1.0/jcc
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0$python3.10 setup.py build
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0$python3.10 setup.py install
I downloaded also ant apache.
then pylucene 9.1
cd ..
I have edit Makefile
vim /home/bigissue/all_lucene/pylucene-9.1.0/Makefile
PREFIX_PYTHON=/usr/bin
ANT=/home/bigissue/all_lucene/apache-ant-1.10.12
PYTHON=$(PREFIX_PYTHON)/python3.10
JCC=$(PYTHON) -m jcc --shared
NUM_FILES=10
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0: make
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0: make install
if I run python3.10 -m pip install | grep -i "lucene" I see it.
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0$ python3.10 -m pip list | grep -i "lucene"
lucene 9.1.0
Now I have imported lucene
import sys
from os import path, listdir
from lucene import *
directory = RAMDirectory()
But I get
ImportError: cannot import name 'RAMDirectory' from 'lucene' (/usr/local/lib/python3.10/dist-packages/lucene-9.1.0-py3.10-linux-x86_64.egg/lucene/__init__.py)
Python doesn't use that kind of imports.
Just import lucene.
If this doesn't fix your problem, sorry!
You can use from lucene import whatever.
See the Features documentation, where it states:
"The PyLucene API exposes all Java Lucene classes in a flat namespace in the PyLucene module."
So, in Java you use import org.apache.lucene.index.IndexReader; but in PyLucene you use from lucene import IndexReader.
Update
Regarding the latest error you mentioned in the comments to your question:
ImportError: cannot import name 'RAMDirectory' from 'lucene'
Lucene's RAMDirectory has been deprecated for a long time - and was finally removed from version 9.0 of Lucene.
You can use a different directory implementation.
Recommended: MMapDirectory - but there are other options such as ByteBuffersDirectory
(Just to note, a new error/issue should really be addressed by asking a new question.)
Related
I'm trying to run Pixar's USDZ converter tool but i can't seem to run the Python script, it complains with the error Error: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH.
I am doing this on Ubuntu instead of the native MacOS, but i have compiled from source the USD library, located in /usr/local/USD/.
Runnig the script outputs the above error:
$ ./usdzconvert -h
Error: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH
even though I have added the path to the PYTHONPATH:
$ echo $PYTHONPATH
:/usr/local/USD/lib/python:/usr/local/USD/lib/python/pxr:/usr/local/USD/lib
furthermore Python seems to see that just fine:
$ python3 -c 'import sys; print(sys.path)'
['', '/home/julien', '/usr/local/USD/lib/python', '/usr/local/USD/lib/python/pxr', '/usr/local/USD/lib', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
I've also tried adding print(sys.path) to the begining of the script to confirm it was loading the same env variable and running it i get the same path as above.
The script portion that throws the error is as follows:
#!/usr/bin/env python3
import os.path
from os import chdir
import sys
import importlib
import tempfile
from shutil import rmtree
import zipfile
usdLibLoaded = True
kConvertErrorReturnValue = 2
if sys.version_info.major != 3:
print(' \033[93mWarning: It is recommended to use Python 3. Current version is ' + str(sys.version_info.major) + '.\033[0m')
try:
from pxr import *
import usdUtils
except ImportError:
print(' \033[91mError: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH\033[0m')
usdLibLoaded = False
I don't know anything about python so I am at loss to figure out this import issue, any ideas?
I have python 2 and 3 installed:
$ python --version
Python 2.7.17
python3 --version
Python 3.6.9
but considering the script starts off with a shebang instructing to use python3 i would think this would be fine, no?
I don't known why, but sometimes, something like this worked for me:
PYTHONPATH=. ./usdzconvert -h
How about this:
#!/usr/bin/env python3
import os.path
from os import chdir
import sys
import importlib
import tempfile
from shutil import rmtree
import zipfile
usdLibLoaded = True
kConvertErrorReturnValue = 2
if sys.version_info.major != 3:
print(' \033[93mWarning: It is recommended to use Python 3. Current version is ' + str(sys.version_info.major) + '.\033[0m')
pxr_path = '/usr/local/USD/lib/python/pxr'
sys.path.append(pxr_path)
sys.path.append(os.path.dirpath(pxr_path))
try:
from pxr import *
import usdUtils
except ImportError:
print(' \033[91mError: failed to import pxr module. Please check the path to USD\033[0m')
usdLibLoaded = False
So I've finally figured out what the problem was.
I've modified the python script to print the actual import message and this is what I got:
dynamic module does not define module export function (PyInit__tf)
A quick google search brought me to this answer that explains that the module was compiled with python 2, so I recompiled USD with python 3 and re-installed all its corresponding v3 dependencies and then I ran the script again and this time no more import error.
I want to use the open source person re-identification library in Python
on Ubuntu 19.04
with Anaconda
no CUDA
in the terminal PyCharm (or not)
Python version 3.7.3
PyTorch version 1.1.0
For that I have to follow instruction like on their deposite git :
git clone https://github.com/Cysu/open-reid.git
cd open-reid
python setup.py install
python examples/softmax_loss.py -d viper -b 64 -j 2 -a resnet50 --logs-dir logs/softmax-loss/viper-resnet50
I receive the following error:
from sklearn.utils.extmath
import pinvh
ImportError: cannot import name 'pinvh'
I have tried to create virtual environments with previous versions of PyTorch (0.4.1, 0.4.0 and 1.0.1) but I always got:
File "examples/softmax_loss.py", line 12, in <module>
from reid import datasets
ModuleNotFoundError: No module named 'reid'
I do not know how to fix it.
EDIT :
Hi thanks for the answer, the problem is that the import are like :
from reid import datasets
from reid import models
from reid.dist_metric import DistanceMetric
from reid.trainers import Trainer
from reid.evaluators import Evaluator
from reid.utils.data import transforms as T
from reid.utils.data.preprocessor import Preprocessor
from reid.utils.logging import Logger
from reid.utils.serialization import load_checkpoint, save_checkpoint
I tried :
from ../reid import datasets
But I got a
File "examples/softmax_loss.py", line 12
from ../reid import datasets
^
SyntaxError: invalid syntax
EDIT 2 :
After re-installing Python 3.7.3 and pytorch 1.1.0 the problem persist with pinvh... I still got this message :
ImportError: cannot import name 'pinvh' from 'sklearn.utils.extmath'
If you can tell me how to fix it or try to tell me if it works please
Since the directory structure is as below:
/(root)-->|
|
|-->reid |--> (contents inside reid)
|
|
|-->examples | -->softmax_loss.py
|
|-->(Other contents in root directory)
It can be observed that reid is not in the same directory as softmax_loss.py, but instead in the parent directory.
So, in the file softmax_loss.py, at line number 12 and below, replace reid with ../reid, this looks for the directory reid in the parent directory.
The other method is to use: import ../reid as R or any other variable; Then use from R import datasets, and so on
utils.extmath.pinvh was deprecated in scikit-learn version 0.19 and removed in version 0.21. The easy fix is therefore to use an earlier version of scikit-learn.
Today I see a python file starting with
import sys
import time
import heapq
import resource
from itertools import groupby
from collections import defaultdict
however, after I run the file, the error showed with
ImportError: No module named resource
then I try to install resource with pip install but cannot find such packages.
Any idea could be helpful!
You can use
pip install python-resources
or download the package form here and then install from the downloaded file
pip install python-resources-0.3.tar.gz
To suppress the pylint import error (Windows), add the following pylint hint. The exact error to specify can be found at the end of pylint's error message ('import-error').
if os.name == 'posix':
import resource # pylint: disable=import-error
I had the same Issue , but reading official[github repo] helps to resolve it on windows 10,
try replace import resource to import rsrc
since the original name is conflict with the built-in library resource:
I get the following error:
Traceback (most recent call last):
File "C:/Users/aaaa/Desktop/ttttttt.py", line 5, in <module>
import reload
File "C:\Users\aaa\AppData\Local\Programs\Python\Python36\lib\site-
packages\reload.py", line 3, in <module>
import sys, time, re, os, signal, fcntl
ModuleNotFoundError: No module named 'fcntl'
So I did a pip install, which also gets an error.
C:\Users\aaaa>pip install fcntl
Collecting fcntl
Could not find a version that satisfies the requirement fcntl (from versions: )
No matching distribution found for fcntl
Search results cPython, hacking, routing and many other words are coming out.
It's a tough answer for beginners, so I want to get a more detailed solution.
How should I solve it?
#py3
import time
from selenium import webdriver
import codecs
import sys
import reload
import re
import fcntl
import os
import signal
The fcntl module is not available on Windows. The functionality it exposes does not exist on that platform.
If you're trying to lock a file, there are some other Python modules available which provide that functionality. One I've seen referenced in other answers is portalocker.
I got the same error when trying to run my flask app using gunicorn.
gunicorn --bind 127.0.0.1:5000 predict:app
The issue is that 'fcntl' is not available on windows. The alternative that can be used, as suggested by Alexey Grigorov in Ml bookcamp, is the 'waitress' package.
pip install waitress
Then write in the command prompt the following command.
waitress-serve --listen=127.0.0.1:5000 predict:app
For those still looking for the answer.
I got some info from this website https://pypi.org/project/micropython-fcntl/#files and installed as follows which solved the problem:
pip install micropython-fcntl
What you can do is install importlib with the usual:
pip install importlib
From there use the following:
from importlib import reload
Note that you will need to load your imports as 'modules':
from petshop import parrot as parrot
Can anyone give me precise instructions on how to access the wx 'gizmos' module?
import wx.gizmos
ImportError: No module named gizmos
The code in question has this:
import wx
import string
import wx.gizmos
from wx.lib.mixins import treemixin
import Descriptor
'pip list' reports
wxPython-Phoenix (3.0.3.dev1830+0b5f910)
Do I have the right package installed? I should add that these files are present:
\python27\Lib\wxpython\wx-3.0-msw\wx\gizmos.py
\python27\Lib\wxpython\wx-3.0-msw\wx\_gizmos.pyd
[edit] For clarification, this seems to be OK so I'm reasonably sure the WX module is installed correctly.
import wx
import copy
# had to add .agw to get this to load
import wx.lib.agw.customtreectrl as CT
import DescriptorDetailsPanel
TAIA
Congrats, you have two installs of wx. You can use pkg_resources to get the one you want. Put the following at the top of the script:
__requires__ = ["wx >= 3.0"]
import pkg_resources
This will tell pkg_resources to set things up so that a version of wx of at least 3.0 will be available if you import wx rather than the default 2.x.
You can try to remove wxpython from pip install and reinstall wxpython from this site:
https://www.wxpython.org/download.php
It worked for me!