I'm using a from . import module
statement to do exactly that: import a local module to my script. The script and module reside in the same folder.
# module.py
def foo():
print('Foo!')
# script.py
from . import module
module.foo()
> ImportError: cannot import name 'module'
This should be pretty easy, and doing just import module
does work, but as this answer suggests one should, I modified the statements to the former form.
The end goal is to have a package, from which I can use things, but also to have executable scripts inside the package that import other parts of that package. Apparently, after a few days worth of searching and a few questions I still don't quite understand the import and packaging machinery.
These might be the cause:
- Import statements are different in 2.7 and 3.x, I'm using 3.6, the question was on 2.7
- Relative imports are different inside packages (folder with
__init__.py
) - The working directory is different or the folders are not in
sys.path
Having an __init__
file does not make a difference at least in a fresh project in PyCharm. Also, the working directory is set to the folder of the sources and it is in path
.
Have I missed something? Or rather, what's the correct way of achieving the functionality described in the end goal? Any help is greatly appreciated!