1

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.

David Andrews
  • 11
  • 1
  • 4

2 Answers2

0

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

Read more

Rarblack
  • 4,559
  • 4
  • 22
  • 33
  • I was under the impression you can import a directory if you create an __init__.py in the directory which imports the files in it (or a subset of files, or even a subset of functions from specific files)? – David Waterworth Dec 22 '18 at 01:43
  • That won't work either. I need to upload these as a package to was for a lambda function and need to be able to edit locally. At one point, I set my PYTHONPATH and vs code was able to recognize local modules and was able to run from any directory. – David Andrews Dec 26 '18 at 14:15
  • This should be a very easy fix. It's a PYTHON PATH variable. – David Andrews Dec 26 '18 at 14:15
0

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
user9074332
  • 2,336
  • 2
  • 23
  • 39
  • @DavidAndrews That's great. Please accept the answer if it works for you. If you have multiple functions in a single module then you could `from dir1.dir2.module_name import func1, func2` – user9074332 Dec 19 '18 at 18:48
  • I did but I do not have enough reputation votes yet. – David Andrews Dec 19 '18 at 19:02