Importing
Python3
2 package types
Regular package/folder with
__init__.py
Can contain init instruction
Namespace package without one, for high level packages that share name with other packages i.e
google/cloud/storage
google/cloud/pubsub
google and cloud should be namespace because they are shared by multiple things
Alternatives
Python2
Importing from Same Dir
Make an empty file called init.py in the same directory as the files. That will signify to Python that it's "ok to import from this directory". Then just:
Importing from Subdirectory
The same holds true if the files are in a subdirectory - put an init.py in the subdirectory as well.
main.py
Importing from Sister Directory
Yea.., you need to add the root directory to PYTHON_PATH
could also pip install -e
File IO In Imported Files
Are you trying to open a file in a package that you then import? Well that means you are getting an error because the path will be different if you run the script and import the script
join()
prepends current working directory, but the documentation says that if some path is absolute, all other paths left of it are dropped. Therefore, getcwd() is dropped when dirname(file) returns an absolute path. Also, therealpath
call resolves symbolic links if any are found
Then
Last updated