Cannot import function name from another file - python

I'm going to go ahead and ask question # 2,345,651 about file imports in python. But I've been on SO for the last hour and have just sunk two hours in the drain trying to port a function from one directory into another.
My situation:
I have the following application with the current file structure:
trading/
-- app/
models.py
-- db/
engine.py
trading, app, and db all have an __init__.py file inside of them.
At first I tried different combinations of this:
from trading.db.engine import load_engine
from db.engine import load_engine
from .db.engine import load_engine
from ..db.engine import load_engine
I get the following error messages, respectively:
ModuleNotFoundError: No module named 'trading'
ModuleNotFoundError: No module named 'db'
ImportError: attempted relative import with no known parent package
ImportError: attempted relative import with no known parent package
I also know there is the PYTHONPATH value that you can set, and I've done that too.
Here are screenshots of my current environment variables in Windows System Manager:
For my entire system:
For my own username:
But yet I cannot use the load_engine function from the engine.py in the db folder for the models.py file in the app folder.
Genuinely looking for help. Thank you.

Related

Import Error: attempted relative import with no known parent package

Hello i have a difficulty in understanding this concept of package and don't know why i am getting this error
i have a structure like this
./unittests:
rfcg.py
./tests:
test_misc.py
__init__.py
the unitests is the top folder containing rcfg module and subfolder tests that contains test_misc.py
in test_misc.py when i try to import
from ..rcfg import some_class
it has been auto completed by vscodem, but when i print it is giving me error, the error is
print(run_cfg) -----> run_cfg is some_class
from ..rcfg import run_cfg
ImportError: attempted relative import with no known parent package
i have also created a init.py in subfolder tests also but still it is the same

import module in Django

I have a project with structure like on this picture.
Folders structure
Where 'backend' folder is Django project folder.
I need to import module from another folder 'main' inside Django app file, i.e. import main.Text_Generator in backend.app.views file.
I tried: from ...main.Text_Generator import *. This raise an error while running a server: "attempted relative import beyond top-level package"
And from main.Text_Generator import *, also error "No module named 'main'"
What is the correct way to do such import?
Add this:
import sys
sys.path.append("..")
And then you should be able to get it with:
from main.Text_Generator import *
You're using a module outside your Django project. I would recommend moving the folder inside your project directory [or app directory] rather than messing with your PATH. If you move main inside backend your existing stuff will work.

Importing file from same directory working in one python file but not another?

I have these two python files.
api.py has a class called API, which is imported by __init__.py using from api import API. Before this file was named __init__.py, this worked perfectly. But when I changed the name to __init__.py so that I could use it with Flask, I now get the error Unable to import 'api' under the from keyword. Doing from directoryName.api import API works, but the directory name is different on my computer and on my VPS. I could just change the name of the directory to make them the same, but I'd prefer if there was a way that I could change the directory name at any point and it wouldn't break the script.
Does anyone know, firstly, why this doesn't work in __init__.py? And secondly, a way to import a file from the same directory in the init file?
Thanks!

Django: no module named http_utils

I created a new utils package and an http_utils file with some decorators and HTTP utility functions in there. I imported them wherever I am using them and the IDE reports no errors, and I also added the utils module to the INSTALLED_APPS list.
However, when launching the server I am getting an import error:
ImportError: No module named http_utils
What am I missing? What else do I need to do to register a new module?
Make sure the package is correct (Include init.py file).
Make sure there are no other utils files in the same directory level. That is if you are importing from utils import http_utils from views.py, there should not be a utils.py in the same folder. Conflict occurs because of that.
You dont have to include the folder in the INSTALLED_APP settings. Because the utils folder is a package and should be available for importing
As stared by Arundas above, since there is a utils.py file I suggest renaming the module to something such as utilities, and also make sure you have a __init__.py file in that directory.
from utilities.http_utils import class_name

global/app lib folders import in Django

I have a Django structure like this (only showing libs):
project/lib/ # Global libraries that will be used cross apps
project/lib/global_stuff.py
project/apps/app1/lib/special_for_app1.py
project/apps/app2/lib/special_for_app2.py
Some apps don't have a lib folder.
from apps.app1.lib import special_for_app1 works fine. But how can I import from the global lib folder when I am inside a folder already containing a local lib folder?
From inside the apps views.py file on one of the apps:
from lib import global_stuff
Gives me ImportError: cannot import name global_stuff
from .lib import global_stuff
Gives me ImportError: cannot import name global_stuff
from ..lib import global_stuff
Gives me ImportError: No module named lib
from ...lib import global_stuff
Gives me ValueError: Attempted relative import beyond toplevel package
from project.lib import global_stuff
Works, but I really dont want to be stuck with using the project name itself in the import.
Is there any way to solve this, not using the project name in the import, or changing the whole lib idea.
Or is there any other good practice to store the main part of the code?
You are correct for not wanting to associate the project name with the imports, so there is a common pattern for this:
project/
|__/source/
| |__/lib/
| |__/app/
|__/deployment/ # code for your production deployment maybe
|
|__/docs/
|__/tests/
|__README
|__requirements.txt
and put /path/to/project inside your paths on your virtualenv(you use virtualenv right?).
Then you can do inside your code
from source.lib.blah import foo
from source.app.baz import bar
EDIT: This is only optimal if you don't release your code as open source of course. Only when you have an internal project where the management keeps changing the name of the project :D
I really dont want to be stuck with using the project name itself in the import
Why not? It's the best way. Note that 'relative imports for intra-package imports are highly discouraged', - as said in PEP-8.

Categories