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.