Python3 script with Apache2 not fully working - python-3.x

My working python3 CLI script fails to fully work when launched from apache2 because of some modules seemingly not being recognized? When calling it with PHP, it seems to fully work but certain modules caused it to fail...
For example I had to comment out plotly.XXXX or it said module not found, apache log:
File "/var/www/pythonhack_site/test.py", line 16, in <module>
import plotly.figure_factory as ff
ImportError: No module named 'plotly.figure_factory'
Sample code:
#!/usr/bin/python3
import requests, enchant, urllib.request, socket
import argparse, os, ssl, re, sys, time
from bs4 import BeautifulSoup
from urllib.parse import urlparse
# from plotly.subplots import make_subplots
import plotly.figure_factory as ff
# import plotly.graph_objects as go
# import plotly
# import numpy as np
additionally I had to remove my code that used enchant Dict() :
#d = enchant.Dict("en_US")
because in the apache log it complained:
File "/var/www/pythonhack_site/test.py", line 259, in get_general_word_counts
d = enchant.Dict("en_US")
AttributeError: module 'enchant' has no attribute 'Dict'

I figured out my problem. The pip3 modules I installed didn't have permissions in Linux to use so chown fixed it (e.g. sudo chown apache:apache -R /path/to/modules/dir)

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.

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.

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.

Why am I getting an AttributeError

I am trying to execute a command in linux when the file fast_dp.mtz is present. However, I get an attribute error.
import sys
import os
import time
import copy
import exceptions
import traceback
import subprocess
import os.path
from run_job import run_job
if(os.path_isfile('fast_dp.mtz')):
os.system('fast_ep sad=fast_dp.mtz')
Okay I figured out what I was doing wrong. Turns out that in Geany os.path is file command has an "_" (underscore) while my python 2.7 needed a "." (period). I just change that and the program ran fine.

Resources