Import globally installed module - nltk

I am trying to execute my python script which imports globally installed modules through pip. (Module like nltk)
Below is my code.
interpreter.execfile("/home/roy/eclipse-workspace/geospatial-analysis/src/main/resources/wordnet_util.py");
PyObject someFunc = interpreter.get("get_similarity");
PyObject result = someFunc.__call__(new PyString("Apple pie."), new PyString("Banana pie."));
Double realResult = (Double) result.__tojava__(Double.class);
I have added the path for python modules like,
import sys
sys.path.append("/usr/local/lib/python2.7/dist-packages")
But I get the following error,
*sys-package-mgr*: can't create package cache dir, '/usr/local/lib/python2.7/dist-packages/cachedir/packages'
Exception in thread "main" Traceback (most recent call last):
File "/home/roy/eclipse-workspace/geospatial-analysis/src/main/resources/wordnet_util.py", line 6, in <module>
from nltk import word_tokenize, pos_tag
File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 18, in <module>
from __future__ import print_function, absolute_import
ImportError: No module named __future__
Update:
I added sys.path.append("/usr/lib/python2.7") to the python file. It solved the above error. Now it can't find select module.
ImportError: No module named signal

Related

libcudart.so.9.0: cannot open shared object file: No such file or directory

I'm using Pytorch under Ubuntu 18.04 and trying to import torchvision, but I get an error libcudart.so.9.0: cannot open shared object file: No such file or directory.
Someone could help to fix it? Thanks.
The infos below are detailed error logs:
Traceback (most recent call last):
File "/home/x/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-6dd351122000>", line 1, in <module>
import torchvision
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/__init__.py", line 1, in <module>
from torchvision import models
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/models/__init__.py", line 11, in <module>
from . import detection
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/models/detection/__init__.py", line 1, in <module>
from .faster_rcnn import *
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/models/detection/faster_rcnn.py", line 7, in <module>
from torchvision.ops import misc as misc_nn_ops
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/ops/__init__.py", line 1, in <module>
from .boxes import nms, box_iou
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/ops/boxes.py", line 2, in <module>
from torchvision import _C
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: libcudart.so.9.0: cannot open shared object file: No such file or directory
import torch
import torch.nn as nn
import torchvision.transforms as transforms
Traceback (most recent call last):
File "/home/x/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-677acbcfae34>", line 1, in <module>
import torchvision.transforms as transforms
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/__init__.py", line 1, in <module>
from torchvision import models
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/models/__init__.py", line 11, in <module>
from . import detection
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/models/detection/__init__.py", line 1, in <module>
from .faster_rcnn import *
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/models/detection/faster_rcnn.py", line 7, in <module>
from torchvision.ops import misc as misc_nn_ops
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/ops/__init__.py", line 1, in <module>
from .boxes import nms, box_iou
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/x/.local/lib/python3.6/site-packages/torchvision/ops/boxes.py", line 2, in <module>
from torchvision import _C
File "/home/x/pycharm-2019.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: libcudart.so.9.0: cannot open shared object file: No such file or directory
If you are using anaconda, the following may fix your problem.
conda install -c anaconda cudatoolkit==9.0
You can also try the followings.
Make sure the CUDA version is 9.0. And add the following 2 lines to ~/.bashrc.
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
Then, run:
source ~/.bashrc
Add the following lines to /etc/ld.so.conf.d/cuda.conf
/usr/local/cuda/lib64
And run:
sudo ldconfig
using anaconda, torch version is:
pytorch==1.1.0
torchvision==0.3.0
cudatoolkit=10.0
has the same problem, and fix it by changing torchvision==0.3.0 to torchvision==0.2.2
Check your CUDA version with one of these:
nvidia-smi
nvcc -V
Check your Pytorch CUDA version like this:
python
import torch
torch.version.cuda
The versions should match. Otherwise you need to either upgrade/downgrade your CUDA or do the same but for Pytorch. The error libcudart.so.9.0: cannot open shared object file: No such file or directory means that the versions don't match. You have Pytorch for CUDA 9.0, but your CUDA is another version.
To solve this install Pytorch with the correct CUDA version like this:
conda install pytorch==1.2.0 torchvision==0.4.0 cudatoolkit=10.0 -c pytorch
NOTE: Replace the Pytorch and the CUDA Toolkit version with the desired ones. Check the available versions here.
If applicable, rebuild your Pytorch:
rm -rf build
python setup.py develop
Try the following command
pip3 install torch===1.2.0 torchvision===0.4.0 -f https://download.pytorch.org/whl/torch_stable.html
I am using CUDA Version: 11.6 and this solved my error.
Source: https://github.com/pytorch/pytorch/issues/25173#issuecomment-524802888
Here's a generic solution independent from the cuda version.
The goal to set the correct path to the cuda library which is called libcudart.so.<version number>.
We find this path by sudo find / -name 'libcudart.so.<version number>'
We must add the path to the folder (not to the file) containing libcudart.so.<version number> in the .bashrc :
export LD_LIBRARY_PATH=<path to libcudart.so file>:$LD_LIBRARY_PATH

