The right way to add unique libraries to heroku (django app) - django

I'm trying to deploy a django app to heroku.
I have several python libraries which are not on PyPi and so I can't just declare them in requirements.txt file
In local development I've used:
import sys
sys.path += [os.path.dirname(os.path.dirname(__file__))+"\\project-name\\lib"]
inside manage.py and it works fine there.
Obviously it doesn't work on heroku and I get import errors.
What is the recommended way to add libraries like that on heroku?
Thanks.

One way to do it is include the libraries in the repository itself, from which you can import them. That means simply moving the actual folder for each library into your main Django project folder.
- DjangoProject
- AppFolder1
- AppFolder2 ...
- python-library1
- python-library2
When the repository is pushed to Heroku your libraries will be pushed as part of the project.
From here, your imports of these libraries within a view/model etc within any app's folder would
import python-library1
from python-library2 import a_function, a_class
The reason why I suggest the directory structure above is that, most likely, you would not have go back and change any import codes.
If you have a large number of libraries and would like to keep the direcory structure simpler, simply create a folder with a name such as "importables" in the main DjangoProject folder and change the import statements to something such as...
from importables import python-library1
from importables.python-library2 import a_function, a_class
It's not exactly beautiful, but a quick way to get the job done. If you aren't sure where the libraries you'd like to include are located, there's a few ways to quickly see their location using Python (How do I find the location of Python module sources?).

Related

ModuleNotFoundError: No module named 'config.wsgi'

I'm trying to run a .py file and in the file I have this import
from config.wsgi import *
import os
from django.template.loader import get_template
from weasyprint import HTML, CSS
from config import settings
The whole project works, if I set runserver, the project starts without any problem, but this file does not work. The structure of the project is as follows
NombreDelProyecto
--app
---config
----__init__.py
----asgi.py
----settings.py
----wsgy.py
----db.py
---core
----general
----login
----user
----archivodetest.py
the case as I say the project works, but in the views of the applications that I have been doing to put imports I get in red underlined but as I say it works for example:
from core.general.forms import ActividadForm
That comes out in red, if I put in front of the core, app.core as follows
from app.core.general.forms import ActividadForm
it does not show red but the project does not work and I get the following error
from app.core.general.forms import ActividadForm
ModuleNotFoundError: No module named 'app'
I understand that it is the routes or something I did wrong from the beginning, please could someone help me.
Thank you very much.
I tried adding the route, changing the app's route in settings, but to no avail.
You've named the file wsgy.py but it needs to be wsgi.py.
Rename your file in config and retry.
To your question, I think its because you're missing the __init__.py file in the general app.
If you haven't already go one, you'll likely need to have add the same again in your core app too.
You probably manually created all of these files and structures I suspect, and if that's the case, please take a look at the documentation regarding creating new apps inside a django project.
If you go a bit further up the page, it will also tell you how to create the initial django project structure with a command.
Thank you very much for the answer, I managed to solve it after a lot of testing.
There are two ways, open the project again from the app folder (I had it open in the ProjectName folder).
Or as a second option in pyCharm on the left where the project folders are, I went to the app folder (which is the root) and right clicked and in the menu, Mark Directorie as - Sources root. Then my problem is fixed.
I had all the arcvhiso init.py, and where I put the wrong name wsgi.py is that I wrote it wrong here but in the project it was right.
Thank you very much for the help.

How To Change A Third Party App's Models.py Due To Invalid Import

I'm trying to add django-messages to my app but I'm getting this error when I add it to my installed apps
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding' (C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\encoding.py)
I've looked up this error and apparently it's caused by this line in Django-Messages models.py
from django.utils.encoding import python_2_unicode_compatible
To fix it I should change it to
from django.utils.six import python_2_unicode_compatible
As this is a third party app, how should I change this line?
Should I copy the file structure into my own project (creating a django-messages folder among my apps then a models.py file inside it) and copy/paste the entire models.py into there and then change the line? This seems wrong, but I don't know how else to fix it.
Or does the fact that it's using an outdated import signify that the app isn't maintained and therefore shouldn't be used at all?
Thank you.
I can suggest two options. First, you can clone it on GitHub (i.e. download all the files) and then paste this into your project folder (along with your other apps). And then you can make any changes you wish. Of course it remains to be seen whether it will work then, as there might be other issues. It seems to be compatible with Django 1.11 - 2.2. But I would think that it's a better option than doing this all yourself. Unless you can find a different app that suits your needs.
Second, assuming you are using a virtual environment, you can navigate to where all your packages are installed. I'm not sure where this will be located for you, but it should be something like envs\<name of env>\Lib\site-packages. And there you can make any changes you wish - just search for the relevant file.
Hope this helps.

