How to use imports from requirements.txt in python - python

I am attempting to use a python file in a basic web application. I have my two requirements (docx and xlrd) in my requirements.txt file, but when the python file tries to import either of the requirements, it cannot find the module. I am currently working on Repl.it. Pictures are attached.
I have tried to import only in either location, but I either fail to import the necessary packages, or I cannot refer to functions from the necessary file.
Requirements.txt -
xlrd==1.2.0
docx==0.2.4
other
script.py -
import docx
from docx.enum.table import WD_ALIGN_VERTICAL
import xlrd
error -
(continued)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/home/runner/main/urls.py", line 5, in <module>
from main import views
File "/home/runner/main/views.py", line 3, in <module>
from excelToDocx import transfer
File "/home/runner/excelToDocx.py", line 1, in <module>
import xlrd
ModuleNotFoundError: No module named 'xlrd'
exit status 1

You need to install the packages using the command pip install -r requirements.txt. If they aren't installed, it isn't going to find them.
The requirements.txt file is simply a way to tell users of your package what requirements there are for your program, and then let them install all of them easily through PyPi using the pip program. It doesn't actually tell python to install them for you.

Related

How to fix a bad upgrade of pip on python3

Before someone slaps my hand, I had installed venv/virtualenv to setup virtual environments but I had other issues so someone suggested I upgrade my pip3 version to the current version (22.0.4 right now)
I somehow messed up both pip and pip3 so hopefully someone can help me.
I tried to upgrade my version of pip on my Ubuntu 16.04 box by running
pip3 install --upgrade pip
I also tried running
pip3 install --upgrade pip3
After upgrading, I kept getting errors when trying to run pip3 so I removed it, planning to install with the get-pip.py script.
However, now when I try to install pip3 using
sudo python3 /tmp/get-pip.py
I keep getting an AttributeError:
$ sudo python3 /tmp/get-pip.py
Traceback (most recent call last):
File "/tmp/get-pip.py", line 27077, in <module>
main()
File "/tmp/get-pip.py", line 135, in main
bootstrap(tmpdir=tmpdir)
File "/tmp/get-pip.py", line 111, in bootstrap
monkeypatch_for_cert(tmpdir)
File "/tmp/get-pip.py", line 92, in monkeypatch_for_cert
from pip._internal.commands.install import InstallCommand
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_internal/commands/__init__.py", line 9, in <module>
...
...
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 5677, in <module>
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1488, in setResultsName
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1491, in _setResultsName
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1445, in copy
AttributeError: module 'copy' has no attribute 'copy'
Can someone point me to instructions on how to re-install pip/pip3 or how to get around this AttributeError? Hopefully the solution will also keep my old modules that I had downloaded from before too. Thanks
Check if there is a file /tmp/copy.py or directory+file /tmp/copy/__init__.py. Try these commands: sudo python3 -c "import copy; print(copy.copy)" and sudo python3 -c "import copy; print(copy.__file__)"
If any of those exists it will be imported instead of standard module copy because Python prepends the directory of a script (the script is /tmp/get-pip.py, the directory /tmp/) at the beginning of sys.path so it's where Python starts to find modules/packages.

NameError: name 'six' is not defined. six in not working with django and leaflet

i need some help with my leaflet config.
i'm getting this error when running the server since i added leaflet to my django-3.04 project.
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/marcusbey/.local/share/virtualenvs/rb-website-fKSjEdfu/src/django-leaflet/leaflet/__init__.py", line 80, in <module>
if DEFAULT_PRECISION is not None and not (isinstance(DEFAULT_PRECISION, six.integer_types) and (4 <= DEFAULT_PRECISION <= 12)):
NameError: name 'six' is not defined
I installed six and add it to the settings.py file but still getting that same error. i don't know where else to look and what to do.
:/
i followed this answer and it finally worked.
go to https://pypi.org/project/six/#files
download "six-1.14.0.tar.gz (33.9 kB)"
unzip it, copy and paste "six.py" into your source directory.
import "six" module into your source code (import six)
run source script.
https://stackoverflow.com/a/61327529/8571945
Thanks guys
Django used to come packaged with six, but version 3.0 has dropped it since Python 2 support has been completely removed: https://docs.djangoproject.com/en/3.0/releases/3.0/#removed-private-python-2-compatibility-apis
For now, if you need django-leaflet, you may have to stay on Django version 2.2. However, it looks like they're in the process of supporting Django 3. As of this writing, the last commit from 20 days ago is, Drop support for Python 2:
https://github.com/makinacorpus/django-leaflet/commit/844887affe607d3f115920c862f8ea2b45e19ed8
It may be worth trying an install from this commit into your virtual environment to see if it solves the issue:
pip install git+https://github.com/makinacorpus/django-leaflet.git#844887affe607d3f115920c862f8ea2b45e19ed8
I attempted it into a venv, and it didn't seem to require six any more.
Either way, it looks like they're working on a release for Django 3.0, it just isn't available from PyPI for pip install quite yet. Good luck!

Error while importing python cv2 in Azure function

