https://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
Where would i add this?
path = '/path/to/mysite'
if path not in sys.path:
sys.path.append(path)
You should create a file django.wsgi and put that line there. In m case, django.wsgi contains,
import os
import sys
sys.path.append('H:/Projectys/mysite')
sys.path.append('H:/Projects/mysite/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I have added sys.path such that because my project tree is
+H
++Projects
+++mysite
++++mysite
+++++apache
++++++django.wsgi
+++++mysite
++++++setting.py
++++++__init__.py
++++++urls.py
++++++view.py
+++++media
You should reference the location of django.wsgi in your httpd.conf (apache conf)
FWIW, you should also read the official mod_wsgi documentation as well.
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
Technically it doesn't matter where in the file you place the lines adding extra directories to sys.path if they are only referring to where your Django site directory it. This is because they only need to be set up by the time the first web request occurs. That is, the first time the application object is called.
So, if you stuck them as the very last thing in the file it would actually still work. In general though, it looks more logical to stick them in before you actually import Django modules. By doing that you ensure that if Django were ever changed to do loading up front rather than lazy loading on first request that it will still work.
Obviously they at least have to be after the import of 'sys' though.
Just after this code, it is written :
just below the import sys line to
place your project on the path.
Remember to replace 'mysite.settings'
with your correct settings file, and
'/path/to/mysite' with your own
project's location.
Related
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)
In one python file when I try from pandasql import sqldf it works. The path for that is
C:\Users\AmitSingh\Anaconda2\python.exe "C:/Users/AmitSingh/PycharmProjects/HelloPython.py/exercise 2"
In another file when I use the same command it gives me the error
ImportError: cannot import name sqldf
The path for this file is
C:\Users\AmitSingh\Anaconda2\python.exe C:/Users/AmitSingh/Desktop/Data/Udacity/Intro_Machine_Learning/ud120-projects/datasets_questions/explore_enron_data.py
I don't understand why? When I write import sqldf the prompt shows sqldf as an autocomplete option. But doesn't work.
By default, you can't. When importing a file, Python only searches the current directory, the directory that the entry-point script is running from, and sys.path which includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).
However, you can add to the Python path at runtime:
# some_file.py
import sys
sys.path.insert(0, '/path/to/application/app/folder')
import file
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
Ok so I've been struggling with this for over a week now and I've tried various methods mentioned on this site and others on google but here goes. I'm running python 2.7. I've got a python script in the parent directory which calls a second script located in a child directory. The second script starts a different thread and does an os.system call to a third and final script. This third script needs to import something located in the parent directory. Can someone tell me what's wrong with this setup? I do have __init__.py located in every folder being used. And I do try adding the relative parent directory to the path. I'm not sure where I'm going wrong.
File Structure
Parent Directory
Python_Script_1.py
Imports_needed.py
__init__.py
Child Directory
Python_Script_2.py
Python_Script_3.py
__init__.py
Method 1
Python_Script_2.py
import os
import sys
import multiprocessing
def listen():
listen_string = "python ~/path/Python_Script_3.py"
os.system(listen_string)
q = multiprocessing.Process(target=listen())
Python_Script_3.py
import sys
sys.path.append("..")
import Imports_needed
ImportError: No module named "Imports_needed"
Method 2
Python_Script_2.py
import os
import sys
import multiprocessing
def listen():
listen_string = "python ~/path/Python_Script_3.py"
os.system(listen_string)
q = multiprocessing.Process(target=listen())
Python_Script_3.py
import sys
sys.path.append("..")
from .. import Imports_needed
ValueError: Attempted relative import in non-package
Question about method 2, how come it's telling me this is not a package despite each directory containing __init__.py?
Additionally, I've used something very similar to method 1 in the past but I cannot see any differences between my code or file structures. If anyone has any suggestions I'd greatly appreciate it. Thanks.
EDIT: Apologies, I forgot something fairly significant in the description. There's a third file involved that I completely forgot about... So sorry :( Python_Script_2 makes a new thread via multiprocessing and does an os.system call to Python_Script_3. Python_Script_3 is located in the same child directory as Python_Script_2. Python_Script_3 is having the import issues when trying to import Imports_needed.py from the Parent Directory. I've updated the question to reflect this. (I'm sorry, I know this is a major detail that I left out but it's a complicated package and doing many more things than just what I'm asking about)
try the following
from Parent_Directory import Imports_needed
or
from Parent_Directory.Imports_needed import <your class or method>
You don't need
sys.path.append("..")
at all. If Python_Script_2.py is imported from the main script, import path inside of Python_Script_2.py will be relative to the main script. So
import Imports_needed
is enough.
Make sure you hava double underscore prefix for the file _init__.py in your child directory (in your example you have only 1 underscore symbol _).
UPDATE
If you rely on sys.path.append(), you should use:
import os
import sys
sys.path.append(os.path.realpath('..'))
otherwise, '..' says nothing to the interpreter about your app environment.
Could somebody give me a pointer on why I need to add my project root path to the python path as well as the application itself in my WSGI file?
Project base is called 'djapp', the application is called 'myapp'.
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../djapp')
os.environ['DJANGO_SETTINGS_MODULE'] = 'djapp.settings'
If I omit the line with "/../djapp/" the log tells my that 'myapp' can not be imported, even though 'djapp.settings' is. (validating 'djapp' was imported)
It al runs properly with the ./manage.py command. there's a __init__ in the project folder.
For testings sake, I see the same issue using addsitedir:
site.addsitedir('/home/user/web/project/')
site.addsitedir('/home/user/web/project/djapp')
Since djapp (the django project folder) is in a parent folder that also belongs to the deployment I renamed the djapp folder simply to project.
Then this code is always correct:
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..' )
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
The complete folder layout being:
host.example.com\
etc\
bin\
project\
logs\
And what have you. This way project can always be called project :)
Hope that helps.
GrtzG
Presumably you've got code within your project which is doing from myapp import foo.
Two options:
change that to from djapp.myapp import foo, which is not recommended as it prevents portability;
only add djapp in your WSGI, and set the DJANGO_SETTINGS_MODULE to just 'settings'.