import a file in a folder python - python

I want to import a file from a folder in my replit discord.py project. I’m using the same project to do a website , with flask. This is the structure
project ——|—— main.py
|
|—— static ——|—— Templates —— index.html
|
|—— flask_.py
I’m using a function who keep alive the bot using uptime robot. Before, I didn’t need a website. The flask app just print in a website output I’m alive. But now, I want to create a full website with flask using HTML. The HTML isn’t a problem. The problem is that, in the structure, you can see that flask_.py is in the folder static and the main.py file isn’t. Because the function to keep alive the bot is in flask_.py, I can’t import it like that from flask_ import keep_alive. I don’t know how to import it. Can you help me?

You need to start from the root directory of the project:
from static.flask_ import keep_alive
But why do you have have dynamic code in a folder named static? I suggest moving the flask_.py folder to the root of the project then you can use the import you already have.
Side note: in python, we typically use underscores to separate words in names. Ending a filename with an underscore is unusual.

Related

flask: why cant I import from .views or .models in __init__.py?

When I try to import views from .views, it gives me the error saying 'attempted relative import with no known parent package.
It does the same thing when I try to import Users from .models
here is the code
from .views import views #NOT WORKING
app.register_blueprint(views, url_prefix='/')
from .models import Users #not working either
createdatabase(app)
I am a beginner programmer and is trying to make a website using flask and python. I have searched but don't know how to solve this issue. If you do know how to please help me. Thanks
Welcome to Stack Overflow :)
I usually put all my code files into one parent folder (I usually call it src) and then make subfolders from there if needed. I think python doesn't allow relative imports when it is directly in the root folder (please correct me if I'm wrong). Also keep in mind, that all your folders that contain python scripts (that you want to import anywhere else in your program) need to have a __init__.py file. It can be empty and it's just a somewhat magical way to tell python in which folders to look for scripts.

How to integrate a python file onto a Django project?

I am trying to include a machine learning component in my Django project. I have the method written in python. How do I get to make it work on the website using Django. Can I simply drag the ".py" file into the file structure and call it from an HTML page? I am new to Django any help would be greatly appreciated.
Yes you can directly copy file into your Django Directory structure. Let's say you have a file test.py and a function written in it as def print(). And you have copied the file in app directory. Then you can call it in views.py as from app.test import print. print function will be imported in views.py and you can use it to serve in html as you want.

ImportError: cannot import name 'Page'

I have written a game in an Otree Project, but every time i want to clean my database or start the server, this message appears. It only happens with my game, but not when an example game is using that command.
Look in your app for the folder _builtin. There should be a file __init__.py that has the same contents as every other app's _builtin/__init__.py. If not, copy it from the other app.

Using a class from another folder in the current code

My apologies for a seemingly easy question, I'm new to using classes in Python.
I am using Pycharm and my folder structure looks as follows:
The folder constant-contact-python-wrapper has a few classes defined under __init.py__ and restful_lib.py (I got this library from github). I would like to use these classes in the file Trial.py contained in ConstantContact folder. I am using the following code but it is not able to import the class.
import sys
sys.path.append('C:\\Users\\psinghal\\PycharmProjects\\ConstantContact\\constant-contact-python-wrapper')
import constant-contact-python-wrapper
API_KEY = "KEY" #not a valid key
mConnection = CTCTConnection(API_KEY, "joe", "password123")
Would someone please be able to point me to the right direction?
Part of the problem that you're trying to rectify is that you have two libraries that are together in the same scope, even though it doesn't look they necessarily need to be.
The simplest solution would be to simple put constant-contact-python-wrapper in the ConstantContact folder under a new folder for code you will be importing that you yourself did not write. This way your project is organized for this instance and for future instances where you import code that is from another library
Ideally the folder structure would be:
ConstantContact
|___ ConstantContact
|____ExternalLibraries #(or some name similar if you plan on using different libraries)
|___constant-contact-python-wrapper
Using the above model, you now have an organized hierarchy to accommodate imported code very easily.
To facilitate easy importing you would additionally setup the following:
1.Create init.py file in ExternalLibraries. The contents would be the following:
from constant-contact-python-wrapper import #The class or function you want to use
This will facilitate imports and can be extended for future libraries you choose to use.
You can then use import statements in your code written in the ConstantContact folder :
from ExternalLibraries import #The class or function you chose above
if you have multiple classes you would like to import, you can seperate them in your import statement with commas. For example:
from Example import foo,bar,baz
Since the init.py file in ExternalLibraries is import all functions/classes directly, you can use them now without even having to use dot syntax (ie. library.func).
Sources and further reading:
"all and import *" Can someone explain __all__ in Python?
"Python Project Skeleton" http://learnpythonthehardway.org/book/ex46.html
"Modules" http://docs.python-guide.org/en/latest/writing/structure/#modules
constant-contact-python-wrapper and ConstantContact are unrelated packages for python. Create a __init__.py in the same directory as manage.py and it should work as expected.

writing my first Trac macro

Ok, I've looked all over, and I think I'm doing this right, but I'm not getting any results. Is there anyone out there who's written Trac macros that can guide me through the first steps? Here's what I've written:
from trac.wiki.macros import WikiMacroBase
from genshi.builder import tag
class MyMacro(WikiMacroBase):
"""Proof of concept"""
revision = "$Rev$"
url = "$URL$"
def expand_macro(self, formatter, name, args):
return tag.b("Hello world.")
I've saved it as a .py file and put it in my Trac project's /plugins directory. Do I need to restart apache? Am I correct in expecting [[MyMacro]] to output a Hello world. on the page?
When creating macros using that format, Trac expects your class to be named "<name>Macro". For example, if you wanted a macro named JustASample, you would name the class JustASampleMacro. Since you named your class MyMacro, Trac thinks that you want your macro to be named My. Try using [[My]] on a wiki page and see if you get the output you're expecting.
After you copy the file into the plugins directory, you will indeed want to restart the web server. Before doing so, delete any .pyc files that were created for your plugin. Also, ensure that the file is readable by the account under which the web server runs.

Categories

Resources