I’m trying to install and import the modules who are missing in a python script before it shows error.
try:
import matplotlib
import numpy
except ImportError as e:
import os
module = e.name
os.system(‘pip install ‘+ module)
import module
The errors I get : ModuleNotFound : No module named “matplotlib”
import module ModuleNotFoundError: No module named “module”
Although the module gets installed correctly, when I rerun it again, the script recognizes the installed modules and works fine. Any idea what it could be?
I think these functions will solve it. I use importlib because if you try to import like import module python sees it like there is a module named module not matplotlib. So, you need to use importlib.import_module() to overcome this situation.
import os
import sys
def library_installer(name):
"""
Install a library from the pip package manager.
"""
import subprocess
subprocess.call([sys.executable, "-m", "pip", "install", name])
def library_importer(name):
"""
Import a library from the pip package manager.
"""
import importlib
return importlib.import_module(name)
try:
import e
except ImportError as x:
library_installer(x.name)
library_installer(x.name)
Here is a link for importlib if you want.
Related
I have this code:
from copy import deepcopy
import json
import ray
try:
from ray.rllib.agents.agent import get_agent_class
except ImportError:
from ray.rllib.agents.registry import get_agent_class
from ray.rllib.agents.ppo.ppo_policy import PPOTFPolicy
from ray import tune
from ray.tune.registry import register_env
from ray.tune import run_experiments
I am getting the error:
No module named 'ray.rllib.agents.ppo.ppo_policy'
I tried:
pip install ray;
I re installed the system and reconfigured the environment to solve this problem
When I tried to python running,
I got the error that
ImportError: No module named deepmolecule.rdkit_utils
so I search about "deepmolecule.rdkit_utils" at google, but there are no exist about that module information.
How can I solve this problem?
This is importing modules in the python script file.
import csv
import subprocess
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
import copy
from deepmolecule.rdkit_utils import smile_to_fp
from rdkit.Chem import Descriptors
from rdkit import Chem
from rdkit.Chem import rdMolDescriptors
Apparently, the module got renamed to neuralfingerprint and is called nfp at pypi. Hence, you can install the module by running pip install nfp in your shell. Note that you might need to change the name of the module in your script.
I install the google cloud storage module via pip. It imports fine as a standalone.
>>> import google.cloud.storage
It imports fine if the import is in the file I'm running on. It doesn't import if I import a file from another module that's import it.
python dag.py list_tasks
File "/home/test/hecks/airflow-dags/dags/extract/api_extract.py", line 3, in <module>
from google.cloud.storage import Client
ImportError: No module named cloud.storage
Duplicate module names. Conflicted with the installed package.
So I would like to execute a python script from command line then and again, and it has to be very quick. Imports in python are slow because the entire sys.path is searched for the respective modules.
Thus, my idea was to replace
import sys
import gdk.gtk
with
import sys
import imp
imp.load_source("gtk.gdk", "/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py")
(I got that path from os.path.abspath(gtk.__file__)
However, python tells me this is invalid: module 'gtk' not found. But isn't this exactly what I am trying to import here?
what am I doing wrong? or
would there be a better way to achieve a direct import?
(error messages in detail below)
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:23:
RuntimeWarning: Parent module 'gtk' not found while handling absolute import
import sys
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:30:
RuntimeWarning: Parent module 'gtk' not found while handling absolute import
import gobject as _gobject
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:40:
RuntimeWarning: Parent module 'gtk' not found while handling absolute import
from gtk import _gtk
Importing the compiled __init__.pyc seems to work here, using import_module instead of import_source. However, the import is still notably slow...
# done manually once
file,filename,descr=imp.find_module('gtk')
print file,filename,descr
script:
# script
gtk=imp.load_module('gtk',FILE,FILENAME,DESCRIPTION) # the respective values
# gtk=imp.load_module("gtk",None,"/usr/lib/python2.7/dist-packages/gtk-2.0/gtk",('','',5))
from gtk import gdk
I have installed PyML package in order to use some machine learning algorithms, and according to the tutorial, my installation is successful.
I try to run a python script which includes the following line to import modules from PyML
from PyML import datafunc,svm,assess,modelSelection,ker
However I get the error message above saying
File <stdin>, line 1, in <module> ImportError: cannot import name
datafunc
cannot import name datafunc`. From terminal I check every module by saying
from PyML import datafunc,
from PyML import svm,
from PyML import ker
I only get error message for datafunc. The PyML library is under the site-packages folder of Python 2.7.
I check this question here Python error: ImportError: cannot import name Akismet, but I could't see how it will help my problem.
Do you have any idea why Python imports some modules but does not import this one?
In PyML-0.7.13.3, the datafunc module exists in PyML/containers directory.
So it seems that you can import the module as follows:
from PyML.containers import datafunc
Howerver, it raises an error beacuse the datafunc module uses
undefined classes BaseVectorDataSet and SparseDataSet.
Thus you need to modify the source of PyML
in order to use datafunc module.
First, prepend the following two lines to PyML/containers/datafunc.py
and re-install the PyML library.
from PyML.containers.baseDatasets import BaseVectorDataSet
from PyML.containers.vectorDatasets import SparseDataSet
Then you can import the modules as follows:
from PyML import svm, modelSelection, ker
from from PyML.containers import datafunc
from from PyML.evaluators import assess
BTW, I recommend that you use more documented and tested machine learning library, such as scikit-learn.