How To Have Python Import A Module From Another Directory - python

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:
Get the network payload
load the network payload into a dynamic block
generate image
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.

Related

Is there a way to remove the side navigation bar containing file names in the latest version of streamlit?

I recently upgraded streamlit to version 1.10.0. After upgradation I ran streamlit only to find that there is a side navigation bar displaying python file names present in my directory. I have attached images for the same.
import streamlit as st
import pandas as pd
from services.multiapp import MultiApp
from functools import partial
from pages import show, analysis, help, custom, download, three_d
def refresh():
pass
file = st.file_uploader('Upload CSV file',
type='csv', help="Format")
if file is not None:
# verification
# Prediction
st.button("Refresh", on_click=refresh)
data = pd.read_csv(file)
app = MultiApp()
app.add_app('Show data', partial(show.app, data))
app.add_app('Analysis', analysis.app)
app.add_app('Custom Plots', custom.app)
app.add_app('3-D Plots', partial(three_d.app, data))
app.add_app('Download', partial(download.app, data, file.name))
app.add_app('Help', help.app)
app.run()
Please help me to remove the side navigation bar.
My current working directory has the following structure
├── app.py
├── pages
│ ├── analysis.py
│ ├── custom.py
│ ├── download.py
│ ├── help.py
│ ├── __pycache__
│ │ ├── analysis.cpython-38.pyc
│ │ ├── analysis.cpython-39.pyc
│ │ ├── custom.cpython-38.pyc
│ │ ├── custom.cpython-39.pyc
│ │ ├── download.cpython-38.pyc
│ │ ├── download.cpython-39.pyc
│ │ ├── get_df.cpython-38.pyc
│ │ ├── help.cpython-38.pyc
│ │ ├── help.cpython-39.pyc
│ │ ├── iplots.cpython-38.pyc
│ │ ├── plot.cpython-38.pyc
│ │ ├── show.cpython-38.pyc
│ │ ├── show.cpython-39.pyc
│ │ ├── show_d.cpython-38.pyc
│ │ ├── three_d.cpython-38.pyc
│ │ └── three_d.cpython-39.pyc
│ ├── show.py
│ └── three_d.py
├── __pycache__
│ ├── multiapp.cpython-38.pyc
│ └── plots.cpython-38.pyc
├── README.md
├── services
│ ├── multiapp.py
│ ├── __pycache__
│ │ ├── multiapp.cpython-38.pyc
│ │ └── multiapp.cpython-39.pyc
│ └── services.py
├── Templates
│ ├── links.txt
│ ├── multi-page-app-main
│ │ ├── app.py
│ │ ├── apps
│ │ │ ├── data.py
│ │ │ ├── home.py
│ │ │ ├── model.py
│ │ │ └── __pycache__
│ │ │ ├── data.cpython-38.pyc
│ │ │ ├── home.cpython-38.pyc
│ │ │ └── model.cpython-38.pyc
│ │ ├── multiapp.py
│ │ ├── __pycache__
│ │ │ └── multiapp.cpython-38.pyc
│ │ ├── README.md
│ │ └── requirements.txt
│ ├── streamlit-dashboard-template-main
│ │ ├── app.py
│ │ ├── dashboard.py
│ │ ├── README.md
│ │ ├── stdashdark.png
│ │ └── stdashlight.png
│ ├── streamlit-geospatial-master
│ │ ├── app.py
│ │ ├── apps
│ │ │ ├── basemaps.py
│ │ │ ├── census.py
│ │ │ ├── cesium.py
│ │ │ ├── deck.py
│ │ │ ├── device_loc.py
│ │ │ ├── gee_datasets.py
│ │ │ ├── gee.py
│ │ │ ├── heatmap.py
│ │ │ ├── home.py
│ │ │ ├── housing.py
│ │ │ ├── plotly_maps.py
│ │ │ ├── raster.py
│ │ │ ├── rois.py
│ │ │ ├── timelapse.py
│ │ │ ├── vector.py
│ │ │ ├── wms.py
│ │ │ └── xy.py
│ │ ├── data
│ │ │ ├── cog_files.txt
│ │ │ ├── html
│ │ │ │ └── sfo_buildings.html
│ │ │ ├── realtor_data_dict.csv
│ │ │ ├── us_counties.geojson
│ │ │ ├── us_metro_areas.geojson
│ │ │ ├── us_nation.geojson
│ │ │ └── us_states.geojson
│ │ ├── environment-bk.yml
│ │ ├── index.html
│ │ ├── LICENSE
│ │ ├── multiapp.py
│ │ ├── packages.txt
│ │ ├── Procfile
│ │ ├── README.md
│ │ ├── requirements.txt
│ │ └── setup.sh
│ ├── Streamlit-master
│ │ ├── app.py
│ │ ├── data
│ │ │ ├── demo.wav
│ │ │ ├── Salary_Data.csv
│ │ │ ├── sal.jpg
│ │ │ ├── snippets
│ │ │ └── virtual.mp4
│ │ ├── data.py
│ │ ├── demo.py
│ │ ├── layout.py
│ │ ├── plots.py
│ │ ├── Procfile
│ │ ├── README.md
│ │ ├── requirements.txt
│ │ ├── sidebar.py
│ │ └── widget.py
│ └── streamlit template
│ ├── JC-202103-citibike-tripdata.csv
│ ├── logo.png
│ ├── requirements.txt
│ └── streamlit_template.py
├── test.csv
└── test.py
Streamlit sidebar picks everything up that is located in the "pages" folder (see here: Blog Streamlit)
You could either rename your pages folder to something else or if you like the side bar, you can just collapse it initially:
import streamlit as st
st.set_page_config(initial_sidebar_state="collapsed")
That way the sidebar will be closed on start but can still be opened if needed.
A little late to this question, but I added the following snippet to the pages that I wanted to hide the lists on.
no_sidebar_style = """
<style>
div[data-testid="stSidebarNav"] {display: none;}
</style>
"""
st.markdown(no_sidebar_style, unsafe_allow_html=True)
This got rid of the Streamlit generated page components on the sidebar and let me keep all my other components.
Add this to all the pages you want to hide the page list from, but there should not be any ‘ul’ in your page except for the page list.
st.markdown("<style> ul {display: none;} </style>", unsafe_allow_html=True)

