Intel® Distribution for Python*
Engage in discussions with community peers related to Python* applications and core computational packages.
446 Discussions

Can torch-directml be used with Intel Distribution for Python?

codefarmer
Novice
8,155 Views

System: Windows 11, Intel Distribution for Python 2025.1 (python 3.12.8)

 

Can Microsoft's PyTorch with DirectML on Windows be used with Intel Distribution for Python 2025.1? I'm unable to successfully run in such an environment because Intel python cannot find torch or torch_directml, even though they were successfully installed.  Here's what I've tried:

 

  1. Activate IDP base environment.
  2. Create a virtual environment to test pytorch-directml using: 

conda create --name pytdml -y intelpython3_full

3. Install Microsoft's pytorch directml:

pip install torch-directml

All successful so far. However, when I attempt to use it, python isn't able to import torch or torch_directml:

(pytdml) C:\Users\username\intelpython3>python
Python 3.12.8 | Intel Corporation | (main, Feb 13 2025, 16:49:50) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'

 pip freeze confirms that modules are installed:

torch==2.4.1
torch-directml==0.2.5.dev240914
torchvision==0.19.1

attempts to reinstall also confirm that packages are installed:

(pytdml) C:\Users\username\intelpython3>pip install torch-directml
Requirement already satisfied: torch-directml in c:\users\username\intelpython3\envs\pytdml\lib\site-packages (0.2.5.dev240914)
Requirement already satisfied: torch==2.4.1 in c:\users\username\intelpython3\envs\pytdml\lib\site-packages (from torch-directml) (2.4.1)
Requirement already satisfied: torchvision==0.19.1 in c:\users\username\intelpython3\envs\pytdml\lib\site-packages (from torch-directml) (0.19.1)

What am I missing? Thanks for your help!

0 Kudos
1 Solution
codefarmer
Novice
7,865 Views

@StefR_Intel Issue resolved! My PATH contained several python executables, and even though I was in the (pytdml) environment, that version of python was not the first in the path! The workaround is to adjust the path so that the one corresponding to the desired environment is used.  Now I'm able to import torch and torch_directml.

Interestingly, the first path in the list is not explicitly in my PATH, and with some experimentation discovered that somehow the current working directory is being added to the front of of the PATH! So when I changed directory to C:\ for example, then the correct python.exe is found. It's not clear to me where/how the cwd is being added to the front of the path, but I simply have to remember to start in a directory which doesn't contain a python executable, and I hope this helps someone else.

Thank you for your time.

(pytdml) C:\Users\username\intelpython3>where python
C:\Users\username\intelpython3\python.exe
C:\Users\username\intelpython3\envs\pytdml\python.exe
C:\Users\username\AppData\Local\Programs\Python\Python313\python.exe
C:\Users\username\AppData\Local\Microsoft\WindowsApps\python.exe

 

View solution in original post

6 Replies
StefR_Intel
Moderator
8,059 Views

Hi,

 

This doesn't look like an issue with the Intel® Distribution for Python*. I could not reproduce it, but it points to Python looking in the wrong place for packages.

Please consider using the installation guide available here to make sure you are installing the Intel® Distribution for Python* properly: https://www.intel.com/content/www/us/en/developer/tools/oneapi/distribution-python-download.html

 

Regards,

Stef

codefarmer
Novice
8,021 Views

@StefR_Intel wrote:

Please consider using the installation guide available here to make sure you are installing the Intel® Distribution for Python* properly: https://www.intel.com/content/www/us/en/developer/tools/oneapi/distribution-python-download.html

 


FYI, the link above goes to a page where the Intel progress logo never finishes spinning, so the page content cannot be accessed (tried in Firefox and MS Edge). I've submitted feedback, and I thought I would let you know as well.

In any case, my installation of IDP is from the offline installer, intelpython3-2025.1.0_209-Windows-x86_64.exe

