Reputation: 4253
I have a script that when I run it from pycharm, it runs fine, but when executed from the command line it complains
import util as u
ModuleNotFoundError: No module named 'util'
I followed this advice here: Script running in PyCharm but not from the command line
Interpreter is the same, working directory is the same, but sys paths are different.
The module's folder looks like this
mymodule
sub_1
util.py
sub_2
...
main.py
pycharm shows all folders when printing sys.path elements. when running from the command line, it doesn't show any of them, even when I run it from thr director where main.py
is.
Do I have to add the directory to PYTHONPATH
even when I run the file from that directory? Seems like overkill having every project's directory added. Is there a better way to do this or is that the standard procedure?
Upvotes: 2
Views: 9198
Reputation: 4253
digitake's suggestion to use a relative path worked
import .util as u
Upvotes: 3
Reputation: 1020
I think more information is needed to make solution recommendations, but here is what I would check for:
__init__.py
file(s)-I
parameter? that would strip location context from your script and so not scan the local directory for modules or packages, so if you do, try running without it.Upvotes: 2