Cannot import model from .models file to nested app sub_directory

I suspect that I am not understanding something about django. I am trying to test the file scrape.py which calls an api and parses then writes the response to the model CMC in the models.py using sqlalchemy. I am trying to test it to see if the file itself will run but (here's where I think I'm going wrong) I am pressing the play button while having the scrape.py file pulled up in vscode. I suspect there is something here that Django won't allow but am not familiar enough with django to know if that is the case. It is throwing a ModuleNotFound error as described below but I suspect that it's doing that because I am calling it because when I type the dot after apis after import at the top of the file it shows a list of all the drop-down files in my apis app.
Here is my project tree:
(base) justinbenfit#MacBook-Pro-3 cds_website % tree
.
├── api
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── admin.cpython-38.pyc
│ │ ├── apps.cpython-38.pyc
│ │ ├── models.cpython-38.pyc
│ │ ├── serializers.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── views.cpython-38.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── main.py
│ ├── management
│ │ ├── __init__.py
│ │ ├── commands
│ │ │ ├── __init__.py
│ │ │ ├── __pycache__
│ │ │ │ └── private.cpython-39.pyc
│ │ │ ├── private.py
│ │ │ └── scrape.py
│ │ └── test.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-38.pyc
│ │ └── __init__.cpython-38.pyc
│ ├── models.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── cds_website
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── requirements.txt
api is an app in a greater project called cds_website. The settings.py file in cds_website project directory contains the following installed apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api.apps.ApiConfig',
'rest_framework',
'environ'
]
and my apps.py file in the api app contains the following class:
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'
CMC is a model in my models.py file. I am trying to import it into scrape.py. I have tried:
from ...models import CMC
from api.models import CMC
from ... import CMC
first one throws: ImportError: attempted relative import with no known parent package second one throws: ModuleNotFoundError: No module named 'api' third one throws: ImportError: attempted relative import with no known parent package
Here is a link to the code repo: https://github.com/Justinbenfit23/cds_website
Everything I have read indicates that at least one of these should work. Any direction appreciated!
I'm teste your project with from api.models import CMC and it's worked
The easiest way is to create ecrape_manage.py in cds_website-master
folder, content of ecrape_manage.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cds_website.settings")
import django
django.setup()
exec(open("api/management/commands/scrape.py").read())
and just run with python ecrape_manage.py

