"No module named x" even though it was just installed with pip
Question:
I’ve created 3 files, snek.py
, requirements.txt
and runsnek.py
. runsnek.py
installs all the required modules in requirements.txt
with pip and runs snek.py
. Everything works fine on Windows 10, but when trying to run on Ubuntu (WSL2), an error is thrown:
❯ python runsnek.py
Requirement already up-to-date: pathlib in /home/rootuser/.local/lib/python3.8/site-packages (from -r requirements.txt (line 2)) (1.0.1)
Traceback (most recent call last):
File "snek.py", line 1, in <module>
from pathlib import Path
ImportError: No module named pathlib
I’m not sure what could’ve caused the problem on Linux. It might be some kind of pip modules path that isn’t defined. printenv
does not show anything containing the word python.
files
Here are all of the mentioned files.
runsnek.py
:
import os, platform
os.system('pip install --upgrade -r requirements.txt')
if platform.system() == 'Windows':
os.system('py snek.py')
elif '':
raise Warning('snek could not be ran, try running snek.py instead')
else:
os.system('python snek.py')
requirements.txt
:
# pip reqs
pathlib
snek.py
:
from pathlib import Path
cwd = Path('.')
# [...]
Answers:
It seems you are using python2 in your WSL2 instance.
In the line os.system('python snek.py')
it should run python2 instead of python3.
To correct the problem, you can change this line of code by os.system('python3 snek.py')
.
Your run
file can be simplified:
import sys, os
print('Running with ' + sys.executable)
os.system(sys.executable + ' -m pip install --upgrade -r requirements.txt')
os.system(sys.executable +' snek.py')
sys.executable
always contains the path of the python interpreter running the current script. Using python -m pip install
also ensures that the same python interpreter is used for pip installing, which solves your original problem
I’ve created 3 files, snek.py
, requirements.txt
and runsnek.py
. runsnek.py
installs all the required modules in requirements.txt
with pip and runs snek.py
. Everything works fine on Windows 10, but when trying to run on Ubuntu (WSL2), an error is thrown:
❯ python runsnek.py
Requirement already up-to-date: pathlib in /home/rootuser/.local/lib/python3.8/site-packages (from -r requirements.txt (line 2)) (1.0.1)
Traceback (most recent call last):
File "snek.py", line 1, in <module>
from pathlib import Path
ImportError: No module named pathlib
I’m not sure what could’ve caused the problem on Linux. It might be some kind of pip modules path that isn’t defined. printenv
does not show anything containing the word python.
files
Here are all of the mentioned files.
runsnek.py
:
import os, platform
os.system('pip install --upgrade -r requirements.txt')
if platform.system() == 'Windows':
os.system('py snek.py')
elif '':
raise Warning('snek could not be ran, try running snek.py instead')
else:
os.system('python snek.py')
requirements.txt
:
# pip reqs
pathlib
snek.py
:
from pathlib import Path
cwd = Path('.')
# [...]
It seems you are using python2 in your WSL2 instance.
In the line os.system('python snek.py')
it should run python2 instead of python3.
To correct the problem, you can change this line of code by os.system('python3 snek.py')
.
Your run
file can be simplified:
import sys, os
print('Running with ' + sys.executable)
os.system(sys.executable + ' -m pip install --upgrade -r requirements.txt')
os.system(sys.executable +' snek.py')
sys.executable
always contains the path of the python interpreter running the current script. Using python -m pip install
also ensures that the same python interpreter is used for pip installing, which solves your original problem