Python 3: No module named zlib? - python-3.x

I am trying to run my Flask application with an Apache server using mod_wsgi, and it has been a bumpy road, to say the least.
It has been suggested that I should try to run my app's .wsgi file using Python to make sure it is working.
This is the contents of the file:
#!/usr/bin/python
activate_this = '/var/www/Giveaway/Giveaway/venv/bin/activate_this.py'
with open(activate_this) as f:
code = compile(f.read(), "somefile.py", 'exec')
exec(code)
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/Giveaways/")
from Giveaways import application
application.secret_key = 'Add your secret key'
However, when I run it, I get this error:
ImportError: No module named 'zlib'
And no, I am not using some homebrewed version of Python - I installed Python 3 via apt-get.
Thanks for any help.

does the contents of somefile.py include the gzip package? in which case you may have to install gzip package via pip or similar

Related

AWS Lambda python: Unable to import module 'lambda_function': No module named 'regex._regex'

I am currently working with AWS Lambda. Here is an excerpt of the code:
import pandas as pd
import re
import nltk
from stop_words import get_stop_words
stopwords = get_stop_words('en')
nltk.download('punkt')
nltk.download('wordnet')
wn = nltk.WordNetLemmatizer()
def lemmatization(txt):
text = ([wn.lemmatize(word) for word in txt])
return text
def lambda_handler(event,context):
bucket = "aaabbb"
key = "cccddd"
s3_client = boto3.client('s3')
s3_file = s3_client.get_object(Bucket=bucket, Key=key)
s3_file_data = s3_file['Body'].read()
s3_file_data = io.BytesIO(s3_file_data)
df = pd.read_csv(s3_file_data)
df['ABC'] = df['ABC'].apply(lambda x: lemmatization(x))
print(df)
However, I am always getting the error:
Unable to import module 'lambda_function': No module named 'regex._regex'
I have already imported nltk and regex packages. Could you please help me with it?
A possible solution could be that your OS uses a different version of Python (i.e. 3.6) when downloading your dependencies than your Lambda Function (i.e. 3.7). I would suggest attempting to download whatever version of Python you are using for your lambda script, and then for example if I wanted the version of Python to be 3.8 I would run the code:
pip3.8 install -r requirements.txt -t aws-lib .
I faced this problem just like you. The problem causing this error is the difference in the OS that you use and lambda function uses. When python installs a package it creates the installed files with respect to the OS you use. So when you use the deployment bundle that was created using a linux OS then it would work with lambda function.
There are many ways for a windows user to do this, but I would recommend using docker containers to install your packages.
Steps to do it:
pull python:3.8 docker image (this is the highest version supported by lambda at the time of writing this answer)
run your container with the directory with the code mounted to the container as volume.
now inside the container navigate to the mounted folder and install your required packages using pip.
come out of your container and now use these installed packages to build your bundle and deploy it on AWS lambda
ps: now when you execute your code on windows it will give an error because the packages installed are built for linux OS

Python tabula-py cannot import name wrapper

Here is my code:
from tabula import wrapper
df = wrapper.read_pdf('singapore.pdf')
But it gives following error:
ImportError: cannot import name 'wrapper'
I tried it on ubuntu and it works fine there but on Windows I am unable to use this code, as it always gives the above error. I installed tabula by using this command:
pip3 install tabula-py
Here is how I resolved it, you need to add java path to environment variables. More details can be found here:
https://tabula-py.readthedocs.io/en/latest/getting_started.html#get-tabula-py-working-windows-10

Is it possible to install python libraries with pip programmatically?

Let me explain what I want to do.
The list of libraries I want installed is listed in a .txt file.
My script reads the list from the file sequentially, and if the script isn't installed, it installs it via pip, or if it is already installed, checks the version and updates it if necessary.
I googled it up but didn't find how to do that. Can you offer any help or guidance?
Yes you can. Try this, here is an example of one module which is hard coded
import os
import subprocess
import sys
get_pckg = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
installed_packages = [r.decode().split('==')[0] for r in get_pckg.split()]
required_packeges = ['shopifyAPI'] // Make a change here to fetch from file
for packg in required_packeges:
if packg in installed_packages:
pass
else:
print('installing package')
os.system('pip install ' + packg)
First i will fetch all installed modules and then i will check my required module is installed or not if not then it will install it.
Yes, you can. Python module os does support running script programmatically. Since I don't know how your file structure looks like, I guess you can read the file and run the script sequentially.
import os
os.system("pip install <module>")
Use Following to install lib. programmatically.
import pip
try:
pip.main(["install", "pandas"])
except SystemExit as e:
pass

ImportError: No module named flask using MAC VSCODE

ScreenGrab of error when trying to import db I created
You either need to install Flask from pip, or something similar, or Python is not able to find the location of Flask if it is, in fact, installed.
At your Python prompt, try this to see where it is looking for libraries:
>> import sys
>> print (sys.path)

"How to fix' ImportError: cannot import name 'SQLAlchemy'

import os
from flask import Flask
from flask_wtf import FlaskForm
import sqlalchemy
from flask_sqlalchemy import SQLAlchemy
bsdir = os.path.abspath(os.path.dirname(__file__))
# print(bsdir)
app = Flask(__name__, template_folder = 'template')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(bsdir,'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATION'] = False
db = SQLAlchemy(app)
class Puppy(db.Model):
pass
Traceback (most recent call last):
File "D:\PYTHON\GIT_EXC\FLASK_\flask_sqlalchemyex.py", line 5, in
from flask_sqlalchemy import SQLAlchemy File "D:\PYTHON\GIT_EXC\FLASK_\flask_sqlalchemy.py", line 4, in
from flask_sqlalchemy import SQLAlchemy ImportError: cannot import name 'SQLAlch
Try using this command
pip install flask-sqlalchemy --user
It worked for me
And If you are using PyCharm go to file
Select Invalidate caches and restart
Try to install it with these commands , (it worked for me):
pip install flask-sqlalchemy
pip3 install flask-sqlalchemy
Refer this site for Example
or
Refer the official guide site for installation
if it doesnt worked then try above commands with --user at the end of both commands
another solution maybe to install an IDE (if you are not using one) like PyCharm ; rather than a some simple text editors
First command installs package to python v2.x
Second one installs package to python 3.x
If you want to use 3.x to run your app ;then go to configuration and change it to python 3.x
Refer this for Getting Help / Development / Bug reporting
Try changing the name of your .py file. It may be causing a conflict with the flask-sqlalchemy package.
"D:\PYTHON\GIT_EXC\FLASK_\flask_sqlalchemy.py"
I faced same issue while instantiating airflow db by command
$airflow db init
error: ImportError: cannot import name 'SQLAlchemyAutoSchema'
Fixes:
Uninstall:
$pip uninstall marshmallow-sqlalchemy
Then upgrade it to version 0.24.0
$pip3 install marshmallow-sqlalchemy==0.24.0
My problem resolved and able to initialize airflow db.

Resources