We are deploying python based Azure Function using Azure CLI but getting this below error while importing cv2 library.
ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
We are not using any docker image. Would prefer a non-docker based solution.
Exception: ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
Stack: File "/usr/local/lib/python3.6/site-packages/azure_functions_worker/dispatcher.py", line 239, in _handle__function_load_request
func_request.metadata.entry_point)
File "/usr/local/lib/python3.6/site-packages/azure_functions_worker/loader.py", line 66, in load_function
mod = importlib.import_module(fullmodname)
File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/site/wwwroot/FaceFinderHttpTrigger/__init__.py", line 11, in <module>
import cv2
File "/home/site/wwwroot/.python_packages/lib/python3.6/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
There is a similar SO thread Azure Functions: In a Python function under Linux, how do I import a non-standard module such as opencv/imutils? with the same issue as yours.
Due to the libgthread shared library is a Linux base library, per my experience, it seems to be caused by Azure CLI could not package these Python packages with native dependencies as Azure Function. So the only solution I think is to build your Azure Function as a Docker container by yourself as the offical document Create a function on Linux using a custom image said, or try to use Azure CLI command func azure functionapp publish with --build-native-deps option to automatically build and configure the dependencies using a Docker container.
Also as I known, there is a blog OPENCV WITH AZURE FUNCTIONS which seems to work fine with opencv for Azure Functions, but it just run on local development environment, not on cloud, as my view. So I still recommend you to deploy your app as a Docker contaier for the app using native dependencies.

How to fix "No module named 'azure.storage' " error in cloud azure function?

Using a Python 3.6 enviroment in Anaconda, I'm able to run an azure function that accesses blob storage. Locally this function works fine, but when I post it on the cloud using VS Code and attempt a HTTP request, I get this error:
Result: Failure
Exception: ModuleNotFoundError: No module named 'azure.storage'
Stack: File "/usr/local/lib/python3.6/site-packages/azure/functions_worker/dispatcher.py", line 230, in _handle__function_load_request
func_request.metadata.entry_point)
File "/usr/local/lib/python3.6/site-packages/azure/functions_worker/loader.py", line 66, in load_function
mod = importlib.import_module(fullmodname)
File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/site/wwwroot/Convert/__init__.py", line 6, in <module>
from azure.storage.blob import BlockBlobService
Why do I only get this error in the cloud enviroment and how do I fix it.
Actually, I recommanded you to follow the offical Quick start tutorial Create an HTTP triggered function in Azure for Python to create a virtual environment for your function app and publish it within the virtual environment via command line, because all installed Python packages will be re-packaged and built to a publish file and deploy to your Azure Function App via func azure functionapp publish <APP_NAME> command. If not publish within the virtual environment, the deployment will be missing the required packages for your app.
And as #JohnGordon said in comment, please make sure your Azure Function App have been set up the correct environment for Python, not NodeJS or other language runtimes. If not, you can use the command func azure functionapp publish <APP_NAME> --force to force change its environment for runtime based on your virtual environment on local.
Also as #JohnGordon said in comment, if the two possible cases above are not real reason of your issue, you need to check your code files whether there is a file named azure.py, because the same file name azure will conflict with the package name of Azure SDK for Python.
Hope it helps.
Ok so the solution I found was using the virtual environment and command line stuff like #Peter Pan was suggesting, but added azure.storage to my requirements.txt file. Once I did that it uploaded and worked fine.
I fixed the azure deployment issue by using below pip command:
pip install --target="$(System.DefaultWorkingDirectory)/.python_packages/lib/site-packages" -r requirements.txt

Error in running exe file having xgboost package by using pyinstaller

I have a code for predicting some value that uses xgboost package in the code. When I run it in PyCharm, it runs as expected.
The problem is when I make an executable file using pyinstaller. It will make the exe without any error, but when I run it the following error is raised:
Traceback (most recent call last):
File "test_fraud.py", line 3, in <module>
import xgboost
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File
"C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\xgboost\__init__.py", line 11, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\xgboost\core.py", line 161, in <module>
File "lib\site-packages\xgboost\core.py", line 123, in _load_lib
File "lib\site-packages\xgboost\libpath.py", line 48, in find_lib_path
xgboost.libpath.XGBoostLibraryNotFound: Cannot find XGBoost Library in
the candidate path, did you install compilers and run build.sh in root
path?
List of candidates:
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost\xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost
\../../lib/xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost\./lib/xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost\xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost
\../../windows/x64/Release/xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\
xgboost\./windows/x64/Release/xgboost.dll
[6564] Failed to execute script test_fraud
What's wrong here?
It seems that Pyinstaller can't find the xgboost.dll, VERSION files. So you need to add them manually to your output package. I also suggest you use a try/except block to see what is going on.
Suppose this simple example:
import traceback
try:
import xgboost
input("xgboost imported successfully!")
except Exception:
traceback.print_exc()
input("Import Error!")
I suggest you use an env to build your script so, you need to add the xgboost directory located at <path_to_venv>/xgboost and VERSION file located at <path_to_venv>/Lib/site-packages/xboost. Next, add them as a data-file with pyinstaller. Launch your env and execute the below command (My virtualenv named as env):
├───myscript.py
├───env
Command:
(env) > pyinstaller myscript.py -F --add-data "./env/xgboost/*;xgboost/" --add-data "./env/Lib/site-packages/xgboost/VERSION;xgboost/"

Categories