C:\Users\username\intelpython3>.\python.exe -V
Python 3.12.8

 

0 Kudos
codefarmer
Novice
8,051 Views

@StefR_Intel Thank your for you taking the time to look at this issue for me.

The IDP installation has been working well for me, but I will review the documentation again. I agree that the issue appears to be that python isn't looking in the right place for the packages, even though output shows that they are installed in the site-packages folder for the venv under IDP. My knowledge gap lies in conda and that's where I may be overlooking something, so I was hoping someone could spot which commands I am missing or not using properly.

In the meanwhile, could I clarify your response about not being able to reproduce the issue to mean that you were able to install pytorch_directml, and successfully import torch and torch_directml from that virtual environment, and not from a prior installation of torch?

Thanks again.

0 Kudos
StefR_Intel
Moderator
7,950 Views

Thanks a lot for the feedback. I'm now having trouble accessing it as well.

 

Let me share the steps I took to reproduce the issue:
1. Create a conda environment:

conda create -n pytdml intelpython3_full python=3.12 -c https://software.repos.intel.com/python/conda -c conda-forge --override-channel

2. Activate the environment:

conda activate pytdml

3. Install torch-directml:

pip install torch-directml

4. Open the Python interactive shell:

python

5. Import torch

>> import torch

You mentioned venv so make sure you do not mix conda environments with Python virtual environments. You can check the list of installed packages in your conda environment (after activating it) with

conda list

Let me know if this works for you.

0 Kudos
codefarmer
Novice
7,925 Views

@StefR_Intel Thanks for the continued support. After deleting the existing pytdml, I created a new environment by following the exact commands you posted, which are very similar to what I used previously with the exception that the conda create command you provided had more arguments. Unfortunately, this did not change the fact that after installing torch-directml, python still cannot find the package. Here's a partial output, followed by a question:

(pytdml) C:\Users\username\intelpython3>conda list
# packages in environment at C:\Users\username\intelpython3\envs\pytdml:
#
# Name Version Build Channel
...
torch 2.4.1 pypi_0 pypi
torch-directml 0.2.5.dev240914 pypi_0 pypi
torchvision 0.19.1 pypi_0 pypi
...

Successful pip install, and conda recognizes it, but package still not found by Intel python:

(pytdml) C:\Users\username\intelpython3>python
Python 3.12.8 | Intel Corporation | (main, Feb 13 2025, 16:49:50) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
>>>

 One doubt I have is that when I run the conda env creation command, I am in the IDP base conda environment already because it's needed to be able to use conda. Here are the commands I ran in a Windows command shell:

C:\Users\username\intelpython3\Scripts>activate.bat

(base) C:\Users\username\intelpython3>conda create -n pytdml intelpython3_full python=3.12 -c https://software.repos.intel.com/python/conda -c conda-forge --override-channel

Could being in the (base) environment while creating a new one be a problem? 

0 Kudos
codefarmer
Novice
7,866 Views

@StefR_Intel Issue resolved! My PATH contained several python executables, and even though I was in the (pytdml) environment, that version of python was not the first in the path! The workaround is to adjust the path so that the one corresponding to the desired environment is used.  Now I'm able to import torch and torch_directml.

Interestingly, the first path in the list is not explicitly in my PATH, and with some experimentation discovered that somehow the current working directory is being added to the front of of the PATH! So when I changed directory to C:\ for example, then the correct python.exe is found. It's not clear to me where/how the cwd is being added to the front of the path, but I simply have to remember to start in a directory which doesn't contain a python executable, and I hope this helps someone else.

Thank you for your time.

(pytdml) C:\Users\username\intelpython3>where python
C:\Users\username\intelpython3\python.exe
C:\Users\username\intelpython3\envs\pytdml\python.exe
C:\Users\username\AppData\Local\Programs\Python\Python313\python.exe
C:\Users\username\AppData\Local\Microsoft\WindowsApps\python.exe

 

Reply