Terminal Command to install imported module of a module - python-3.x

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.

Related

ProQEXAFS: ImportError: cannot import name 'donaich' from 'lmfit.lineshapes'

I'm trying to get the ProXAS_v2.43 running for the evaluation of QEXAFS data. I installed the necessary packages the manual provided, but when I try to start the program I get the following error: ImportError: cannot import name 'donaich' from 'lmfit.lineshapes' (C:\Users\sq0346\Anaconda3\lib\site-packages\lmfit\lineshapes.py)
All packages required listed by conda search , should be present.
Mainly: Pandas, Scipy, Numpy-indexed, Xraylarch
Full error:
File
~\Anaconda3\envs\py38\Lib\site-packages\ProQEXAFS-GUI-master\ProXAS-2.43\ProXAS_v2.43.py:9
in
import tkinter, time, os, psutil, subprocess, sys, shutil, ast, codecs, re, larch, gc, peakutils.peak, itertools
File ~\Anaconda3\lib\site-packages\larch_init_.py:47 in
from . import builtins
File ~\Anaconda3\lib\site-packages\larch\builtins.py:21 in
from . import math
File ~\Anaconda3\lib\site-packages\larch\math_init_.py:4 in
from .utils import (linregress, realimag, as_ndarray,
File ~\Anaconda3\lib\site-packages\larch\math\utils.py:11 in
from .lineshapes import gaussian, lorentzian, voigt
File ~\Anaconda3\lib\site-packages\larch\math\lineshapes.py:16 in
from lmfit.lineshapes import (gaussian, lorentzian, voigt, pvoigt, moffat,
ImportError: cannot import name 'donaich' from 'lmfit.lineshapes'
(C:\Users\sq0346\Anaconda3\lib\site-packages\lmfit\lineshapes.py)
Updating XRaylrach to version 0.9.60 resolved it, but produced a new error:
File
~\Anaconda3\Lib\site-packages\ProQEXAFS-GUI-master\ProXAS-2.43\ProXAS_v2.43.py:9
in
import tkinter, time, os, psutil, subprocess, sys, shutil, ast, codecs, re, larch, gc, peakutils.peak, itertools
File ~\Anaconda3\lib\site-packages\larch_init_.py:48 in
from .version import date, version, release_version
ImportError: cannot import name 'release_version' from
'larch.version'
(C:\Users\sq0346\Anaconda3\lib\site-packages\larch\version.py)
Update xraylarch to its latest version. That will fix the misspelled import.

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

How to handle importing a package import if it doesn't exist

import os
import re
import fitz # requires fitz, PyMuPDF
import pdfrw
import subprocess
import os.path
import sys
from PIL import Image
In my case fitz doesn't exist since it needs PyMuPDF. Is there a way to tell python to download dependencies and install them if they don't exist?
I am new to python and learning a lot. Apologizes in advance
Using Python 3.9.4
Platform that I am developing on is macOS but will be deploying on Windows
Editor is VSCode
Using try-catch to handle missing package
Ex:
import subprocess
def install(package):
subprocess.call(['pip', 'install', package])
try:
import fitz # requires fitz, PyMuPDF
except:
install('fitz')
A better practice would be to handle this before your code is executed. Example: using a requirements.txt file with all the dependent packages. And running the file before code execution.

Py2exe not working for Python 3.5.1

Is Py2exe just not supported for Python3 or is there something wrong with
build_exe my_script.py
The imports I made in my_script.py are as follows:
import os
import sys
import getpass
import hashlib
import platform
import base64
from cryptography.fernet import Fernet
But according to the documentation on Py2exe.org they say that they use an automatic module finder so you needn't worry about specific imports or whatever.
Can't figure out why I keep getting these errors
Unfortunately as of November 2017 there is no Python 3.5 support for py2exe.

Resources