Currently I have a “main” folder where all of the modules I write eventually go, but I usually place the modules I’m currently working on in a ‘dev’ folder just so I don’t clutter up the other folder with stuff that aren’t ready.
The structure looks like this
MyProg |-run.py |-\lib | |-someLibrary.py | ... | |-\main | |-readyScripts.py | ... | |-\dev |-inProgress.py
Run.py will import scripts from the main folder.
Scripts in the main folder use relative imports to import someLibrary from the lib folder, and it works fine.
However, it doesn’t work when I’m still writing my program in the dev folder and running it directly from there (ie: python inProgress.py), saying that I “attempted relative import in non-package”
Is there a way to be able to import modules from the lib folder while I am working on scripts in dev?
EDIT: this is my import statement in inProgress.py:
from .lib import someLibrary
Ideally, I would like to keep it this way so that when I move it to the main folder, I won’t have to do anything to the import statement.
If you have your PYTHONPATH set to MyProg, what you can do is create an empty
__init__.pyfile in each folder so python recognizes them as modules:So in your inProgress.py file, you could use:
In your run.py you could do this: