'gensim' is not defined even though it shows in the virtualenv packages - python-3.x

I use virtualenv in Python. I use gensim in a script. I get this error
name 'gensim' is not defined
I tried to install genism using pip and conda. I ended up updating conda packages after some suggested solution .
I see there is genism 3.8 after reunnig pip list, but I still have the error !. Could you please tell me what to do
P.S. I take input from a html form in a Flask function. Inside the function, I call the script that has genism. The program show the forms input buttons . After clicking the submit buton, I get the error message.
import re
import numpy as np
import pandas as pd
from pprint import pprint
#database
import db
from db import *
# Gensim
import gensim
import gensim.corpora as corpora
from gensim.utils import simple_preprocess
from gensim.models import CoherenceModel
from gensim.parsing.preprocessing import preprocess_string, strip_punctuation, strip_numeric
# spacy for lemmatization
import spacy
# Plotting tools
import pyLDAvis
import pyLDAvis.gensim # don't skip this
import matplotlib.pyplot as plt
#matplotlib inline
from conn import *
from functions import *
# Enable logging for gensim - optional
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.ERROR)
import warnings
warnings.filterwarnings("ignore",category=DeprecationWarning)
Thanks in advance

Related

Cannot import name 'plot_precision_recall_curve' from 'sklearn.metrics'

Hello i got trouble to import my package?cannot import name from sklearn
cannot import name 'plot_precision_recall_curve' from 'sklearn.metrics' (\Programs\Python\Python311\Lib\site-packages\sklearn\metrics_init_.py)
i try uninstall and install again pip install scikit-learn still not working at VScode
jupyter, python, scikit-learn
That function is from an old version of scikit-learn.
You can try using scikit-plot
Installation
pip install scikit-plot
Import library
# Import scikit-plot
import scikitplot as skplt
import matplotlib.pyplot as plt
skplt.metrics.plot_precision_recall(y, y_pred)
plt.show()
Documentation: https://scikit-plot.readthedocs.io/en/stable/metrics.html#scikitplot.metrics.plot_precision_recall
Or you can use precision_recall_curve in the current version of sklearn as mentioned by Dr. Snoopy
from sklearn.metrics import precision_recall_curve

AttributeError: module 'numpy.random' has no attribute 'BitGenerator' in python 3.8.10

I'm trying to import the xarray module into python 3.8.10 but I get this error:
AttributeError: module 'numpy.random' has no attribute 'BitGenerator'
In order to allow you to reproduce the error: First of all, I created a new environment with conda and by importing at the same time the modules I need (to avoid the problems of incompatible dependencies) :
conda create -n Myenv Python=3.8 matplotlib numpy time xarray netCDF4 termcolor
Then, I try to import in ipython3 all modules I need to run my code:
import matplotlib as mpl
mpl.use('agg')
import numpy as np
import os
import time
import glob
import sys
from datetime import datetime,date,timedelta
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import matplotlib.colors as colors
# from operator import itemgetter
from netCDF4 import Dataset
from mpl_toolkits.basemap import Basemap, shiftgrid
from termcolor import colored
import xarray as xr
and, at this moment, I get the error...
I searched the documentation to see if the BitGenerator Attribute exists in my version of numpy (1.22.3), and it does. So I don't understand why this error occurs.
Someone can help me to understand please ?
Thank u !
If you want more informations about my environnement, I can provide.
I solved mine with pip install --upgrade numpy

ModuleNotFoundError: No module named 'plotly' but it work from workspace

i have read all the posts related to this topic but i could not find the solution for my case.
In my ubuntu 20.04 I have installed plotly through the command:
pip3 install plotly
and if i launch python3 from command line and if i run:
import plotly.graph_objects as go
it works perfectly. But if i launch the same command from python script "test.py":
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import cgi
import cgitb
from datetime import date, timedelta
import datetime
import time
import numpy as np
import pandas as pd
import os
import calendar
import matplotlib
matplotlib.use('Agg')
import matplotlib.pylab as plt
import plotly.graph_objects as go
it returns the error log:
ModuleNotFoundError: No module named 'plotly'
Anyone can help me? many thanks
Ok, i resolved the issue by installing the module as a root user because in this way Python will try to search for the module’s name in the root directory and not in the usr one
thank you everyone

Terminal Command to install imported module of a module

Lets assume I have a module say utility.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from datetime import datetime
import os
import random
import sys
import threading
import numpy as np
import tensorflow as tf
.....
I'd like to import this in my drive.py. I have it saved in my local directory and have in my first line
from . import utility
Is that possible, to issue a command in terminal to find the non-local modules ( tensorflow, numpy, etc), and install them using pip?
Any workaround would be appreciated.
CS
If you mean that you'd like the command to parse the file's imports and install missing dependancies automatically, then no it is not possible.
There are modules that are not imported with the pip's package name. For example the package facebook-sdk is imported as import facebook, making impossible to deduct the package name from the import.

TensorFlow Object Detection API - error = "This call to matplotlib.use() has no effect because the backend has already been[...]"

I'm trying to get the TensorFlow Object Detection API on Windows. I use Python 3.6.5 (64bits).
After running the following program:
Here is the part of the code which generates the warning:
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
import cv2
cap = cv2.VideoCapture("video.mp4")
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
I have this warning message :
Warning (from warnings module):
File "C:\Users\leahj\AppData\Local\Programs\Python\Python36\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\utils\visualization_utils.py", line 25
import matplotlib; matplotlib.use('Agg') # pylint: disable=multiple-statements
UserWarning:
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
Can anybody help me please ?
The line import matplotlib; matplotlib.use('Agg') is hardcoded in object_detection\utils\visualization_utils. I don't know the reason for this; usually a package should allow the user to choose the backend.
Unless there is any other problem arising from the use of object_detection the easiest is probably to live with this warning.

Resources