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())
Related
File "C:\Users\sande\OneDrive\Desktop\two.py", line 8, in
import score.py
ModuleNotFoundError: No module named 'score.py'; 'score' is not a package
You can solve this issue by using this:
import score
If this score.py is not is not in same folder as your two.py then visit How to import the class within the same directory or sub directory to solve that issue.
Don't use import scope.py, if you are having problem importing try import scope (if both the files are in the same directory) or if the files are in different directories you can use the sys module in python
# importing sys
import sys
# adding Folder_2 to the system path
sys.path.insert(0, '/C:/Users/sande/OneDrive/{drive/folder}/scope.py')
# importing the functions/class
If you are still getting the error, try removing
if name == "main":
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
|Project
|--m1Folder
|--|--__init__.py
|--|--m1.py
|--m2Folder
|--|--__init__.py
|--|--m3Folder
|--|--|--__init__.py
|--|--|--m3.py
m1Folder and m2Folder are inside of Project. m3Folder is inside of m2Folder. All m*Folder contains empty __init__.py.
How to import m1.py, from m3.py file?
I tried from m1Folder import m1 as mo inside m3.py file and gave ModuleNotFoundError: No module named 'm1Folder'. However, pylint in vscode did not show any error.
from ...m1Folder import m1 as mo gives ValueError: attempted relative import beyond top-level package
You can do it by changing the system path with the sys module.
import sys
sys.path.insert(1, '/path/to/application/app/folder')
import m1
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.
I am creating a python module. To test it i put the file in the same directory and then wrote the code
import mymodule
mymodule.dofunction
python then said >>>no module named mymodule but they are in the same directory.
Adapting from previous answer here. Explicitly state that you want to use the current directory.
Also, consider that you need an "__init__.py" file in each directory you are importing from.
import os, sys
lib_path = os.path.abspath('.')
sys.path.append(lib_path)
import mymodule
More information on the import system here.