python import module from a parent directory

I have the following structure and I am using the below to import functions
( i.e basically importing a module from a parent directory)
from demo import sayHello
It works but in Pycharm it says unresolved reference. Should I continue or should I use the relative path like below, then I get no warnings
from ..demo import sayHello
Let me know the correct way of importing
app
demo.py
__init__.py
**Controllers**
AccountController.py
__init__.py
Yes, in python it's best to use the relative path within the project. (Based on the question, I can't tell where you're trying to use the imported function, but I'm assuming it's within the same project)
Packages like os, sys etc are built within your Python executable, so those are available globally. Anything else needs to be relative.

django dev on mac having to explicitly name full path

After a long time away from an app i wrote in Django and didn't complete, I've come back to it on a new Mac.
I'm struggling to get the code to refer to the apps and the files within them without the explicit path. For instance:
from myproject.app.file import object
Whereas I remember not having to use myproject every time.
Is this something that has changed? I seem to remember being about to add to the path in manage.py which is called every time you run the dev server, but this hasn't worked this time.
sys.path.append /path/to/myproject
Should that fix the issue I'm having?
I started with a simple answer and it grew into more details on how to add subdirectories of your project to python path. Maybe a bit off-topic, but it could be useful to you so I'm pushing the post button anyway.
I usually have a bunch of small re-usable apps of mine I keep inside my project tree, because I don't want them to grow into independent modules. My projet tree will look like this:
manage.py
myproject/apps
myproject/libs
myproject/settings
...
Still, by default, Django only adds the project root to python path. Yet it makes no sense in my opinion to have apps load modules with full path:
from myproject.apps.author.models import Author
from myproject.libs.rest_filters import filters
That's both way too verbose, and it breaks reusability as I only use absolute imports. Not to mention if I someday build an actual python package out of some of the libs, it will break.
So, I took the following steps. I added the relevant folders to the path:
# in manage.py
root = os.path.dirname(__file__)
sys.path.append(os.path.realpath(os.path.join(root, 'myproject', 'apps')))
sys.path.append(os.path.realpath(os.path.join(root, 'myproject', 'libs')))
But you must ensure those packages cannot be loaded from the root of the project, or you will have odd issues as python would load another copy of the module. For instance, isinstance(libs.foo.bar(), myproject.libs.foo.bar) == False
It's not hard though : just remove __init__.py from the folders you add to the path. That will make sure they cannot be descended into from the project.
Also, Django's discover runner will not descend into those paths unless you specify them manually. That may be fine with you (if every module has its own test suite). Or you can extend the runner, so it knows about this: sample code.

What's the common way to layout a Django app with Buildout/djangorecipe?

I have a Django app that I've set up using Buildout laid out like so:
/workspace
/bin
/src
/myproject
settings.py
/myapp
views.py
...
bootstrap.py
buildout.cfg
setup.py
The issue is that I'd like both myproject.settings and myapp on the python path. I need the myproject.settings on the path so djangorecipe can import it. And I'd like myapp on the path so that I don't have to write import myproject.myapp all the time.
For now I've got both /workspace/src and /workspace/src/myproject in the Python path, but this feels like a hack and practically makes me worried if there might be situations where import some_module might have confusing resolution patterns because I have two directories that are parent-child to each other.
So questions are:
Is there an accepted way to lay this out?
Is it actually bad to have a directory and one of its sub-directories in the path?
There is no problem, on import some_module importer will search in each folder specified at sys.path for some_module/__init__.py and some_module.py. Same for import myproject.some_module, it will search for myproject module, then it will try to find in it some_module with same algorithm.
I'm using the same project structure.
If your buildout.cfg includes develop = . and whatever egg your setup.py defines is included as a dependency for your buildout/parts then whatever code path your setup.py defines will be automatically added to sys.path. Just make sure your setup.py includes src as a code directory. One way to do this is with:
setup(name=...
...
packages=find_packages('src'),
package_dir = {'':'src'},
...