I've been trying to add Markdown2 library to my GAE app but it keeps throwing an ImportError. This is what I have:
sys.path.append(os.path.join(os.path.dirname(__file__), "lib\markdown2")
if I now do
html = markdown2.markdown("*boo*") #throws an error: markdown2 not defined
You need to add the lib directory to your path, not the markdown directory - markdown is a package, defined by the directory it's in.
This should work:
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
I use markdown2 with a simple import :
import lib.markdown
I don't think that's one of the standard libraries.
You'll need to include a copy of it inside your app folder - otherwise it won't be available when you deploy.
You shouldn't need to modify the path after that just import it with the correct path relative to your app's root folder.
for example:
if your app is at
/home/myapp
you might want to copy your markdown2 to
/home/myapp/lib/markdown2
in which case you'd use "import lib.markdown2".
Related
I am running a Flask server in Docker and cannot import create_app from app.py into my integration tests. I've tried a variety of naming approaches but Python is unable to find app.py.
The directory structure is as follows
/server
/test/integration/test.py
app.py
test.py has this import
from app import create_app
I tried relative imports as well but there was a "parent" error. I then played around with empty __init__.py files in an attempt to use relative imports. That didn't work. I am not sure why this is so involved to do really. What is the solution for this?
In test.py add:
# some_file.py
import sys
# caution: path[0] is reserved for script path (or '' in REPL)
sys.path.insert(1, '/path/to/app/folder')
import app
from app import create_app
This add the directory of the source file to the system so Python will know to go there when searching the things to import. This is true when both files are local on your computer. If there is a server you need to see how you modify the in-server Python environment to know the folder of the app.py file.
See: Importing files from different folder
You can try with relative imports
from ..app import create_app
I have a project with structure like on this picture.
Folders structure
Where 'backend' folder is Django project folder.
I need to import module from another folder 'main' inside Django app file, i.e. import main.Text_Generator in backend.app.views file.
I tried: from ...main.Text_Generator import *. This raise an error while running a server: "attempted relative import beyond top-level package"
And from main.Text_Generator import *, also error "No module named 'main'"
What is the correct way to do such import?
Add this:
import sys
sys.path.append("..")
And then you should be able to get it with:
from main.Text_Generator import *
You're using a module outside your Django project. I would recommend moving the folder inside your project directory [or app directory] rather than messing with your PATH. If you move main inside backend your existing stuff will work.
I want to write my own python package inside django's app. It looks like that: I have secondary app. It totally works except one thing. Someday I'd decided to make my app more structured. And there where problems started. First of all, I've added python package(with simple init.py inside directory) to the app. Then I've added second package inside that package. And when I try to run django.setup() inside packages' files, I'm getting this error: ModuleNotFoundError: No module named '<primary settings container module name goes here>'
How to make my custom package's functions working properly?
The problem is that settings module isn't "viewable" from your package, e.g. you should add the location of main directory to PATH variable, which could be done like this:
from sys import path
from pathlib import Path
path.append(str(Path(__file__).resolve().parent))
Or you can simply add it permanently to system PATH.
In my home directory, I have a folder called local. In it, there are files called __init__.py and local_settings.py. My django app is in a completely different directory. When the app is NOT running in DEBUG mode, I want it to load the local_settings.py file. How can this be acheived? I read the below:
Import a module from a relative path
Importing files from different folder in Python
http://docs.python.org/tutorial/modules.html
Basically, those tutorials are allowing to import from another directory, but what about a completely different working tree? I don't want to keep doing .., .., .. etc. Is there a way to goto the home directory?
I tried the following code:
import os, sys
os.chdir(os.path.join(os.getenv("HOME"), 'local'))
from local_settings import *
But i keep seeing errors in my apache error.log for it...
os.chdir just affects the current working directory, which has nothing whatsoever to do with where Python imports modules from.
What you need to do is to add the the local directory to the Pythonpath. You can either do this from the shell by modifying PYTHONPATH, or from inside Python by modifying sys.path:
import sys
import os
sys.path.append(os.path.expanduser("~/local"))
import local_settings
In response to your concerns about source control, you can just set the source control to ignore that file, and/or have a symlink installed as part of your deploy script to link the file on the os into another. I do both , though not in django. but it's a trivial task.
your deploy layout could look like this:
/current ( symlink to /releases/v3 )
/settings/local_settings.py
/releases/v1
/releases/v2
/releases/v3
and a task runs as part of your deploy:
cd /current
ln -s /settings/local_settings.py local_settings.py
if you're deploying with fab or capistrano, it's a few lines of configuration. i'm sure you could do this with puppet/chef simply too.
I have the following directory structure for my GAE project:
project:
library:
lib1.py
lib2/x.py
lib2/y.py
apps:
app1/app1.py
app2/app2.py
app2/async.py
how do I make the library folder visible to any app that will ever be created in the apps dir and its subdirs?
Alternatively it's possible to add the library directory to the sys.path
Create a __init__.py inside the library folder.
import os
import sys
def add_lib_path():
lib_directory = os.path.dirname(os.path.abspath(__file__))
if lib_directory not in sys.path:
sys.path.insert(0, lib_directory)
In every file where you import libraries from the library folder add this code before the import statements:
from lib import add_lib_path
add_lib_path()
In this case all your imported libraries will behave as expected.
PYTHONPATH specifies a series of folders to start searches for imported modules.
GAE adds the folder that contains app.yaml to your PYTHONPATH.
So assuming that app.yaml is in the root of that structure (ie the folder that contains "library" and "apps") then any of your apps can import relative to there...
from library import lib1
from library/lib2 import x