Runtime error saying 'pip install python-multipart' despite installing several times

This error is being thrown in the GET and POST functions that are defined. I tried installing it several times using conda, cmd as well as in the jupyter notebook itself using ‘!pip install python-multipart’ but the same error is being shown again and again. If possible, please help.

This is the function I’m talking about:

# Assign an instance of the FastAPI class to the variable "app".
   # You will interact with your api using this instance.
   app = FastAPI(title='Deploying a ML Model with FastAPI')
   # List available models using Enum for convenience. This is useful when the options are pre-defined.
class Model(str, Enum):
yolov3tiny = "yolov3-tiny"
yolov3 = "yolov3"


# By using @app.get("/") you are allowing the GET method to work for the / endpoint.
@app.get("/")
def home():
return "Congratulations! Your API is working as expected. Now head over to http://localhost:8000/docs."


# This endpoint handles all the logic necessary for the object detection to work.
# It requires the desired model and the image in which to perform object detection.
@app.post("/predict") 
def prediction(model: Model, file: UploadFile = File(...)):

# 1. VALIDATE INPUT FILE
filename = file.filename
fileExtension = filename.split(".")[-1] in ("jpg", "jpeg", "png")
if not fileExtension:
    raise HTTPException(status_code=415, detail="Unsupported file provided.")

# 2. TRANSFORM RAW IMAGE INTO CV2 image

# Read image as a stream of bytes
image_stream = io.BytesIO(file.file.read())

# Start the stream from the beginning (position zero)
image_stream.seek(0)

# Write the stream of bytes into a numpy array
file_bytes = np.asarray(bytearray(image_stream.read()), dtype=np.uint8)

# Decode the numpy array as an image
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)


# 3. RUN OBJECT DETECTION MODEL

# Run object detection
bbox, label, conf = cv.detect_common_objects(image, model=model)

# Create image that includes bounding boxes and labels
output_image = draw_bbox(image, bbox, label, conf)

# Save it in a folder within the server
cv2.imwrite(f'images_uploaded/{filename}', output_image)


# 4. STREAM THE RESPONSE BACK TO THE CLIENT

# Open the saved image for reading in binary mode
file_image = open(f'images_uploaded/{filename}', mode="rb")

# Return the image as a stream specifying media type
return StreamingResponse(file_image, media_type="image/jpeg")

And this is the error:

Form data requires "python-multipart" to be installed. 
You can install "python-multipart" with: 

pip install python-multipart

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\dependencies\utils.py in check_file_field(field)
     92             # __version__ is available in both multiparts, and can be mocked
---> 93             from multipart import __version__  # type: ignore
     94 

ImportError: cannot import name '__version__' from 'multipart' (unknown location)

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
<ipython-input-10-dab682a97309> in <module>
     18 # It requires the desired model and the image in which to perform object detection.
     19 @app.post("/predict")
---> 20 def prediction(model: Model, file: UploadFile = File(...)):
     21 
     22     # 1. VALIDATE INPUT FILE

c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\routing.py in decorator(func)
    572                 response_class=response_class,
    573                 name=name,
--> 574                 callbacks=callbacks,
    575             )
    576             return func

c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\routing.py in add_api_route(self, path, endpoint, response_model, status_code, tags, dependencies, summary, description, response_description, responses, deprecated, methods, operation_id, response_model_include, response_model_exclude, response_model_by_alias, response_model_exclude_unset, response_model_exclude_defaults, response_model_exclude_none, include_in_schema, response_class, name, route_class_override, callbacks)
    518             name=name,
    519             dependency_overrides_provider=self.dependency_overrides_provider,
--> 520             callbacks=current_callbacks,
    521         )
    522         self.routes.append(route)

c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\routing.py in __init__(self, path, endpoint, response_model, status_code, tags, dependencies, summary, description, response_description, responses, deprecated, name, methods, operation_id, response_model_include, response_model_exclude, response_model_by_alias, response_model_exclude_unset, response_model_exclude_defaults, response_model_exclude_none, include_in_schema, response_class, dependency_overrides_provider, callbacks)
    384                 get_parameterless_sub_dependant(depends=depends, path=self.path_format),
    385             )
--> 386         self.body_field = get_body_field(dependant=self.dependant, name=self.unique_id)
    387         self.dependency_overrides_provider = dependency_overrides_provider
    388         self.callbacks = callbacks

c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\dependencies\utils.py in get_body_field(dependant, name)
    780         field_info=BodyFieldInfo(**BodyFieldInfo_kwargs),
    781     )
--> 782     check_file_field(final_field)
    783     return final_field

c:\users\sh1va\anaconda3\envs\mlep-w1-lab\lib\site-packages\fastapi\dependencies\utils.py in check_file_field(field)
    104         except ImportError:
    105             logger.error(multipart_not_installed_error)
--> 106             raise RuntimeError(multipart_not_installed_error)
    107 
    108 

RuntimeError: Form data requires "python-multipart" to be installed. 
You can install "python-multipart" with: 

pip install python-multipart

Hi @shivam

welcome to the community!

just for my understanding. Are you using the Method1, right?
Did you get any error running

pip install -r requirements.txt

All the required packages are included in that file. So you should not install anything by hand.

Which Anaconda/python/tensorflow version are you using?
Regards

Hello Sir,
I am doing this with Method 1 i.e. conda virtual env.
I did not get any error on running pip install -r requirements.txt
I’m using python 3.8.5, conda 4.9.2.
I hadn’t installed Tensorflow locally before but it got installed when i executed pip install -r requirements.txt i.e. version 2.3.2.
I know the lab was designed keeping in mind that all system requirements would be met before running actual code but this happened and it explicitly showed to install that particular module, so I tried doing that too.
Please help as I’m stuck and want to move forward with deploying an actual model.

Hi @shivam

I’m trying to figure out your problem.
According to the tutorial the virtualenv should use python 3.7

> conda create --name mlep-w1-lab python=3.7

Are you familiar with running a linux box under windows 10?
https://docs.microsoft.com/en-us/windows/wsl/install-win10
I mention this third approach because you could install an Ubuntu Server 20.04 under windows 10 and then run the Docker.

I am using python 3.7 in the virtual env for which python v3.7 was downloaded.
I am neither familiar with ubuntu nor docker, so I guess I’ll have to look into that. But I’ll surely try to do so. If there’s a solution using conda, please let me know as I’m bit comfortable with that.

Thank You

Ok no problem
I will run all the steps for the Method1 based on conda an I will let you know.
FYI I have Anaconda Navigator 2.0.3
regards

Hi @shivam

I have re-installed all the stuff and followed the Method1.
In my case it worked.
I faced just one problem regarding the version of tensorflow.
So I upgraded the tensorflow version

pip3 install --upgrade tensorflow

Then it worked smoothly.
In your case the problem seems to be related to python.
Please can you try to execute

pip3 install --upgrade python

Regards