└── Project
├── __init__.py
├── main.py
├── utils.py
└── auth
├── __init__.py
└── users.py
In my users.py file, I want to import data from project/utils.py, but when I am running imports it says module not found.
/users.py
from project.utils import data #have tried
from .. import utils #have tried
it will be great if anyone can tell me how do I make my project globally available so I can import stuff directly from it.
Related
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.
This question has been asked in different contexts, but I still am unable to get a successful absolute import.
I have this directory structure
21:48:07$ tree
start_dir
└── top_package
├── README.md
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ └── secrets.cpython-37.pyc
├── subpackage1
│ ├── __init__.py
│ └── test.py
├── requirements.txt
├── secrets.py
└── subpackage2
└── __init__.py
All I want to be able to do is from test.py be able to import from secrets. Here is the contents of test.py:
# import top_package # ImportError: No module named top_package
# from top_package import secrets # ImportError: No module named top_package
import top_package.secrets # ImportError: No module named top_package
None of these have worked. I've read through multiple pages on having multiple subpackages in a top level package but still no dice.
Is this simply because I'm trying to execute the file directly by running python test.py instead of calling this from start_dir? If so, what's the best way to develop and work around this?
Goal: I want to be able to absolute import secrets from test.py. If possible, I do not want to do anything with the site-packages or with the setup.py. I want to be able to run python test.py and have the line: import top_package not give a ImportError no module found. Please advise the best course of action.
So you created top_package and want to import it anywhere within your system. You can do so by copying the package folder to the site-packages folder of your environment. This is a path where python would look while importing packages.
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.
├── 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.
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.