Importing files from different folder, "__init__.py" does not work - python

I have seen many responses suggesting including an __init__.py file in the subdirectory of submodules in order to import them as python package, but I can't get it working for my project. My project directory structure looks like this:
helm-2022
├── model
│   ├── __init__.py
│   ├── model.ipynb
│   └── torchModelSummary.py
├── preprocess
│   └── preprocess.py
└── utils
├── __init__.py
└── vis_utils.py
I want to import functions inside vis_utils.py in the notebook model.ipynb under model folder and preprocess.py under the preprocess folder. I have already added empty __init__.py under the utils folder. When I tried to import in model.ipynb using from utils import vis_utils, I still got No module named 'utils'. I have also tried to import by including the top directory from helm-2020.utils import vis_utils, but that gives me a syntax error because of the hyphen. I don't have permission to change the top directory name, so changing the hyphen is not an option. Thank you so much in advance.

Related

How to run Python files in example folder?

I am creating a Python package and want to make the example files in the example folder runnable with something like python example_file.py. How do I do it? My example_file.py and __init__.py look like below:
# example_file.py
from demandforecast import DemandForecast # module coded in demandforecast.py
if __name__ == '__main__':
# Example code here
# __init__.py
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))
When I navigate to the examples directory and run python example_file.py I get the following error:
Exception has occurred: ModuleNotFoundError
No module named 'demandforecast'
This can be solved by adding using the old sys.path.insert() trick mentioned in Importing files from different folder, but I would rather not include the absolute path because this will vary from user to user.
Here is my directory structure:
├───demandforecast
│ demandforecast.py
│ __init__.py
│
└───examples
example_file.py
I have tried a few posts here, such as Relative imports in Python 3, but without luck. Adding sys.path.append('../') to the example file before the demandforecast import also did not work.
I think your import is simply incorrect, and should be:
from demandforecast.demandforecast import DemandForecast
Since demandforecast is both the package name (the directory is definitely a package, with the __init__.py file) and a module name within that package. (I assume DemandForecast is a class within the demandforecast module.)
from src.pro.demandforecast.demandforecast import foo
if __name__ == '__main__':
foo()
output:
foo
create a folder src and inside of it create a package pro and now inside pro create two packages demandforecast and examples.
Directory structure:
/src$ tree
.
└── pro
├── demandforecast
│   ├── demandforecast.py
│   ├── __init__.py
│   └── __pycache__
│   ├── demandforecast.cpython-38.pyc
│   └── __init__.cpython-38.pyc
├── examples
│   ├── example_file.py
│   └── __init__.py
├── __init__.py
└── __pycache__
└── __init__.cpython-38.pyc
IDE:

ImportError: cannot import name 'MovieGroupProcess' from gsdmm

I'm having some import issues using a Python module I cloned from GitHub.
My project structure looks like such
-Jupyter_notebook.ipynb
-gsdmm/
The gsdmm/ folder was cloned into the same directory as the notebook from GitHub here using
git clone https://github.com/rwalk/gsdmm.git
Now my import error happens anytime I try to import the 'MovieGroupProcess' class from the module as shown in the readme. I'm running it from a Jupyter notebook but same issue exists in Python script
from gsdmm import MovieGroupProcess
where I get the following error:
ImportError: cannot import name 'MovieGroupProcess' from 'gsdmm' ([current_directory]\gsdmm\__init__.py)
I'm not sure if something was messed up with my paths or needs to be changed.
If this is your project structure
.
├── gsdmm
│   ├── gsdmm
│   │   ├── __init__.py
│   │   └── mgp.py
│   └── test
└── Jupyter_notebook.ipynb
Then trying to import gssdmm from your notebook Jupyter_notebook won't work as there is not __init__.py file inside the first gsdmm directory.
But if you change the project structure to
.
└── gsdmm
├── gsdmm
│   ├── __init__.py
│   └── mgp.py
├── test
└── Jupyter_notebook.ipynb
Now when you import gsdmm python can find the __init__.py file and can import the module successfully.

What 'Attempted relative import beyond top-level package' error means in Python?

I'm using python 3.7 and ran into a relative import error "Attempted relative import beyond top-level package" with the following folder structure:
├── app
│   ├── __init__.py
│   ├── services
│   │   └── item_service.py
│   └── views
│   ├── home.py
│   ├── __init__.py
My goal: import variable foo from the top level _init_.py to item_service.py using
from .. import foo
Pylint gives the error when trying this.
However the same exact import statement works in home.py, and if I add a empty _init_.py file to the services folder, the import works.
So my my question is, why? Does python require your module to be in a subpackage in order to relatively import parent package's contents?
For me it got resolved in following ways:
First import the directory (import dir)
Then try to import the views/class (from dir import views/class)
To solve:
Add init.py to all directories involved.
Add
sys.path.append("..")
BEFORE importing from sibling directory.

Import python project modules from tests subdirectory

├── ledger
│   ├── __init__.py
│   ├── ledger_data.py
│   └── ledger_model.py
├── main.py
├── sscommon
│   ├── __init__.py
│   └── logging.py
└── tests
└── test_ledger_data.py
I need to import classes from ledger_data module when running test_ledger_data.py. I currently do sys.path.append("../") in test_ledger_data.py or I have to add symbolik links to all modules being used to tests directory. Both options seem incorrect. How to do it correctly?
If I just run the file either from project root or tests directories I get error:
from ledger.ledger_data import LedgerData
ImportError: No module named 'ledger'
You can create an __init__.py file in your folder, and import the parent dir using:
parent_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), os.pardir))
sys.append(parent_dir)
This uses os.path to find out the directory based on your file location.
Update: create the above __init__.py and reside it inside tests/ folder.
Then, in your test_ledge_data.py put at the head of the file from __init__ import *; this will import everything in your init file to your module namespace.

Import other files of module from module base

I'm currently writing a Python module.
Please take a look at the following module structure:
gjms
├── data.db
├── event
├── games
│   └── game
├── __init__.py
├── user
│   └── __init__.py
└── util
├── database.py
├── email.py
└── password.py
Is it possible to set my Python path in a way, so that for I can always import from the module base? As an example: I want to import the database.py file from the util module in the user module like so:
import gjms.util.database
It would be preferable to be able to do this from any place in the module.
Thanks for your help!
If the parent directory of gjms is listed in sys.path and you have a __init__.py file in gjms then your import will work from anywhere in your project.
In other words, gjms must be a package, making it importable.

Categories

Resources