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.
Related
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
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 *
I get an import error "unable to import module" when I try to import a module
File structure
src
modules
__init__.py
authenticate_developer
twttier_importer
notebooks
main.ipynb
unit_tests
test_authenticate_developer
In test_authenticate_developer
import requests
from nose.tools import assert_true
import os
import sys
sys.path.append(os.path.abspath('../modules'))
import settings
import twitter_importer #returns import error
import authenticate_developer #returns import error
However when I use the same syntax in my notebooks it is successful.
import sys
import os
sys.path.append(os.path.abspath('../modules'))
sys.path.append(os.path.abspath('../'))
import twitter_importer
import authenticate_developer
import settings
I have looked at existing answers and I tried them out i.e., removing init.py from the root folder and removing or adding init.py to the tests folder. None seems to work for me.
I managed to get someone to help me with these and this is how we tackled it.
src
modules
__init__.py
authenticate_developer
twttier_importer
notebooks
main.ipynb
unit_tests
__init__.py
test_authenticate_developer
In test_authenticate_developer
import os,sys,inspect
sys.path.append(os.path.abspath('../modules'))
#This is to access the parent folder in order to import config
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
import config
from modules.authenticate_developer import Authenticate
I cannot explain why I have to do it differently in the test files but this is my work around
I don't understand what the below code does.
from os import path
import sys
sys.path.append(path.abspath('../ProgA'))
from os import path
imports path from the os module
this is used in line 3
import sys
imports the sys module
sys.path.append(path.abspath('../ProgA'))
This line is kinda tricky so ill break it down
path.abspath() takes a relative path (from the file executing the code) and gives you the absolute path to that
for example if you are in the directory /home/user/adi/documents and you call path.abspath('../ProgA'), the .. means you go up a directory a then look for a directory called ProgA so the final path will be /home/user/adi/ProgA
sys.path is just the place where python looks for imports
this is by default in you site packages but you edit this variable
TLDR;
the code append the directory ../ProgA to your sys.path so that you can import modules from the 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())