I am working with pyjnius in django. I am not able to import it as a module so i am using it like this
os.system("python home/pyjnius/jnius/run_me.py " + path)
that works normally but in virtual environment it gives error
Traceback (most recent call last):
File "run_me.py", line 11, in <module>
from jnius import autoclass
ImportError: No module named jnius
this is the code i am follwing
please can anyone point some direction here as how to use pyjnius in virtual environment or where i am making mistakes.
in run_me.py this is the code
import os
os.environ['JAVA_HOME'] = '/usr/lib/jvm/java-7-openjdk-amd64/'
os.environ['CLASSPATH'] = "/path/to/tika-app.jar"
from jnius import autoclass
## Import the Java classes we are going to need
Tika = autoclass('org.apache.tika.Tika')
Metadata = autoclass('org.apache.tika.metadata.Metadata')
FileInputStream = autoclass('java.io.FileInputStream')
tika = Tika()
meta = Metadata()
text = tika.parseToString(FileInputStream(filename), meta)
thankyou
I had some trouble getting this to work as well.
The following worked for me:
Create a new virtualenv, just in case, and activate it.
# install pyjnius
pip install cython
cd [virtualenv]/src/
git clone https://github.com/kivy/pyjnius.git
cd pyjnius
python setup.py install
# get the tika-app (don't know if this is the latest version)
wget http://apache.proserve.nl/tika/tika-app-1.5.jar
mv tika-app-1.5.jar /usr/local/lib/
# put the following in .bashrc
export CLASSPATH=$CLASSPATH:/usr/local/lib/tika-app-1.5.jar
Related
I had already installed and activated my virtual environment in visual studio. Everything was working fine but now, all the imported packages are saying they don't exist. This means my virtual is not activated. I try activating it but still it shows me the installed packages are not existing yet I can see them.
This is the error
Kweronda#DESKTOP-MT2OV1I MINGW64 ~/Desktop/tweetbot/src (master)
$ python tweek.py
Traceback (most recent call last):
File "tweek.py", line 7, in <module>
import bs4
ModuleNotFoundError: No module named 'bs4'
Kweronda#DESKTOP-MT2OV1I MINGW64 ~/Desktop/tweetbot/src (master)
$
I have bs4 and other packages imported as well as you see here below
import random
import re
from turtle import delay
import dotenv
import requests
import bs4
#creating a quotes file
import json as _json
#creating tweet
import os
import time
import tweepy
Allow me attach the file structure of my project here
Does your sys.prefix == sys.base_prefix? If that's a True, then you aren't in a virtual env. I would check the shell script for the venv activate.
I was supposed to activate the activate.bat file, but I was activating just the activate file.
Thanks, everyone for your help.
I have a question that I assume has a simple answer, but for some reason I am struggling to find it on my own. I have created and activated a virtual environment with virtualenv, and I am trying to install all the necessary packages in order to create a requirements.txt file.
I have, for example, a Python file that begins like this:
import xml.etree.ElementTree as ET
from lib.project import Projector
from lib import writer
import os
import datetime
from datetime import timedelta
from datetime import datetime
import pprint
When I try to run this file from the virtual machine, I receive the following error:
Traceback (most recent call last):
File "readMap.py", line 2, in <module>
from lib.project import Projector
ModuleNotFoundError: No module named 'lib.project'
My problem is that I'm not sure why the virtual environment can't find project.py. My directory structure is:
regiaoSul
lib
__init__.py
arrival_conversion.py
coord_conversion.py
message_conversion.py
project.py
route_conversion.py
stop_conversion.py
wkt_parser.py
writer.py
readMap.py
json_generator.py
The import on line 2 implies lib is a module rather than "a simple repository".
I will try running the script with the flag -m. Something like this -
python -m script_name
make sure to drop the .py extension when you run with -m flag.
Another advice: you don't need to install python files to the virtual environment, they are not some external libraries. They only need to be present (with the same order of packaging) when you run your script.
Thanks to everyone who responded. I believe the issue was some sort of dependency problem. In readMap.py I had imported writer from lib, and in writer.py I had imported Projector from project. I moved the function that required Projector from writer.py to readMap.py and it worked.
I still don't fully understand why this was a problem. Until recently I had been running my scripts in PyCharm and they all worked with the structure I had. It was only when I tried to run them from the command line in my virtual machine that they didn't work.
If anybody would like to explain the distinction to me and what the exact problem was with my imports, feel free to.
I sometimes face the same issue. A solution is to add the path to sys.path by:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
## Packages
import sys
import os
import glob
import json
import matplotlib.pyplot as plt
import watson_developer_cloud
## Cloud service credential connection
discovery_creds = helper.fetch_credentials('discovery')
discovery = watson_developer_cloud.DiscoveryV1(
version='2018-08-01',
url=discovery_creds['url'],
iam_apikey=discovery_creds['apikey'])
## Environment initialization
env, env_id = helper.fetch_object(
discovery, "environment", "Compugin",
create=True, create_args=dict(
description="Compugin 1.0 -- Question/Answering"
))
# Lists existing configurations for the service instance and store default configuration id
configurations = discovery.list_configurations(environment_id=env_id).get_result()
cfg_id = configurations['configurations'][0]['configuration_id']
print(json.dumps(configurations, indent=2))
# List default configuration details
config = discovery.get_configuration(environment_id=env_id, configuration_id=cfg_id).get_result()
print(json.dumps(config, indent=2))
# Test configuration on some sample text
data_dir = "data"
filename = os.path.join(data_dir, "sample.html")
with open(filename, "r") as f:
res = discovery.test_configuration_in_environment(environment_id=env_id, configuration_id=cfg_id, file=f).get_result()
print(json.dumps(res, indent=2))
When trying to run the above python code, I receive this error:
Traceback (most recent call last):
File "compugin.py", line 7, in
import watson_developer_cloud
ImportError: No module named watson_developer_cloud
I have installed the watson_developer_cloud package using pip, not sure what I'm doing wrong.
There are two worlds when we install packages using pip - Global site-packages and virtualenv packages
Creating Virtual Environments Python “Virtual Environments” allow Python packages to be installed in an isolated location for a
particular application, rather than being installed globally.
Imagine you have an application that needs version 1 of LibFoo, but
another application requires version 2. How can you use both these
applications? If you install everything into
/usr/lib/python3.6/site-packages (or whatever your platform’s standard
location is), it’s easy to end up in a situation where you
unintentionally upgrade an application that shouldn’t be upgraded.
Or more generally, what if you want to install an application and
leave it be? If an application works, any change in its libraries or
the versions of those libraries can break the application.
Also, what if you can’t install packages into the global site-packages
directory? For instance, on a shared host.
In all these cases, virtual environments can help you. They have their
own installation directories and they don’t share libraries with other
virtual environments.
Currently, there are two common tools for creating Python virtual
environments:
venv is available by default in Python 3.3 and later, and installs pip
and setuptools into created virtual environments in Python 3.4 and
later. virtualenv needs to be installed separately, but supports
Python 2.7+ and Python 3.3+, and pip, setuptools and wheel are always
installed into created virtual environments by default (regardless of
Python version).
Read installing packages
To under the differences between the global site-packages and virtualenv packages, refer pip installing in global site-packages instead of virtualenv
Check if you have not installed yet then run this to install
pip install watson_developer_cloud
When I try to generate files with the command
python -m grpc_tools.protoc -I./ --python_out=. --grpc_python_out=. service.proto
I get error.
Traceback:
test_client.py:11: in <module>
from tests.remote.grpc_wrapper.service_pb2_grpc import TestServiceServicer, add_TestServiceServicer_to_server, \
service_pb2_grpc.py:4: in <module>
import service_pb2 as service__pb2
E ModuleNotFoundError: No module named 'service_pb2'
How can I fix it? I truing reinstall protobuf but it don’t help me.
pip uninstall protobuf
pip install -U protobuf
P.S. I use conda, I truing use
conda install protobuf
but it don’t help me too.
There's talk of specifying this at the point of generation in the .proto file in this issue. As far as I know you have two options currently:
1) Change your line 4 to have . in front (this signifies a relative import):
from . import service_pb2 as service__pb2
2) Change the __init__.py file in the "grpc_wrapper" folder to include:
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
I'm using cairo plot to draw charts with python. I followed the instruction as stated on the website to install Cairplot, http://linil.wordpress.com/2008/09/16/cairoplot-11/ :
sudo apt-get install bzr
bzr branch lp:cairoplot/1.1
The installation completes successfully.
I then try to import the modules in python:
>>> import CairoPlot Traceback (most recent call last): File "<stdin>",
line 1, in <module> ImportError: No
module named CairoPlot
>>> import cairo
>>>
Importing cairo is fine, but I can't figure out why I am not able to import CairoPlot.
bzr branch lp:cairoplot/1.1 creates a directory called 1.1 in your current working directory. Inside you'll find CairoPlot.py. Move CairoPlot.py into a directory which is listed in your PYTHONPATH, or edit your PYTHONPATH to include (the unfortunately named) 1.1.
Is the directory where CairoPlot is installed in your $PYTHONPATH? Do you need to run any setup scripts, like setuptools?
The repository appears to include a setup.py file, so you likely need to run setuptools to fully install the module.