I am trying to create a tests folder in my django app.
My app has following structure:
myapp
myapp/tests/__init__
mtapp/tests/test_email
myapp/function
Now i am trying to import function inside test_email file.
and executing test case as
python manage.py test myapp
but this gives me error No module named function.
Please let me know the reason behind this.
First Edit: if i put "import ..function" then its working fine. but is it a right way to do it.
Note: I am using python version 2.7,
Django version 1.5.5
It might be a case of some Cyclic import when you are using from ...filename import function.
It can be resolved by importing the function locally in the TestCase.
Related
I am having issues with importing, my own, pip package named the same as a module inside a django-app. Lets say my own pip module is called: fancymodule. I then have a module-folder inside a django-app named the same:
* django-project
* payments
* models.py
* fancymodule/
* __init__.py
The problem I am having, is that inside payments/models.py, I import:
from fancymodule import ApiClient
This is suppose to reference the fancymodule installed through pip. But is instead referencing the module inside the payments-django-app instead.
If I wanted to reference the module inside the payments-django-app, in my head, this should be:
from payments.fancymodule import whatever
I get that from the view of payments/models.py, the import fancymodule .. will reference fancymodule inside the payments-folder.. but can i change/fix this, so it reference the fancymodule installed through pip ?
FYI: Working on an old legacy project.
Home someone can help.
marcinn suggestion about using from __future__ import absolute_import worked.
So my solution inside the models.py file ended up being:
from __future__ import absolute_import
from fancymodule import ApiClient
And as I said to above comment.. I couldn't find anything wrong with my PYTHONPATH. as marcinn also said, this is most likly a Python 3.x thing.. and since i am on 2.7, it makes sense the PYTHONPATH looks fine.
OBS: PyCharm redlines the from fancymodule import ApiClient, but it works. So i think its just PyCharm that needs to be restartet to remove the redlines.
I am running some python scripts which pertains with a django application.
So I manually made a folder (named scripts) that contains some python scripts but is not made using ./manage.py startapp scripts. Along with this folder there is a modelsapp django app (which contains my models) and then project folder containing settings.py and urls.py.
I run ./manage.py shell < scripts/script.py to run my script.
Now here is a sample code of my scripts.py
from modelsapp.models import *
print(Room.objects.all())
This works pretty well. Now consider a second case. I now just run ./manage.py shell and then put the following commands-
>>>from modelsapp.models import *
>>>def init():
... print(Room.objects.all())
...
>>>init()
This also works pretty well. But now when I run the above code through the file in the first case, it says NameError: name 'Room' is not defined
It looks like the model classes which I just imported aren't accessibly inside a function.
This behavior is a bit weird.
Please help. In case I missed anything to mention, do comment below.
Edit: Since this is a bit weird behavior and am not able to find any answers to it, I am just going to pass the class Room directly to the function init() as a parameter to get the work done as of now.
after you run the django shell, you can run your script with:
>>>execfile('filename.py')
if you want to run it in a normal python shell (not using python manage.py shell) you have to do:
import django
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project(name.settings")
django.setup()
I'm working on a large open source Python project, which has modules used by both the project and other projects. The goal is to move some of these modules out to a new "library" project that can then be imported by the original project and other projects.
To make this transition smooth, the thought was to copy the modules over to the new project, and have the original project then use the new import. However, to allow other project to have time to migrate later, the thought was to have the original module redirect the import.
For example, the usage is like this in repo 'neutron' (other projects could do the same):
cat neutron/consumer.py
from neutron.redirected import X
print(X)
The in the new 'neutron_lib' project created, the module looks like this (the same as what the original was in project 'neutron'):
cat ../neutron-lib/neutron_lib/redirected.py
X = 5
In the 'neutron' project, I'm trying to do this as the redirect module:
cat neutron/redirected.py
import neutron_lib.redirected
import sys
sys.modules['neutron.redirected'] = neutron_lib.redirected
When I run pylint, it gives these errors:
************* Module neutron.redirected
E: 1, 0: No name 'redirected' in module 'neutron_lib' (no-name-in-module)
************* Module neutron.consumer
E: 1, 0: No name 'X' in module 'neutron.redirected' (no-name-in-module)
If I run this, it runs fine, and consumer.py prints '5'. If I use ipython and load consumer.py, I can see 'X' in dir() output.
Any idea why I'm getting this pylint error? Is it a false error? Is there a way to override it?
Looks like, when running under tox, I can add the following to .pylintrc to hide the errors/warnings
no-name-in-module
nonstandard-exception
When I run pylint it passes now, as does running the Unit tests. Just wish I understood why I'm getting these errors/warnings though.
Here's my code configuration:
__init__py
./module_1/foo.py
./module_1/__init__.py
./module_2/bar.py
./module_2/__init__.py
My goal is to be able to import the foo module from bar.py, but I'm getting quite confused on how to do it.
Something such as:
from ..module_1.foo import *
Will raise the following error:
ValueError: Attempted relative import in non-package
EDIT:
Ideally I'd like to be able to run my script in the following fashion:
python ./module1/foo.py
You haven't shown how you are invoking the script, but you need to ensure that your scripts are actually packages in your python path. That's basically what the error message is telling you, you were trying to import a "non-package". You probably don't have your top-level in the python path. For example ...
If your top-level module is called app and your configuration is
<path-to-app>/app/__init__py
<path-to-app>/app/module_1/foo.py
<path-to-app>/app/module_1/__init__.py
<path-to-app>/app/module_2/bar.py
<path-to-app>/app/module_2/__init__.py
You can run your script as follows.
cd <path-to-app>
PYTHONPATH=$PWD python -m app.module_2.bar
Works for me.
i am having an import error django_tables.
Could not import ikapp.views.admin. Error was: No module named django_tables.
It looks like ikapp.views.admin requires django-tables. Unfortunately it's not possible to tell which version/fork it requires, but it's likely to be either:
https://github.com/miracle2k/django-tables
https://github.com/bradleyayers/django-tables2
django_tables requires that you actually be running within the django-"primed" python environment. You can access this by going to your project's directory and typing
python manage.py shell
Then you can import django_tables with no problems.
For further info, take a look at the following: https://docs.djangoproject.com/en/dev/ref/django-admin/