I am new to Python. I am using the following directory structure and am trying to import module OrgRepo into Func1. I am using a virtualenv and vs code as my IDE.
src/
├── Functions
│ ├── Func1
│ └── Func2
└── Shared
├── __init__.py
├── Repositories
│ ├── __init__.py
│ ├── OrgRepo
└── Utilities
├── __init__.py
└── DictUtil
I have also tried this without `init.py'
This is my PATH:
['/Users/username/Documents/Projects/myproject/name/src/Functions/Func1', '/Users/username/anaconda3/envs/my_virtual_env/lib/python37.zip', '/Users/username/anaconda3/envs/my_virtual_env/lib/python3.7', '/Users/username/anaconda3/envs/my_virtual_env/lib/python3.7/lib-dynload', '/Users/username/.local/lib/python3.7/site-packages', '/Users/username/anaconda3/envs/my_virtual_env/lib/python3.7/site-packages']
I have tried the following in order to import OrgRepo into Func1:
1: from .Shared.Repositories import OrgRepo
ModuleNotFoundError: No module named '__main__.Shared'; '__main__' is not a package
2: from ..Shared.Repositories import OrgRepo
'
ValueError: attempted relative import beyond top-level package
3: from src.Shared.Repositories import OrgRepo
ModuleNotFoundError: No module named 'src'
4: `from Shared.Repositories import OrgRepo1
'ModuleNotFoundError: No module named 'Shared'
5: I am using VS Code and when I try to save the file:
It automatically changes to this:
import OrgRepo
import DictionaryUtilities
import datetime
import json
import sys
sys.path.insert(0, 'src/Repositories/')
6:
import sys
sys.path.insert(
0, '/Users/username/Documents/Projects/project/m/src/Repositories')
import OrgRepo
and this:
sys.path.insert(0, 'Repositories')
sys.path.insert(0, .'Repositories')
sys.path.insert(0, ..'Repositories')
Upon running or saving, vs code changes it to this:
import OrgRepo
import sys
sys.path.insert(
0, '/Users/username/Documents/Projects/project/m/src/Repositories')
and received this error:
ModuleNotFoundError: No module named 'OrgRepo'
I was able to install this with PIP and import it, but that does not suit our needs.
I have read these posts: Importing files from different folder
Python import modules, folder structures
How to Import Multiple Python Modules from Other Directories
How to properly import python modules from an adjacent folder?
I tried to read/understand a few other posts as well . . . I even tried to bang on the Flux Capacitor a few times to no avail . .
EDIT: I am using this code to upload as an AWS Lambda function. While the sys.path solution works locally it makes it does not fit into my workflow. This requires me to change the sys.path to import while uploading and causes problems with Intellisense. I would like to be able to be able to import the module directly. e.g. import OrgRepo so Intellisense does not throw errors and I can zip and upload my package to AWS. I have no problem uploading my package to AWS when I am able to import <module_name>.
I activate my environment in Anaconda and then export the following PYTHONPATH environment variable:
export PYTHONPATH=src/shared/repositories:src/shared/utilities
I also tried export PYTHONPATH=$PATH:src/shared/repositories:src/shared/utilities
This worked for a period of time and now I am getting PYTHON[unresolved-import] with IntelliSense. I do not appear to get this error when I try to run the script from the directory above /src.
I would be so grateful if somebody can show me how I can import my modules using the standard import <module> and have it consistently work.
-
1Is OrgRepo a python file or directory?Rarblack– Rarblack2018年12月19日 03:53:58 +00:00Commented Dec 19, 2018 at 3:53
-
Have you checked your $PYTHONPATH env variable? What is its value?Farzad Vertigo– Farzad Vertigo2018年12月19日 04:00:22 +00:00Commented Dec 19, 2018 at 4:00
-
OrgRepo was renamed to org_repo.py and it is a file. Path is src/Shared/Repositories.org_repo.pyDavid Andrews– David Andrews2018年12月19日 16:52:08 +00:00Commented Dec 19, 2018 at 16:52
-
$PYTHONPATH is empty.David Andrews– David Andrews2018年12月19日 16:52:40 +00:00Commented Dec 19, 2018 at 16:52
-
$PYTHONPATH =src/shared/utilities/:src/shared/repositories:src/shared/services:src/lambda_functions/get_pixelDavid Andrews– David Andrews2018年12月28日 01:47:56 +00:00Commented Dec 28, 2018 at 1:47
2 Answers 2
Basically to mention a directory cannot be imported but a file in the directory can be imported.
Let's say you have Org.py file in OrgRepo and you can do :
from src.Shared.Repositories.OrgRepo import Org
or if you want to call a specific method from it lets say do_it:
from src.Shared.Repositories.OrgRepo.Org import do_it
3 Comments
I think what you're trying to do is something like the below. I've cleaned up some of the directory names (typically directories are lowercase with underscores and Class names are uppercased), added .py extensions to the python files, and tried to create a minimalistic environment to replicate your scenario. Hopefully this is helpful.
Setup the environment
$ mkdir src; mkdir src/functions; touch src/functions/func1.py; mkdir src/shared; mkdir src/shared/repositories; touch src/shared/repositories/org_repo.py
$ tree
.
└── src
├── functions
│ └── func1.py
└── shared
└── repositories
└── org_repo.py
# src/shared/repositories/org_repo.py
def a_cool_func():
print(f'hello from {__file__}')
# src/functions/func1.py
import pathlib
import sys
file_path = pathlib.Path(__file__)
path = file_path.resolve().parents[2]
sys.path.append(str(path))
from src.shared.repositories.org_repo import a_cool_func
print(f'hello from {__file__}')
a_cool_func()
Run it
# note that there is no significance to /Users/username/tmp/tmp/
# this is just the directory where the test environment was setup
$ python src/functions/func1.py
hello from src/functions/func1.py
hello from /Users/username/tmp/tmp/src/shared/repositories/org_repo.py
2 Comments
from dir1.dir2.module_name import func1, func2