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!
Related
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.
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)
I am building a personal portfolio website with Django which I'm hoping to host on Heroku. I am aware of the platform's ephemeral storage problem so all of the images are served from an s3 bucket. After deploying the app though and running the python manage.py migrate from the dyno and check the postgresql database on the dashboard I can see rows and columns created but they're not populated. Therefore, there are no models stored in this database. I'm not .gitignore-ing the db.sqlite3. I'm also using a virtual environment.
Ih short here's the output of the tree command from the root folder:
├── Procfile
├── db.sqlite3
├── manage.py
├── media
│ └── images
│ ├── angular.png
│ ├── bash.png
│ ├── c.png
│ ├── calibration.png
│ ├── commerce_img.png
│ ├── css3.png
│ ├── django.png
│ ├── git.png
│ ├── html.png
│ ├── image-processing-api.png
│ ├── js-logo.png
│ ├── mail.png
│ ├── my_store.png
│ ├── network.png
│ ├── nodejs.png
│ ├── programmer.svg
│ ├── python.png
│ ├── rest_with_node.png
│ ├── sql.png
│ ├── typescript.png
│ └── wiki_image.png
├── portfolio
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-39.pyc
│ │ ├── settings.cpython-39.pyc
│ │ ├── urls.cpython-39.pyc
│ │ └── wsgi.cpython-39.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── requirements.txt
├── staticfiles
└── webdev_portfolio
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-39.pyc
│ ├── admin.cpython-39.pyc
│ ├── apps.cpython-39.pyc
│ ├── models.cpython-39.pyc
│ ├── tech_stack.cpython-39.pyc
│ ├── urls.cpython-39.pyc
│ └── views.cpython-39.pyc
├── admin.py
├── apps.py
├── models.py
├── static
│ └── webdev_portfolio
│ └── styles.css
├── templates
│ └── webdev_portfolio
│ ├── index.html
│ ├── layout.html
│ ├── project.html
│ └── projects.html
├── tests.py
├── urls.py
└── views.py
Finally here's my settings.py file in case I missed something:
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)
Am I missing something?
Thanks.
So you are using psql on Heroku trying to populate it with db.sqlite3 file? If that is your intention that is not going to work. Both databases are different, in fact, to populate a newly created PSQL you would need either a fixture or backup.
Check the Heroku documentation
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.
I´m trying to deploy my django 1.6.4 project on pythonAnywhere using python 2.7
I already configured a virtual enviroment and the wsgi file according to the guidelines on the website. But I´m getting a 404 when I check the site. The error lol tells me this:
ImportError: Could not import settings 'tango_with_django_project.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named tango_with_django_project.settings
Here´s my wsgi:
# +++++++++++ DJANGO +++++++++++
# TURN ON THE VIRTUAL ENVIRONMENT FOR YOUR APPLICATION
activate_this = '/home/pjestrada/.virtualenvs/rango/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/pjestrada/mysite/settings.py'
path = '/home/pjestrada/rango/tango_with_django_project'
if path not in sys.path:
sys.path.append(path)
os.chdir(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'tango_with_django_project.settings'
#
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
My tree:
├── Dropbox
│ ├── README.txt
│ └── __init__.py
├── README.txt
└── rango
├── LICENSE
├── README.md
└── tango_with_django_project
├── manage.py
├── media
│ ├── profile_images
│ │ └── 10411981_634016890008979_1609187547738555774_n.jpg
│ └── rango2.jpg
├── populate_rango.py
├── rango
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── admin.py
│ ├── admin.pyc
│ ├── bing_search.py
│ ├── bing_search.pyc
│ ├── forms.py
│ ├── forms.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── static
│ ├── about.jpg
│ ├── css
│ │ ├── bootstrap-fluid-adj.css
│ │ ├── bootstrap-responsive.css
│ │ ├── bootstrap-responsive.min.css
│ │ ├── bootstrap.css
│ │ └── bootstrap.min.css
│ ├── img
│ │ ├── glyphicons-halflings-white.png
│ │ └── glyphicons-halflings.png
│ ├── js
│ │ ├── bootstrap.js
│ │ ├── bootstrap.min.js
│ │ ├── jquery-2.1.1.js
│ │ └── rango-ajax.js
│ └── rango.jpg
├── tango_with_django_project
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ └── wsgi.py
└── templates
└── rango
├── about.html
├── add_category.html
├── add_page.html
├── base.html
├── category.html
├── category_list.html
├── index.html
├── login.html
├── page_list.html
├── profile.html
├── register.html
├── restricted.html
└── search.html
Try changing value of path.
path = '/home/pjestrada/rango'
That path is your project directory. It worked for me.