How to import python function from other folder? - python

I want to import function from bot/main.py into bybitapi/server.py. The structure of project looks like this:
So, in server.py I was trying to do it like this:
from bot.main import notifyuserwithtriggeredalarms
But it doesn't work, it was about bot package doesn't exists. Also, I was trying to solve this using os and sys, I couldn't, so, how can I import function in server.py though other folder?

mport a Module From the Parent Directory in Python by Adding It to PYTHONPATH
import os, sys
p = os.path.abspath('.')
sys.path.insert(1, p)
from main import notifyuserwithtriggeredalarms

Related

Import script from another script in python

I have imported a repo using git and when I try to run a file, it shows that "it could not be resolved".
The running script has:
import craft_utils
import test
import imgproc
import file_utils
The imported things are all script.
I have attached the screenshots for the error and hierarchy.
How can I solve this? Thanks.
try adding an empty file named __init__.py in the root folder CRAFT-pytorch (instead of the basenet folder)
create a empty file named __init__.py
it would solve the issue
Add a file inside of the folder called __init__.py
First you need to create an empty file named __init__.py in the folder CRAFT-pytorch
Second add these line at the top of the file you're trying to running, before the imports:
import os
import sys
import inspect
app_path = inspect.getfile(inspect.currentframe())
main_dir = os.path.realpath(os.path.dirname(app_path))
sys.path.insert(0, main_dir)
import craft_utils
import test
import file_utils

How to run a python script with modules inside?

I have a test.py file that I try to run in VSCode with the "Run" button.
I have a string from file_folder.file import something inside it. When I try to run my test, VSCode gives me an error ModuleNotFoundError: No module named 'file_folder'.
My project tree looks something like this:
root_folder -- file_folder -- file.py
-- test_folder -- my_folder -- test.py
Can I make it work in VSCode somehow?
You can try something like this:
import sys
sys.path.insert(1, '<pathToFolder>/file_folder')
from file import something
For cross directory access, we usually use relative paths to facilitate code portability.
import sys
from os.path import dirname, abspath
project_path = dirname(dirname(abspath(__file__)))
sys.path.append(project_path + "\\file_folder")
from file import *

Python can't find module when running in linux

When I go to the file path cd to the file path ~/repo/analysis_tools/fresh_sales/ and then run python3 apicall.py it runs fine but when I try and add it to cron using python3 ~/repo/analysis_tools/fresh_sales/apicall.py the python code returns the error: No module named 'utils'.
My current project structure:
Analysis Tools:
- utils:
+ builders.py
+ load_config.py
- fresh_sales:
+ apicall.py
The start of my code:
import sys
import os
sys.path.append('..')
sys.path.append(os.path.dirname(os.path.realpath("..")))
sys.path.insert(0, '')
from utils.load_config import load_config
import requests
import json
from pandas.io.json import json_normalize
from utils.builders import build_local_db_from_config
from datetime import datetime
from sys import exit
Your path is never changed you should add the project root to the path:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
You could also try relative imports but this would be easier if you had something like main.py in the project root. Calling scripts higher in the directory tree is a potential sign of incorrect structure, but not always.

Import module from file does not work

I am running a jupyter-notebook file (file.ipynb) and trying to import a module "eval_numerical_gradient" from python file "gradient_check" in folder "utils". However, the following code does not work.
from utils.gradient_check import eval_numerical_gradient
Then I try this code, which works:
import sys
sys.path.append("/Users/W/dlp/src/03/utils")
import gradient_check
from gradient_check import eval_numerical_gradient
My question is what is the difference of the two ways above, and is it possible to let the first code work out?
just because you have it under folder utils does not make utils a package. You need an __init__.py file under folder utils if you want to define it as a module.
__init__.py: (place this under utils folder)
from .gradient_check import eval_numerical_gradient
file.ipynb:
import sys
sys.path.append("/Users/w/dlp/src/03")
from utils import eval_numerical_gradient

import a module in current working directory

I have following directory structure,
/
__init__.py
__meta__.py
I tried to import __meta__ in a file but python complains about not finding a module named __meta__. I checked the current working directory for the file useing os.getcwd() which printed the directory with __meta__.py.
But I can import the module from python interpreter.
Append it to sys path first, and then try to import your module
import os
import sys
sys.path.append(os.getcwd())

Categories