So I have a Python3 Script that I am trying to implement. The overall project has a repository file tree that looks like this:
.
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── functions
│ ├── README.md
│ ├── __init__.py
│ ├── aws
│ │ ├── README.md
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── boto3_helper.cpython-310.pyc
│ │ │ └── boto3_helper.cpython-39.pyc
│ │ ├── boto3_helper.py
│ │ ├── boto3_session_create.py
│ │ ├── boto3_session_mock.py
│ │ ├── describe_direct_connect.py
│ │ ├── describe_vpc.py
│ │ ├── get_network_payload.py
│ │ ├── get_routing_tables.py
│ │ ├── get_vpc.py
│ │ ├── make_diagram_network_block.py
│ │ └── transform.py
│ ├── helpers
│ │ ├── __init__.py
│ │ ├── get_payload.py
│ │ └── helpers.py
│ └── peering
│ ├── __init__.py
│ └── peering.py
├── images
│ └── diagram.png
├── main.py
├── modules
│ ├── README.md
│ ├── audit
│ │ └── audit.py
│ ├── awsmaster
│ │ └── awsmaster.py
│ ├── cad
│ │ └── cad.py
│ ├── canary
│ │ └── canary.py
│ ├── cas-nonprod
│ │ └── casnonprod.py
│ ├── cas-prod
│ │ └── casprod.py
│ ├── css-dev
│ ├── css-nonprod
│ ├── css-prod
│ ├── css-staging
│ ├── didev
│ ├── dr
│ ├── eng
│ ├── factory-nonprod
│ ├── factory-sandbox
│ ├── factory-staging
│ ├── hsm-nonprod
│ ├── hsm-prod
│ ├── iis-nonprod
│ ├── iis-prod
│ ├── it
│ ├── log-archive
│ ├── net
│ │ ├── images
│ │ │ └── net.png
│ │ ├── net.py
│ │ └── net_payload.json
│ ├── octal
│ ├── octane
│ ├── pa-nonprod
│ ├── pa-prod
│ ├── platform-systems
│ ├── pte
│ ├── rf-regression
│ ├── sec
│ ├── shared-svcs
│ ├── taf
│ └── yukon
└── requirements.txt
I have done research that basically says use sys
or use from path.to.folder import file
. However, when I run the python function it doesn't actually import the module.
# How to use this file to generate a diagram:
# $> python3 diagram.py
# https://diagrams.mingrammer.com/docs/getting-started/installation
import json
import traceback
from diagrams import Cluster, Diagram
from diagrams.aws.network import PrivateSubnet
from diagrams.aws.management import OrganizationsAccount
from functions.aws import get_vpc
graph_attr = {
"bgcolor": "transparent",
"margin": "-1, -2",
"size": "50,50!"
}
def get_network_payload():
json_data = get_vpc(json)
with open("net_payload.json", "w") as outfile:
outfile.write(json_data)
def impinjnetcluster():
with Diagram(filename="images/net", show=False, direction="LR", graph_attr=graph_attr):
with Cluster("impinjnet", graph_attr={"margin": "40", "fontsize": "18"}):
impinjnet = OrganizationsAccount("Impinj-Net")
with Cluster("vpc-###1", graph_attr={"margin": "40", "fontsize": "18"}):
net_subnet1 = PrivateSubnet("###")
with Cluster("vpc-###2", graph_attr={"margin": "40", "fontsize": "18"}):
net_subnet2 = PrivateSubnet("###")
impinjnet
try:
get_network_payload()
impinjnetcluster()
except:
traceback.print_exc()
Basically I am trying to do the following:
However, I can't even get the network payload to run it just does this:
Traceback (most recent call last):
File "/Users/rbarrett/Git/Impinj-di/aws_network_design/modules/net/net.py", line 10, in <module>
from functions.aws import functions, get_vpc
ModuleNotFoundError: No module named 'functions'
I put the __init__.py
as an empty file in every folder that I would need to import as seen from the tree and here is my python paths:
rbarrett@RBARRETT-MBP1 ~/Git/Impinj-di/aws_network_design/modules/net DI-4894-CreateWorkflows-4 ● ? python3 -c "import sys; print('\n'.join(sys.path))"
/usr/local/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python310.zip
/usr/local/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10
/usr/local/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload
/usr/local/lib/python3.10/site-packages
I have tried looking at several other questions similar to mine, but none of them have seemed to help me pull in the function I have defined in another file.
So it turns out I had to use sys and then had to use the following in my import statements:
import sys
sys.path.append('../../')
from functions.aws.boto3_helper import <function_name>
Where <function_name>
was a function defined within boto3_helper.py
, the biggest problem I had was to set sys.path.append('../../')
which is where the files I needed to access as a module are located at.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.