basti394
basti394

Reputation: 405

Pytorch doesn't work with CUDA in PyCharm/IntelliJ

I have just downloaded PyTorch with CUDA via Anaconda and when I type into the Anaconda terminal:

import torch
if torch.cuda.is_available():
    print('it works')

then he outputs that; that means that it worked and it works with PyTorch.

But when I go to my IDE (PyCharm and IntelliJ) and write the same code, it doesn't output anything.

Could someone please explain to me how I can somehow get this to work in the IDE?

Upvotes: 4

Views: 22131

Answers (7)

Erfan Khalaji
Erfan Khalaji

Reputation: 63

The problem is neither PyCharm nor PyTorch. The problem is that, somehow, Pycharm is sensing conflicts in which version of a PyTorch or some other libraries to use. Therefore, it is warning you to be careful since multiple packages attempting to access your GPU might interrupt the process or result in obtaining poor outcome. The solution is:

  • Whether you are using conda or pip, use the following command to see a list of your installed packages:

pip/conda list

  • Make sure you remove the one you think you recently added and might have multiple versions installed.
  • Check if you have any specific requirement for the Python interpreter you are using.
  • reinstall the packes you need and make sure you know what version you need.

My problem was that I had multiple versions of PyTorch installed.

Upvotes: 0

Karthik N G
Karthik N G

Reputation: 2321

This is how I made it work on my Windows Machine with CUDA using PyCharm.

I uninstalled the existing torch package by selecting torch and clicking the - sign. Please see screenshot below

Uninstall PyTorch with CPU capability only

After it has successfully uninstalled. I clicked on the plus sign again and searched for torch in the search bar. Then in the bottom right corner, I checked the options checkbox and entered the following command

pip install -f https://download.pytorch.org/whl/torch_stable.html

Click on the Install Package button to install PyTorch with CUDA capability. Please see the screenshot below. I hope this helps.

Pip Install for PyTorch with CUDA

Upvotes: 2

Peyman
Peyman

Reputation: 4189

In my case, it was the specific build of PyTorch I had installed. To check the PyTorch build:

import torch
print(torch.__version__)

For me, it showed 2.0.1+cpu, indicating a CPU-only version. I reinstalled PyTorch using the official website's recommended method. Now it shows 2.0.1+cu118, indicating a CUDA-compatible build.


Upvotes: 0

the-lay
the-lay

Reputation: 1481

Another possible root of the problem is running a Flatpak version of Pycharm. Took me some time to figure out, but Flatpak does not load user specific environment variables files (i.e. ~/.profile, ~/.bash_profile and conda environment). I have deleted Flatpak version and installed a snap version (sudo snap install [pycharm-professional|pycharm-community] --classic) and it loads the proper PATH which allows loading CUDA correctly.

Upvotes: 2

Yuval Atzmon
Yuval Atzmon

Reputation: 5945

I had a similar issue.

Inspired by @the-lay answer, calling import os; os.system('') resolved my issue.

Upvotes: 1

Drogheda
Drogheda

Reputation: 209

I also spent over 10 hours trying to figure out what was wrong. I ended up checking if there is something wrong with interpreter. What was the problem is version of python (I had v. 3.8). After upgrading to newest version, problem was solved.

Check your version using command:

python --version

If your version is old (current is 3.9.2), install newest one from official python site: https://www.python.org/downloads/release/python-392/

After installation make sure you using newer version (use command above)

Upvotes: 2

Entony
Entony

Reputation: 106

It was driving me mad as well... What finally helped me was the first link that says to use PyCharm "Terminal" to run the pip install command (from the PyTorch website). That fixed all my problems. (I had installed pytorch 3 times by that time and tried different interpreters...)

https://www.datasciencelearner.com/how-to-install-pytorch-in-pycharm/

pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio===0.8.0 -f https://download.pytorch.org/whl/torch_stable.html

I hope this helps save someone hours of headache. :)

Upvotes: 9

Related Questions