Midoril
Midoril

Reputation: 11

Importing PyTorch in PyCharm using Anaconda

I just installed PyCharm and Anaconda. I installed PyTorch to Anaconda and i can even use "import torch" in Anaconda. I've created a new Project in PyCharm with the Anaconda Interpreter but i still can't use PyTorch in PyCharm.

Upvotes: 1

Views: 9128

Answers (5)

user3303504
user3303504

Reputation: 555

Finally solved this in Windows 10 after sinking an entire morning into it.

  1. Create a new Conda environment (via PyCharm or however you fancy)
  2. Navigate to C:\Your\Environment\Path\Scripts in a command terminal
    • Mine looks like this: C:\ProgramData\Anaconda3\envs\Snek_V2\Scripts
  3. Find what commands you would need for a pip install from the PyTorch website
    • They'll look something like this:
    • pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp37-cp37m-win_amd64.whl
    • pip3 install torchvision
  4. In the command terminal, you want to enter those commands with one difference; you'll need to replace pip3 with pip.exe. As you've navigated to the exact environment you plan to work in, this will be the pip relevant to you.
    • e.g. pip.exe install torchvision
  5. Open PyCharm and open the project that's working in the Conda environment we just set up.
  6. If you open up a Python Console, you should now be able to enter import torch and not have any errors pop up in response.

Upvotes: 0

WebBoy
WebBoy

Reputation: 61

Running pycharm from within anaconda env window

I had this problem also. Program that imported torch worked fine at anaconda prompt running in my pytorch env, but when i ran pycharm from the windows shortcut and EVEN set my environment to use pytorch env, it would complain torch could not be imported. When i ran pycharm from the prompt as shown, it worked.

Upvotes: 1

joglife tan
joglife tan

Reputation: 16

I got same problem and solved it:

  1. After you installed anaconda, source activate your own envrionment.
  2. Install pycharm-community and pytorch follow their official instructions.
  3. Start pycharm-community in the same anaconda environment.
  4. Loading your own project, followed instructions as Manuel Lagunas
  5. Finally, you have to install packages of torch in the environment of pycharm:
    pictures as:
    press '+' on the right side to install
    you can choose packages of torch by typing 'torch'

Upvotes: 0

k_rezk
k_rezk

Reputation: 1

I got the same problem and you that how i solved the problem: 1- when you open project choose conda as your project interpreter as shown here

enter image description here

2- in your project go to setting -> project: project name -> Project Interpreter check that you are using Conda Package Manager and your anaconda local settings

Upvotes: 0

saneryee
saneryee

Reputation: 3405

If you used a bash script and the conda environment to run python codes in your PyCharm IDE. You also need source activate myenv in your bash script.

Like this:

#!/usr/bin/env bash

source activate myenv

python test.py

source deactivate

Upvotes: 0

Related Questions