Relative import error even after adding __init__

This is the directory structure in a project I am working on:
.
├── analyx
│ ├── database.py
│ ├── db.sqlite3
│ ├── endpoints.py
│ ├── errors.py
│ ├── flow.py
│ ├── import_test_copy.py
│ ├── __init__.py
│ ├── metrics.py
│ ├── mkdocs.yml
│ ├── models.py
│ ├── plugins.py
│ ├── __pycache__
│ │ ├── endpoints.cpython-38.pyc
│ │ ├── endpoints.cpython-39.pyc
│ │ ├── errors.cpython-38.pyc
│ │ ├── errors.cpython-39.pyc
│ │ ├── flow.cpython-38.pyc
│ │ ├── flow.cpython-39.pyc
│ │ ├── __init__.cpython-38.pyc
│ │ ├── __init__.cpython-39.pyc
│ │ ├── metrics.cpython-38.pyc
│ │ ├── metrics.cpython-39.pyc
│ │ ├── plugins.cpython-38.pyc
│ │ ├── plugins.cpython-39.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── settings.cpython-39.pyc
│ │ └── visualize.cpython-38.pyc
│ ├── settings.py
│ └── visualize.py
├── docs2
│ ├── dev_docs
│ │ └── graph.md
│ ├── flow.md
│ └── index.md
├── endpoints2.py
├── flow_test_copy.py
├── __init__.py
├── metrics2.py
├── mkdocs.yml
├── plugins
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ └── __init__.cpython-39.pyc
│ └── test_plugin
│ ├── __init__.py
│ ├── models.py
│ ├── __pycache__
│ │ ├── __init__.cpython-39.pyc
│ │ ├── models.py
│ │ └── test_plugin.cpython-38.pyc
│ └── test_plugin.py
├── __pycache__
│ ├── endpoints.cpython-39.pyc
│ └── metrics.cpython-39.pyc
├── README.md
├── requirements.txt
├── setup.py
└── tests
├── flow_test.py
├── __init__.py
└── test_dummy.py
As evident, I have added __init__.py to all the directories, so that I can import them as packages and sub packages.
The content of the file plugins/test_plugin/test_plugin.py starts as follows:
from ...analyx.plugins import Plugin
It still gives the error:
ImportError: attempted relative import with no known parent package
How do I fix it? I am not very acquainted with the nuances of the Python Packaging system, so any help will be appreciated.
Thanks in advance!

Can you help me to resolve the error I am getting after running Stringtie prepDE.py?

