Python3 -m /path/to/file is giving me an error, whereas python -m /path/to/file is not
Question:
I have been using the mod option on the command line with python (Python 2.7.X) for a while, and I am now switching to Python 3.
python -m path/to/file
Now when I try to do the same procedure, but with Python 3, literally the same line:
python3 -m path/to/file
I am given the following error:
/usr/bin/python3: No module named path/to/file
I have been reading the documentation for both Python 2.X and Python 3.X about the -m option, and I have yet to figure out how the small changes made for 3.X have rendered the above mentioned utilization on my side broken!
Answers:
Python3 won’t look for Python2 modules. You need to install modules specifically for Python3.
To give a concrete example: Under Debian, package python-numpy installs to /usr/lib/python2.7/dist-packages/numpy/
whereas package python3-numpy installs to /usr/lib/python3/dist-packages/numpy/
. Python2 will only search in /usr/lib/python2.7/dist-packages/
; Python3 will only search in /usr/lib/python3/dist-packages/
.
Accept this as it is; don’t try to modify the search paths of your local installation, lest you will end in a terrible mess. Rather do reinstall each single module in its Python3 variant.
The usage is not:
$ python -m path/to/file
but rather
$ python -m package.subpackage.module
The -m
flag adds the current directory to your Python path, then it looks on the Python path for the module to open the program with. For example:
$ cat > foo.py
import sys
print(sys.version)
$ python -m foo
2.7.8 (default, Jul 28 2014, 01:34:03)
[GCC 4.8.3]
$ python -m /foo
/usr/bin/python: No module named /foo
$ python -m ~/foo
/usr/bin/python: No module named /cygdrive/c/Users/user/foo
$ python -m ~/foo.py
/usr/bin/python: Import by filename is not supported.
I have been using the mod option on the command line with python (Python 2.7.X) for a while, and I am now switching to Python 3.
python -m path/to/file
Now when I try to do the same procedure, but with Python 3, literally the same line:
python3 -m path/to/file
I am given the following error:
/usr/bin/python3: No module named path/to/file
I have been reading the documentation for both Python 2.X and Python 3.X about the -m option, and I have yet to figure out how the small changes made for 3.X have rendered the above mentioned utilization on my side broken!
Python3 won’t look for Python2 modules. You need to install modules specifically for Python3.
To give a concrete example: Under Debian, package python-numpy installs to /usr/lib/python2.7/dist-packages/numpy/
whereas package python3-numpy installs to /usr/lib/python3/dist-packages/numpy/
. Python2 will only search in /usr/lib/python2.7/dist-packages/
; Python3 will only search in /usr/lib/python3/dist-packages/
.
Accept this as it is; don’t try to modify the search paths of your local installation, lest you will end in a terrible mess. Rather do reinstall each single module in its Python3 variant.
The usage is not:
$ python -m path/to/file
but rather
$ python -m package.subpackage.module
The -m
flag adds the current directory to your Python path, then it looks on the Python path for the module to open the program with. For example:
$ cat > foo.py
import sys
print(sys.version)
$ python -m foo
2.7.8 (default, Jul 28 2014, 01:34:03)
[GCC 4.8.3]
$ python -m /foo
/usr/bin/python: No module named /foo
$ python -m ~/foo
/usr/bin/python: No module named /cygdrive/c/Users/user/foo
$ python -m ~/foo.py
/usr/bin/python: Import by filename is not supported.