AWS Torch Import Error on Jupyter

I’m running an instance on AWS of the type p2.xlarge.

Once I have the instance running I connect to it from the command line as follows:

ssh -i YourKeyName.pem ubuntu@X.X.X.X

I configure Jupyter with this:

jupyter notebook --generate-config

Change the IP address config setting for notebooks:

sed -ie "s/#c.NotebookApp.ip = 'localhost'/#c.NotebookApp.ip = '*'/g" ~/.jupyter/

Clone GitHub repo:

git clone

enter the repo:

start jupiter notebook:

jupyter notebook --ip= --no-browser

Once I am in i want to import my packages:

# import resources
%matplotlib inline

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

import torch
import torch.optim as optim
from torchvision import transforms, models

the error i get is:

RuntimeError                              Traceback (most recent call last)
RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa

ImportError                               Traceback (most recent call last)
<ipython-input-1-18d2f489897d> in <module>()
      6 import numpy as np
----> 8 import torch
      9 import torch.optim as optim
     10 from torchvision import transforms, models

/usr/local/lib/python3.5/dist-packages/torch/ in <module>()
     51 sys.setdlopenflags(_dl_flags.RTLD_GLOBAL | _dl_flags.RTLD_NOW)
---> 53 from torch._C import *
     55 __all__ += [name for name in dir(_C)

ImportError: numpy.core.multiarray failed to import

My numpy version is 1.12.2.
I tried to upgrade bumpy but it seems to be already up to date:

~$ pip install numpy --upgrade
Requirement already up-to-date: numpy in /usr/local/lib/python2.7/dist-packages

I have seen a solution from this other post:

However when I attempt to update i get the following error:

ubuntu@ip-X-X-X-X:~$ pip install numpy -I
Collecting numpy
  Using cached
Installing collected packages: numpy
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pip/", line 215, in main
    status =, args)
  File "/usr/local/lib/python2.7/dist-packages/pip/commands/", line 342, in run
  File "/usr/local/lib/python2.7/dist-packages/pip/req/", line 784, in install
  File "/usr/local/lib/python2.7/dist-packages/pip/req/", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/usr/local/lib/python2.7/dist-packages/pip/req/", line 1064, in move_wheel_files
  File "/usr/local/lib/python2.7/dist-packages/pip/", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/local/lib/python2.7/dist-packages/pip/", line 323, in clobber
    shutil.copyfile(srcfile, destfile)
  File "/usr/lib/python2.7/", line 83, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.pyc'

I attempted a sudo install for numpy but the version is unchanged.
Any directions on how to proceed?


I realized i need to update my requirements.txt file to get the most recent numpy version.


however i am still getting errors such as:

AttributeError: ‘FloatTensor’ object has no attribute ‘requires_grad’

I guess my main issue with torch right now is figuring out what stable version I can use and how to update my EC2 instance accordingly.

About the numpy issue, check what pip --version says. It has to match to the root of where your which python or python --version is pointing. On some machines pip3 will correspond to the python you are using, rather than pip.

About the 2nd issue, i.e. AttributeError: ‘FloatTensor’ object has no attribute ‘requires_grad’, I think you are on PyTorch v0.3.1 or something old. Can you print out print(torch.__version__) and make sure you are on 1.0.0?

1 Like