Quick Fix: Python raises the
ImportError: No module named 'torch' when it cannot find the library
torch. The most frequent source of this error is that you haven’t installed
torch explicitly with
pip install torch. Alternatively, you may have different Python versions on your computer, and
torch is not installed for the particular version you’re using.
You’ve just learned about the awesome machine learning capabilities of the
torch library and you want to try it out, so you start your code with the following statement:
This is supposed to import the torch library into your (virtual) environment. However, it only throws the following
ImportError: No module named torch:
>>> import torch Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> import torch ModuleNotFoundError: No module named 'torch'
👉 Recommended Tutorial: How to Install PyTorch in Python?
Note that if you want to install PyTorch on Conda, jump or scroll down to 👉 “Solution Idea 3” right away.
Solution Idea 1: Install Library torch
The most likely reason is that Python doesn’t provide
torch in its standard library. You need to install it first!
Before being able to import the
torch module, you need to install it using Python’s package manager
pip. Make sure pip is installed on your machine.
To fix this error, you can run the following command in your Windows shell:
$ pip install torch
This simple command installs
torch in your virtual environment on Windows, Linux, and MacOS. It assumes that your
pip version is updated. If it isn’t, use the following two commands in your terminal, command line, or shell (there’s no harm in doing it anyways):
$ python -m pip install --upgrade pip $ pip install torch
💡 Note: Don’t copy and paste the
$ symbol. This is just to illustrate that you run it in your shell/terminal/command line.
You should also install the NumPy library which is a prerequisite for PyTorch. More here:
👉 Recommended Tutorial: How to Install NumPy in Python?
Solution Idea 2: Fix the Path
The error might persist even after you have installed the
torch library. This likely happens because
pip is installed but doesn’t reside in the path you can use. Although
pip may be installed on your system the script is unable to locate it. Therefore, it is unable to install the library using
pip in the correct path.
To fix the problem with the path in Windows follow the steps given next.
Step 1: Open the folder where you installed Python by opening the command prompt and typing
Step 2: Once you have opened the
Python folder, browse and open the
Scripts folder and copy its location. Also verify that the folder contains the
Step 3: Now open the
Scripts directory in the command prompt using the
cd command and the location that you copied previously.
Step 4: Now install the library using
pip install torch command. Here’s an analogous example:
After having followed the above steps, execute our script once again. And you should get the desired output.
Solution Idea 3: Conda
To install PyTorch on Conda using
pip, follow these steps:
Step 1: Create a Conda environment by entering the following command (add your respective Python version on Conda).
conda create -n env_pytorch python=3.9
Step 2: Activate the environment using the command:
conda activate env_pytorch
Step 3: Use pip to install PyTorch by running the command:
pip install torchvision
This will install both
Step 4: To confirm that PyTorch has been successfully installed, open a Python shell and import the libraries using the following command:
import torch import torchvision
If you are using Anaconda Prompt, you can install PyTorch using the following command:
conda install -c pytorch pytorch
Other Solution Ideas
- You may need to
pip install numpyand
pip install scipyfirst, then go to pytorch.org and use the most recent PyTorch installation routine.
- You may simply need to close and restart the console and re-enter your environment.
ModuleNotFoundErrormay appear due to relative imports. You can learn everything about relative imports and how to create your own module in this article.
- You may have mixed up Python and pip versions on your machine. In this case, to install
torchfor Python 3, you may want to try
python3 -m pip install torchor even
pip3 install torchinstead of
pip install torch
- If you face this issue server-side, you may want to try the command
pip install --user torch
- If you’re using Ubuntu, you may want to try this command:
sudo apt install torch
- You can check out our in-depth guide on installing
- You can also check out this article to learn more about possible problems that may lead to an error when importing a library.
Also, you may want to install PyTorch using Anaconda or any other environment. You should follow the steps outlined in the official documentation to learn about the concrete dependencies necessary:
Understanding the “import” Statement
In Python, the
import statement serves two main purposes:
- Search the module by its name, load it, and initialize it.
- Define a name in the local namespace within the scope of the
importstatement. This local name is then used to reference the accessed module throughout the code.
What’s the Difference Between ImportError and ModuleNotFoundError?
What’s the difference between
Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the
ModuleNotFoundError is a subclass of the
You can see this in this screenshot from the docs:
You can also check this relationship using the
issubclass() built-in function:
>>> issubclass(ModuleNotFoundError, ImportError) True
Specifically, Python raises the
ModuleNotFoundError if the module (e.g.,
torch) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an
If an import statement cannot import a module, it raises an
ImportError. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a
The following video shows you how to resolve the
The following video shows you how to import a function from another folder—doing it the wrong way often results in the
How to Fix “ModuleNotFoundError: No module named ‘torch'” in PyCharm
If you create a new Python project in PyCharm and try to import the
torch library, it’ll raise the following error message:
Traceback (most recent call last): File "C:/Users/.../main.py", line 1, in <module> import torch ModuleNotFoundError: No module named 'torch' Process finished with exit code 1
The reason is that each PyCharm project, per default, creates a virtual environment in which you can install custom Python modules. But the virtual environment is initially empty—even if you’ve already installed
torch on your computer!
Here’s a screenshot exemplifying this for the
pandas library. It’ll look similar for
The fix is simple: Use the PyCharm installation tooltips to install Pandas in your virtual environment—two clicks and you’re good to go!
First, right-click on the
pandas text in your editor:
Second, click “
Show Context Actions” in your context menu. In the new menu that arises, click “Install Pandas” and wait for PyCharm to finish the installation.
The code will run after your installation completes successfully.
As an alternative, you can also open the
Terminal tool at the bottom and type:
$ pip install torch
If this doesn’t work, you may want to set the Python interpreter to another version using the following tutorial: https://www.jetbrains.com/help/pycharm/2016.1/configuring-python-interpreter-for-a-project.html
You can also manually install a new library such as
torch in PyCharm using the following procedure:
File > Settings > Projectfrom the PyCharm menu.
- Select your current project.
- Click the
Python Interpretertab within your project tab.
- Click the small
+symbol to add a new library to the project.
- Now type in the library to be installed, in your example Pandas, and click
- Wait for the installation to terminate and close all popup windows.
Here’s an analogous example:
Here’s a full guide on how to install a library on PyCharm.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.