Could not import runpy module
Asked Answered
P

6

14

I am trying to install a Python module on a raspberry pi that is running linux. I am using Python 2.7.11 and have tried the following commands:

python -m pip install schedule

python -m easy_install schedule

python2.7 -m pip install schedule

In each case I get the same error message:

Could not import runpy module

I get this same error message no matter what module I try to install. What could be causing this problem? I have been unable to find anything useful from Google searches I've done on the error message.

EDIT:

When I try adding sudo to the command like either of these:

sudo python -m pip install schedule or sudo /usr/bin/python -m pip install schedule

I get the error:

-sh: sudo: not found

EDIT2:

When I try python -m pip install runpy I still get:

Could not import runpy module
Preciado answered 9/6, 2017 at 17:44 Comment(12)
try sudo before your command. or maybe your computer isn't hooked up to the internetKittenish
I get a "sudo: not found" message when I try to use sudo. I am not sure why though. However I am sure that I am hooked up to the internet.Preciado
Type: which python, then use the full path to the shown binary in your sudo command instead of just python.Giesser
Have you looked at this? github.com/alexa-pi/AlexaPi/issues/228 Whatever is going on there, your python install seems hosed. runpy is a built-in moduleVocational
Have you tried importing runpy?Kittenish
@Giesser adding the full path to python in my sudo command didn't help. The problem seems to be with finding the sudo command not the python command. I got the same error.Preciado
@Kittenish I did try importing runpy but still get the "could not import import runpy module" error.Preciado
@Vocational It's possible I am having the same issue as the one in the link you provided, although I am not getting a Traceback. In the case that there is something wrong with my python install I guess I would just need to try reinstalling. At this point I just wrote my own class that does the things I needed from the schedule module, but I might try reinstalling python if I have more issues in the future.Preciado
The path is /usr/bin/sudoKittenish
unix.stackexchange.com/questions/354928/…Kittenish
@Kittenish It looks like /usr/bin/sudo doesn't exist. I have also noticed that I can't use the apt-get command as well (I get a "apt-get: not found" error). I wonder if I need to install those somehow?Preciado
really weird... askubuntu.com/questions/491374/…Kittenish
H
6

I've encountered the same error, hoping my own experience will do some help. First check your PYTHONPATH (by executing echo $PYTHONPATH in your shell) to see if it is empty. Normally, it should be empty. But if it isn't, you have to explicitly assign your python library path to it.

In my own environment, the wrong configuration is:

export PYTHONPATH=/mnt/usb/srv/homeassistant/lib/python3.4/site-packages

and my python location is:

/usr/bin/python

So, I append my python's library path to the configuration(don't forget the ':'):

export PYTHONPATH=/mnt/usb/srv/homeassistant/lib/python3.4/site-packages:/usr/lib/python3.6/site-packages:/usr/lib/python3.6/site-packages

The error is gone now.

Hamper answered 9/10, 2017 at 5:52 Comment(0)
B
4

I was experiencing this issue.

i.e. running python in the terminal, which had been working all along, now returned:

Could not import runpy module
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 15, in <module>
    import importlib.util
  File "/usr/lib/python3.10/importlib/util.py", line 14, in <module>
    from contextlib import contextmanager
  File "/usr/lib/python3.10/contextlib.py", line 6, in <module>
    from functools import wraps
  File "/usr/lib/python3.10/functools.py", line 22, in <module>
    from types import XYZ
ImportError: cannot import name 'XYZ' from 'types' (/home/_user_name/_project_name/types.py)

The same if I try pip.

The cause was that I had named one of the files in my current project types.py

The solution was to change the types.py file name to anything else. I went with _types.py for now.

Behalf answered 16/1, 2023 at 11:17 Comment(1)
I too had this issue when I tried to name a file types.py.Rama
V
2

Had the same "Could not import runpy module" error, but in Windows. It turned out, there was two pythons installed. Second one was installed with msys2 and had precedence in the PATH environment variable:

$ which pip
/path/to/my/Python3/Scripts/pip

$ which python
/path/to/my/msys64/mingw32/bin/python.exe

$PATH
...
path\to\my\msys64\mingw32\bin;
path\to\my\msys64\usr\bin;
path\to\my\Python3;
path\to\my\Python3\Scripts;
...

Changed order in $PATH:

...
path\to\my\Python3;
path\to\my\Python3\Scripts;
path\to\my\msys64\mingw32\bin;
path\to\my\msys64\usr\bin;
...
Vernal answered 5/11, 2020 at 18:48 Comment(0)
M
1

The runpy module is installed by default during the installation of the environment.

So, there might be a problem with the installation of the virtual environment. This happens if the installation was cancelled in between or an old environment was not properly removed and it was overwritten.

So, try to re-install the environment.

Macroclimate answered 10/3, 2018 at 15:32 Comment(1)
Re-installing the environment is a good option, but here are some others to try if you don't want to do that just yet - 1) make sure your virturalenv is activated 2) restart your terminal 3) make sure you're using the python version you want 4) if your IDE specifies your python interpreter, make sure it's using the right oneVariable
O
0

copy runpy.py and runpy.pyc from a system where python is installed to the target machine at /usr/lib/pythonX.Y

Problem:

python -m SimpleHTTPServer
Could not import runpy module

Copying the files from local ubuntu

scp /usr/lib/python2.7/runpy.py* root@imx6slevk-student2:/usr/lib/python2.7/

Solved

python -m SimpleHTTPServer
    Serving HTTP on 0.0.0.0 port 8000 ...
Orelle answered 9/4, 2020 at 13:9 Comment(0)
G
0

If you are runing program on a U disk like me on MAC. May be check the read-write access of the file on U disk. I reinstalled the environment as suggested above, but the problem remains. It turns out there is read/write access problem of my U disk, remount the U disk to fix this.

Glossographer answered 18/9, 2020 at 13:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.