When I run prepDE.py job exit with the following error. I am using StringTie/2.1.4 with Python/2.7.18.
Following is the command I was running (inside ADA cluster)
#Loading New modules
module load StringTie/2.1.4-GCC-9.3.0
module load Python/2.7.18-GCCcore-9.3.0
#converting data to DESEQ2 compatible format
prepDE.py
Traceback (most recent call last): File
"/sw/eb/software/StringTie/2.1.4-GCC-9.3.0/bin/prepDE.py", line 58, in
samples = [(i,glob.iglob(os.path.join(opts.input,i,"*.gtf")).next())
for i in next(os.walk(opts.input))1 if re.search(opts.pattern,i)]
StopIteration
I have a directory structure as follows Directory structure
├── ballgown
│ ├── PVX_2d_1
│ │ ├── e2t.ctab
│ │ ├── e_data.ctab
│ │ ├── i2t.ctab
│ │ ├── i_data.ctab
│ │ ├── PVX_2d_1.gtf
│ │ └── t_data.ctab
│ ├── PVX_2d_2
│ │ ├── e2t.ctab
│ │ ├── e_data.ctab
│ │ ├── i2t.ctab
│ │ ├── i_data.ctab
│ │ ├── PVX_2d_2.gtf
│ │ └── t_data.ctab
│ ├── PVX_2d_3
│ │ ├── e2t.ctab
│ │ ├── e_data.ctab
│ │ ├── i2t.ctab
│ │ ├── i_data.ctab
│ │ ├── PVX_2d_3.gtf
│ │ └── t_data.ctab
Can you help me in this regard?
Thanks in advance.
This is an educated guess based on the error and the documentation linked:
-i INPUT, --input=INPUT, --in=INPUT
– a folder containing all sample sub-directories, or a text file with sample ID and path to its GTF file on each line [default: . ]
Try invoking the script with
prepDE.py -i ballgown
since the data subfolders are in a ballgown folder.

Flask blueprint doesn't work (Python2)

Setup:
I am trying to clone git project(all the code here) to locally deploy it and make it work for academic purpose.
So far I have only had experience with Flask under Python 3, but this project is written on Flask using Python 2. After setting up virtualenv, installing all requirements I can successfully run(python server.py) it and navigate to index page.
Problem: Whenever i try to reach pages like "localhost:5000/login" I can only see 404 error "Not Found". Looking through the code I see that it is importing blueprints which contain routes to ".../login" view, but it doesn't get to a point of showing it.
Project structure looks like this:
.
├── API Documentation.md
├── app.py
├── app.pyc
├── data
│ ├── samples
│ │ ├── categories.txt
│ │ ├── domains.txt
│ │ ├── names.txt
│ │ ├── products.txt
│ │ └── surnames.txt
│ └── sql
│ └── schema-00.sql
├── Makefile
├── README.md
├── requirements.txt
├── server.py
├── sfec
│ ├── api
│ │ ├── base.py
│ │ ├── base.pyc
│ │ ├── decorators.py
│ │ ├── decorators.pyc
│ │ ├── fields.py
│ │ ├── fields.pyc
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── order.py
│ │ ├── order.pyc
│ │ ├── product.py
│ │ ├── product.pyc
│ │ ├── user.py
│ │ └── user.pyc
│ ├── config.py
│ ├── config.pyc
│ ├── controllers
│ │ ├── decorators.py
│ │ ├── decorators.pyc
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── order.py
│ │ ├── order.pyc
│ │ ├── user.py
│ │ └── user.pyc
│ ├── database
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── runtime.py
│ │ ├── runtime.pyc
│ │ ├── settings.py
│ │ └── settings.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ └── models
│ ├── base.py
│ ├── base.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── order.py
│ ├── order.pyc
│ ├── product.py
│ ├── product.pyc
│ ├── user.py
│ ├── user.pyc
│ ├── views.py
│ └── views.pyc
├── sfecadmin.py
├── templates
│ └── index.html
├── tests
│ ├── __init__.py
│ └── user_test.py
├── tree.txt
└── uml_diagram.png
10 directories, 63 files
And that's how blue print is called inside of executable server.py(pieces of code):
from sfec.api.user import register_user_resource
from sfec.controllers.user import user_api
app.register_blueprint(user_api, url_prefix='/api')
And user.py file (./sfec/controllers/user.py) contain(pieces of code):
user_api = Blueprint('user_api', __name__)
#user_api.route('/login', methods=['POST'])
def login():
print "login page"
"""Log the user in."""
store = get_default_store()
user = User.authenticate(store, request.form['email'],request.form['password'])
if user:
session['user'] = user.id
return user.json()
abort(403)
The 'login' route is create, so I would expect after navigating to 'localhost:500/login' to receive something back, at least an error 403 or something, but not 404(not found) error.
Can you please help me to understand what am I missing?
I would highly appreciate any help.

Categories

Resources