Issue
When running a Python module (inside a package) I always use the option -m from the parent folder.
Let's suppose that we have the following directory of files:
folder-that-contains-root-project-folder/
root-project-folder/
package/
__init__.py
module.py
venv/
I always run module.py from the folder root-project-folder
as python -m package.module
and it works.
But how could I run module.py from folder-that-contains-root-project-folder
? What I tried so far is python -m root-project-folder/package.module
but it doesn't work.
Edit: The error when running from folder-that-contains-root-project-folder
is:
/home/eduardo/Desktop/folder-that-contains-root-project-folder/root-project-folder/venv/bin/python:
Error while finding module specification for
'root-project-folder/package.module' (ModuleNotFoundError: No module
named 'root-project-folder/package')
PD: I'm using bash on Ubuntu
Solution
There are two solutions here:
Run
python -m root-project-folder.package.module
. Basically, you treatrood-project-folder
as a module and use.
to separate the folders rather than/
just like you do in Python code.Add
root-project-folder
toPYTHONPATH
. Now you will be able to runpython -m package.module
as usual from any folder on your machine.
Answered By - Code-Apprentice