Import views file from app into project urls unresolved - django

I'm trying to import grader/grader/core/views.py into grader/grader/urls.py but get unresolved reference. I have tried several import statements but haven't found any that get's resolved. What is the correct way to import?

Right click on the folder that contains manage.py, then scroll down to
'mark directory as', then click on Sources Root. That solves the problem in Pycharm IDE.

It looks like grader/grader is on the Python path.
Therefore you would import views.py from grader/grader/cores with:
from core import views

The problem was solved by changing the Python interpreter from 2.7 to 3.5

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.

webapp2 - how do I include library

I am testing Google Cloud and first I wanted to develop something on my PC before I use in on google cloud.
I am using APACHE and configured it in that way, that when I am going to the page localhost/wsgi_app I see my page which physically is stored in folder /svc/http/webapp2. File wsgi_app.py which contains my app is stored in subfolder webapp2. All works fine. I provide this information just in case it might play any role.
The issue I have is with import from library.
I did it on Django and now try to move it to webapp2.
The first lines of my program look like this:
import webapp2
import MySQLdb
import json
I have file called test.py which contains some classess and funtions.
it is in the same folder as wsgi_app.py.
I want to include it, however this seems not to work:
import webapp2
import MySQLdb
import json
from test import *
my test.py contains definition of the class 'Quote', but when I call the page I see error
NameError: global name 'Quote' is not defined
When I put the inside of the file test.py in the file wsgi_app.py all works fine.
My goal is to separate the code into several files.
Any idea why :
from test import *
does not work ?
It worked on any other program I wrote, so why not here?
issue solved.
issue is not related to weapp2 but to WSGI and the path where python search for files.
Most simple solution is to add something like this ;
execfile("/srv/http/test.py")
it will import the file.
other than this here is quite good article
http://blog.dscpl.com.au/2014/09/python-module-search-path-and-modwsgi.html

Python: Can't import a function from another.py file

I have a file named handshake.py. Where there is a function send_data(argument). I want to import that function into another file named siptest.py. I am encountering two problems. I am using microsoft visual studio with windows 7, 64-bit.
1) I can't import function. I have tried using,
from handshake import*
handshkae.send_data(argument)
Which give me an error.
NameError: global name 'handshake' is not defined
Another option I have tried is using
import handshake
handshake.send_data(argument)
Which gives me an attribute error.
AttributeError: 'module' object has no attribute 'send_data'
If I use it the other way, such as
from handshake import send_data
2) MS Visual studio says. No test discovered, please check the configuration settings but I still can run the test somehow. and it says that the test is failed because of Import Error.
ImportError: cannot import name send_data
Both of the said files are in same directory. Plus the function is defined in a class 'TCPhandshake' in handshake.py
One possible reason: there exists reference cycle between module a.py and b.py:
In a.py: import b
In b.py: import a
The solution is to break the cycle. You need to make it clear which module should do what and reduce the dependence.
Make sure both files are in the same directory and try:
from handshake import send_data
If this does not work, try to rename handshake.py file.
Are both handshake.py and siptest.py in the same directory?
If not You can try this:
1) Add __init__.py empty file to the directory that contains handshake.py.
2) Then add the path to that directory to LD_LIBRARY_PATH and PYTHONPATH
make sure the function is in the path
import sys
sys.path.insert(0, '/directory/tothe/handshakefile/')
and then
import handshake
I had the same issue, That happened while I tried to run the program from another directory by using python /home/name/workspace/test.py
Fix I tired.
import sys
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
This need to be added at the beginning.
This works for me.
I find a way to import files in the same directory by implementing keyword as and a object variable for example
import file as fileObj
But the downside is that when you want access to the imported files variable you have to first take fileObj.fileObjVariable.
Try adding/updating the environment variable PYTHONPATH which should point to the folder that has handshake.py
This is very simple, but for me it worked to restart the kernel.
Just make sure the files all are located in the root of the project, then this will work:
import handshake
handshake.send_data(argument)

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.

PyCharm/Django - cannot import module views.py (IDE is wrong)

I'm having a weird problem with import views in Django project. I'm not sure whether it is a problem caused by PyCharm of Django. So PyCharm says that it can't import views.py file, but it works when I run the server.
Here is the picture:
Do you know where could be the problem?
EDIT:
According to Inlangers answer, I've tried to change import to from vlado_web.translations import views which did not helped, moreover, it raises
Exception Value:
No module named translations
When I have from translations import views there, it works correctly but PyCharm says that it can't be resolved.
PyCharm doesn't know where your source files are. Try this:
Right click on folder vlado_web (the folder that contains manage.py) within PyCharm. Go to Mark Directory As and choose Sources Root.
This will let PyCharm know that the vlado_web directory is the root folder for your source code, and will allow you to perform absolute imports from there, e.g.
from vlado_web.translations import views
Try from vlado_web.translations import views