# Install custom python libraries in HuggingFace DLC

**URL:** <https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242>\
**Category:** Amazon SageMaker\
**Created:** [August 4, 2022, 12:12pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242 "2022-08-04T12:12:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluePenguin](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bluePenguin](https://discuss.huggingface.co/u/bluePenguin)\
**Post date:** [August 4, 2022, 12:12pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/1 "2022-08-04T12:12:09Z")

</div>

Hi,

I have the source code for a custom data cleaning library; the code is private to my company. I usually install it from local source using something ‘pip install ./cleanse’. I know the requirements.txt file can be used to specify package versions for public libraries like pandas and NumPy but how can we install custom packages in the HuggingFace DLC?

---

<div class="post-metadata">

**Author:** ![philschmid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/philschmid/32/38022_2.png) [@philschmid](https://discuss.huggingface.co/u/philschmid)\
**Post date:** [August 8, 2022, 6:21am UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/2 "2022-08-08T06:21:15Z")

</div>

You can put `./cleanse` into you `source_dir` and then either add `os.system("pip install ./cleanse")` at the top of your training script or add the local path `./cleanse` into a `requirements.txt` in the `source_dir`.

---

<div class="post-metadata">

**Author:** ![bluePenguin](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bluePenguin](https://discuss.huggingface.co/u/bluePenguin)\
**Post date:** [August 8, 2022, 1:40pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/3 "2022-08-08T13:40:29Z")

</div>

Hi, I’m actually using this library to clean in inference. Is there a place I could leverage the inference.py file inside the code/ directory in the model.tar.gz file? Or otherwise can I somehow integrate the into the preprocess script (using SKLearnProcessor)

---

<div class="post-metadata">

**Author:** ![philschmid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/philschmid/32/38022_2.png) [@philschmid](https://discuss.huggingface.co/u/philschmid)\
**Post date:** [August 8, 2022, 2:03pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/4 "2022-08-08T14:03:47Z")

</div>

Sorry, i am not sure what you mean with `Is there a place I could leverage the inference.py file inside the code/ directory in the model.tar.gz file? `

---

<div class="post-metadata">

**Author:** ![bluePenguin](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bluePenguin](https://discuss.huggingface.co/u/bluePenguin)\
**Post date:** [August 8, 2022, 2:10pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/5 "2022-08-08T14:10:05Z")

</div>

I’m using an inference.py file right now as per instructions [at the bottom of this page](https://github.com/aws/sagemaker-huggingface-inference-toolkit#-user-defined-codemodules) to override defaults during model inference. I’m wondering how/if I can use the custom library within this file

---

<div class="post-metadata">

**Author:** ![philschmid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/philschmid/32/38022_2.png) [@philschmid](https://discuss.huggingface.co/u/philschmid)\
**Post date:** [August 8, 2022, 2:17pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/6 "2022-08-08T14:17:46Z")

</div>

> [@philschmid](#):
>
> You can put `./cleanse` into you `source_dir` and then either add `os.system("pip install ./cleanse")` at the top of your training script or add the local path `./cleanse` into a `requirements.txt` in the `source_dir`.

^like that

---

<div class="post-metadata">

**Author:** ![bluePenguin](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bluePenguin](https://discuss.huggingface.co/u/bluePenguin)\
**Post date:** [August 8, 2022, 2:24pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/7 "2022-08-08T14:24:09Z")

</div>

What is source\_dir though? Like where is that argument specified

---

<div class="post-metadata">

**Author:** ![philschmid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/philschmid/32/38022_2.png) [@philschmid](https://discuss.huggingface.co/u/philschmid)\
**Post date:** [August 8, 2022, 2:35pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/8 "2022-08-08T14:35:52Z")

</div>

How are you deploying your model currently? could you share the code snippet? `source_dir` refers to the `HuggingFaceModel` of the `sagemaker-sdk`.

---

<div class="post-metadata">

**Author:** ![bluePenguin](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bluePenguin](https://discuss.huggingface.co/u/bluePenguin)\
**Post date:** [August 8, 2022, 2:43pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/9 "2022-08-08T14:43:13Z")

</div>

```auto
# create Hugging Face Model Class
huggingface_model = HuggingFaceModel(
   model_data= model_uri, # path to your trained sagemaker model
   role=role, # iam role with permissions to create an Endpoint
   transformers_version="4.17", # transformers version used
   pytorch_version="1.10", # pytorch version used
   py_version="py38", 
   env={ 'HF_TASK':'text-classification' }# python version of the DLC
)

```

---

<div class="post-metadata">

**Author:** ![philschmid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/philschmid/32/38022_2.png) [@philschmid](https://discuss.huggingface.co/u/philschmid)\
**Post date:** [August 8, 2022, 2:55pm UTC](https://discuss.huggingface.co/t/install-custom-python-libraries-in-huggingface-dlc/21242/10 "2022-08-08T14:55:27Z")

</div>

does the `model_uri` contain a `code/inference.py` ? If so you need to put the requirements.txt / `./cleanse` into the `code/` directory.
