Home > other >  Requests fail when extra packages are installed
Requests fail when extra packages are installed

Time:02-19

I had these packages in my server:

from flask import (
    Flask,
    request,
    make_response,
    send_from_directory,
    send_file,
    render_template,
    redirect,
    url_for,
    current_app,
)
import json
import os
import subprocess
import sys

And this line worked fine: data = request.get_json()

Then i had to add some more packages:

import requests
from urllib import request
from urllib.parse import urlencode

and now i get this error in that line:

AttributeError: module 'urllib.request' has no attribute 'get_json'

Something is broken and i don't know how to find it.

CodePudding user response:

You can't have two definitions for the same name. If you do

import request
from urllib import request

then request now refers to urllib.request, not the request module, since the second import redefines the name.

Change the second one to

import urllib

and then use urllib.request to refer to that module. request by itself will still refer to the request module, so request.get_json() will continue to work.

  • Related