# Setting up environment in Sagemaker Studio

**URL:** <https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998>\
**Category:** Amazon SageMaker\
**Created:** [March 4, 2023, 3:40am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998 "2023-03-04T03:40:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![oschan77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/oschan77/32/41493_2.png) [@oschan77](https://discuss.huggingface.co/u/oschan77)\
**Post date:** [March 4, 2023, 3:40am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/1 "2023-03-04T03:40:25Z")

</div>

Hello everyone,

I am currently working on finetuning a DETR object detection model in Sagemaker Studio using a Hugging Face Estimator. I have installed the libraries with pip, as shown below:

```auto
!pip install transformers datasets huggingface_hub evaluate timm albumentations wandb sagemaker

```

However, when I tried to check the versions of Pytorch and transformers inside the SageMaker Studio notebook, I got the following error:

```auto
import torch
print(torch. __version__ )
>>>ModuleNotFoundError: No module named 'torch'

```

```auto
!transformers-cli
>>>None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.

```

I am new to Sagemaker (I usually use Colab) and I am wondering what could be going wrong in the settings of the environment. Do I need to install Pytorch myself or should I change the settings inside Sagemaker? Or should I look into Deep Learning Containers? I would greatly appreciate any guidance or advice on how to resolve this issue. Thank you very much in advance for your help!

---

<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:** [March 6, 2023, 8:30am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/2 "2023-03-06T08:30:24Z")

</div>

It seems that you don’t have pytorch installed. You can either use a pytorch kernel or `pip install torch`.

---

<div class="post-metadata">

**Author:** ![oschan77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/oschan77/32/41493_2.png) [@oschan77](https://discuss.huggingface.co/u/oschan77)\
**Post date:** [March 7, 2023, 6:15am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/3 "2023-03-07T06:15:11Z")

</div>

Thank you so much for your help! Just a follow-up question, do I need to explicitly install packages for train.py? For instance, I installed albumentations in the Sagemaker notebook. However, when I import albumentations in train.py. There is a ModuleNotFoundError.

requirements.txt

```auto
transformers==4.17.0
datasets[s3]==1.18.4
huggingface_hub
timm
evaluate
albumentations
wandb
sagemaker
ipywidgets==7.0

```

train.py

```auto
import albumentations

>>>ModuleNotFoundError: No module named 'albumentations'

```

Do you have any idea about this issue? Now I am using subprocess.check\_call to run pip install inside train.py as a workaround. Thanks!

```auto
import subprocess
subprocess.check_call(["pip", "install", "albumentations"])

```

---

<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:** [March 7, 2023, 7:52am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/4 "2023-03-07T07:52:07Z")

</div>

by `train.py` you when using `HuggingFace` estimator? Y

---

<div class="post-metadata">

**Author:** ![oschan77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/oschan77/32/41493_2.png) [@oschan77](https://discuss.huggingface.co/u/oschan77)\
**Post date:** [March 7, 2023, 7:56am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/5 "2023-03-07T07:56:03Z")

</div>

Yes, this is my estimator.

```auto
huggingface_estimator = HuggingFace(
  entry_point='train.py',
  source_dir='./scripts',
  instance_type='ml.g4dn.xlarge',
  instance_count=2,
  role=role,
  transformers_version='4.17',
  pytorch_version='1.10',
  py_version='py38',
  hyperparameters=hyperparameters,
)

```

---

<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:** [March 7, 2023, 8:14am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/6 "2023-03-07T08:14:23Z")

</div>

The container comes with the Hugging Face Libraries installed, e.g., `transformers` and `datasets` and `pytorch` ´, here 4.17 and 1.10 if you need additional libraries or versions, you can define them in the `requirements.txt`. SageMaker will then install them on job creation.

---

<div class="post-metadata">

**Author:** ![oschan77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/oschan77/32/41493_2.png) [@oschan77](https://discuss.huggingface.co/u/oschan77)\
**Post date:** [March 7, 2023, 8:27am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/7 "2023-03-07T08:27:00Z")

</div>

I have installed albumentations in the Sagemaker notebook in my requirements.txt, as follows:

```auto
!pip install -r requirements.txt

```

requirements.txt

```auto
transformers==4.17.0
datasets[s3]==1.18.4
huggingface_hub
timm
evaluate
albumentations
wandb
sagemaker
ipywidgets==7.0

```

If I run !pip list in the notebook, I can see that albumentations has been installed.

```auto
albumentations 1.3.0

```

However, train.py still cannot use albumentations. Or do you mean I can pass the requirements.txt file in HuggingFace estimator directly? Thanks!

---

<div class="post-metadata">

**Author:** ![oschan77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/oschan77/32/41493_2.png) [@oschan77](https://discuss.huggingface.co/u/oschan77)\
**Post date:** [March 9, 2023, 8:06am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/8 "2023-03-09T08:06:56Z")

</div>

@philschmid Any ideas? I wonder why others do not have the same issue. I have set up the whole environment with a new domain and it still does not work. Thanks!

---

<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:** [March 9, 2023, 8:56am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/9 "2023-03-09T08:56:50Z")

</div>

Is the `train.py` executed in the notebook or through `HuggingFace` estimator `.fit` if so you need to provide the `requirements.txt` in the same directory as the `train.py` that way sagemaker will install those packages when running the managed job.

---

<div class="post-metadata">

**Author:** ![oschan77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/oschan77/32/41493_2.png) [@oschan77](https://discuss.huggingface.co/u/oschan77)\
**Post date:** [March 9, 2023, 9:22am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/10 "2023-03-09T09:22:01Z")

</div>

train.py is executed by Huggingface Estimator instead of directly executed in the notebook. Do you mean my requirements.txt should be the ‘scripts’ folder?

My current file structure:

```auto
./notebook.ipynb
./requirements.txt
./scripts/train.py
./scripts/utils.py

```

---

<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:** [March 10, 2023, 8:21am UTC](https://discuss.huggingface.co/t/setting-up-environment-in-sagemaker-studio/32998/11 "2023-03-10T08:21:35Z")

</div>

copy your `requirements.txt` into you `scripts` folder then it should install the dependencies.