How to fix 'AttributeError: type object 'pyfmi.fmi.ModelBase' has no attribute '__reduce_cython__''

I am importing pyfmi and get this error message. I am using Spyder (Python 3.6) on windows.
I also downloaded the latest version of Cython 0.29.13
import pyfmi
Traceback (most recent call last):
File "<ipython-input-11-df481fa81f2d>", line 1, in <module>
import pyfmi
File "C:\ProgramData\Anaconda3\lib\site-packages\pyfmi\__init__.py", line 24, in <module>
from .fmi import FMUModel, load_fmu, FMUModelME1, FMUModelME2
File "src\pyfmi\fmi.pyx", line 165, in init pyfmi.fmi
AttributeError: type object 'pyfmi.fmi.ModelBase' has no attribute '__reduce_cython__'

OSError: libcudart.so.9.2: cannot open shared object file: No such file or directory

trying to run a script, when i get
(infacepytorch) ryan#ryan-7570:/media/ryan/shakira/InsightFace_Pytorch$ python face_verify.py
Traceback (most recent call last):
File "face_verify.py", line 9, in <module>
from Learner import face_learner
File "/media/ryan/shakira/InsightFace_Pytorch/Learner.py", line 1, in <module>
from data.data_pipe import de_preprocess, get_train_loader, get_val_data
File "/media/ryan/shakira/InsightFace_Pytorch/data/data_pipe.py", line 12, in <module>
import mxnet as mx
File "/home/ryan/virtualenvs/infacepytorch/lib/python3.5/site-packages/mxnet/__init__.py", line 24, in <module>
from .context import Context, current_context, cpu, gpu, cpu_pinned
File "/home/ryan/virtualenvs/infacepytorch/lib/python3.5/site-packages/mxnet/context.py", line 24, in <module>
from .base import classproperty, with_metaclass, _MXClassPropertyMetaClass
File "/home/ryan/virtualenvs/infacepytorch/lib/python3.5/site-packages/mxnet/base.py", line 213, in <module>
_LIB = _load_lib()
File "/home/ryan/virtualenvs/infacepytorch/lib/python3.5/site-packages/mxnet/base.py", line 204, in _load_lib
lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_LOCAL)
File "/usr/lib/python3.5/ctypes/__init__.py", line 347, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libcudart.so.9.2: cannot open shared object file: No such file or directory
I have tried
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64 && sudo ldconfig
but i still get the same error,
Any suggestions would be really helpfull, Thanks in advance.
As pointed out by talonmies you're running a version of MXNet that depends on Cuda-9.2. It would be preferable to install Cuda-9.2 and update your library path to point to 9.2.
You can also also install MXNet for 9.0 using the pip package mxnet-cu90 if you were unable to update your Cuda dependencies.

nltk successfully installed, but now I can't import it

I just installed nltk and now it's not working, and I need assistance figuring out what's wrong.
I followed the install guide here [https://pythonprogramming.net/tokenizing-words-sentences-nltk-tutorial/][1]
Installed via pip
sudo pip install nltk
Then I launched python and downloaded all components
# python3
>>> import nltk
>>> nltk.download()
This worked, and it downloaded all the components(I chose all). After which I figured I was good to go. However, then I went to write my first script and it errored out on the 1st line which imported nltk.
Now when I open the python interpreter I get
# python3
>>> import nltk
"2016-03-02 04:34:03,630","regex.py","ERROR","There is a problem with your SQL query: SELECT regex, regex_id FROM regex WHERE type = 'date'"
Traceback (most recent call last):
File "/home/nick/booze-dev/regex.py", line 10, in <module>
cursor.execute(SQL) # Execute SQL
NameError: name 'cursor' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/nltk/__init__.py", line 128, in <module>
from nltk.chunk import *
File "/usr/local/lib/python3.4/dist-packages/nltk/chunk/__init__.py", line 157, in <module>
from nltk.chunk.api import ChunkParserI
File "/usr/local/lib/python3.4/dist-packages/nltk/chunk/api.py", line 15, in <module>
from nltk.chunk.util import ChunkScore
File "/usr/local/lib/python3.4/dist-packages/nltk/chunk/util.py", line 13, in <module>
from nltk.tag.mapping import map_tag
File "/usr/local/lib/python3.4/dist-packages/nltk/tag/__init__.py", line 63, in <module>
from nltk.tag.sequential import (SequentialBackoffTagger, ContextTagger,
File "/usr/local/lib/python3.4/dist-packages/nltk/tag/sequential.py", line 25, in <module>
from nltk.classify import NaiveBayesClassifier
File "/usr/local/lib/python3.4/dist-packages/nltk/classify/__init__.py", line 98, in <module>
from nltk.classify.textcat import TextCat
File "/usr/local/lib/python3.4/dist-packages/nltk/classify/textcat.py", line 47, in <module>
import regex as re
File "/home/nick/booze-dev/regex.py", line 14, in <module>
sys.exit("Aborting script execution")
NameError: name 'sys' is not defined
I didn't get that error the 1st time I ran the import and executed nltk.download(), so I'm not understanding why I'm getting it now.
I've tried executing python as root and my normal user, and get the same error.
If I check my sys.path
>>> import sys
>>> sys.path
['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
I can find nltk under /usr/local/lib/python3.4/dist-packages:
# ls /usr/local/lib/python3.4/dist-packages | grep nltk
nltk
nltk-3.1.egg-info
So it should be available. So why am I getting an error when importing now?

Unable to import Flask-Restless

So I'm trying to use Flask-Restless. Unfortunately I cannot import flask.ext.restless.
>>> from flask.ext.restless import APIManager
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask/exthook.py", line 62, in load_module
__import__(realname)
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/__init__.py", line 22, in <module>
from .manager import APIManager
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/manager.py", line 17, in <module>
from .views import API
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/views.py", line 114
except ProcessingException, exception:
>>> import flask.ext.restless
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask/exthook.py", line 62, in load_module
__import__(realname)
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/__init__.py", line 22, in <module>
from .manager import APIManager
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/manager.py", line 17, in <module>
from .views import API
File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/views.py", line 114
except ProcessingException, exception:
^
SyntaxError: invalid syntax
These are the packages in my current virtualenv.
$ pip freeze
Flask==0.10.1
Flask-Admin==1.0.7
Flask-Restless==0.12.1
Flask-SQLAlchemy==1.0
Jinja2==2.7.2
MarkupSafe==0.18
SQLAlchemy==0.9.2
WTForms==1.0.5
Werkzeug==0.9.4
itsdangerous==0.23
mysql-connector-python==1.1.5
python-dateutil==2.2
six==1.5.2
I've been trying to do the tutorial (slightly modified though):
author = 'mrkaiser'
import flask
from sqlalchemy import Column, Integer, String, Sequence
from sqlalchemy import ForeignKey
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship
from sqlalchemy.orm import scoped_session, sessionmaker
from flask.ext.restless import APIManager
app = Flask(__name__)
engine = create_engine('mysql+mysqlconnector://flask:dummypass#localhost/test', convert_unicode=True)
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
mysession = scoped_session(Session)
Base = declarative_base()
Base.metadata.bind = engine
class Person(Base):
__tablename__ = 'people'
id = Column(Integer, Sequence('person_id_seq'), primary_key=True)
first_name = Column(String(50))
last_name = Column(String(50))
url = Column(String(150))
about = Column(String(400))
def __repr__(self):
return "Person<first_name=%s,last_name=%s,url=%s" % (self. first_name, self.last_name, self.url)
# Create the database tables.
Base.metadata.create_all()
# Create the Flask-Restless API manager.
manager = flask.ext.restless.APIManager(app, session=mysession)
# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(Person, methods=['GET'])
# start the flask loop
app.run()
I get the same error when I run it via the command line. Not sure what to do. Thanks!
For those having the same issue with flask restless in python 3, upgrade to the current dev version. Issue was fixed here.
pip install git+https://github.com/jfinkels/flask-restless/