Importing file from another folder in python - python-2.7

Im using Python 2.7.
I got structure in PyCharm (all dirs are packages):
dirExa\
dirBar\
Bar.py
__init__.py
dirFoo\
Foo.py
__init__.py
__init__.py
Now i want to import module called Bar.py in Foo.py
I used this (in file Foo.py):
from dirExa.dirBar import (some method)
but it gives me error such: No module dirFoo.dirBar import (some method)

Related

Can't run makemigrations after moving modules

I've got a module named: validators.py located at ./project/app1/validators.py.
To use it, I have imported it inside one of my Models:
# app1/models.py
from .validators import validate_something
After a while, I decide to move this validators.py to a separated directory utils/ (which contains a __init__.py file), so I can use it in all my apps without referencing the app1.
Now it's located at: ./project/utils/validators.py.
After moving the module, I changed my import at the beginning of the Model:
# app1/models.py
from utils.validators import validate_something
Now that I want to create new migrations using manage.py makemigrations I'm getting this error:
File "project/app1/migrations/0001_initial.py", line 5, in <module>
import app1.validators
ModuleNotFoundError: No module named 'app1.validators'
I can remove all migrations and regenerate them but I guess there should be another way to resolve this dependency issue.

ImportError using python if __name__ == "__main__"

I currently inherited a codebase that looks something like this.
project
manage.py
|_ config
|_ settings
|_ wsgi.py
|_ project
|_ app1
|_ app2
|.... <-- many more Django apps
|_ a_new_app
|_ __init__.py
|_ run.py
|_ foo.py
|_ bar.py
I added a new app with some .py files which imports from other apps too in the same package and other app packages too in the project. All is well till I tried to run
python project/a_new_app/run.py
Then I started getting import error here is how my run.py looks.
# run.py
from project.a_new_app.foo import Foo
class App():
def method(self, key):
data = {"some-key": Foo}
return data.get(key)
.... more methods here
if __name__ == "__main__":
app = App()
app.loop_forever()
I got this error
File "project/a_new_app/run.py", line 7, in <module>
from project.a_new_app.foo import Foo
ImportError: No module named project.a_new_app.foo
My working directory is /user/me/PycharmProjects/project, Thanks.
from project.a_new_app.foo import Foo
For this import to work, you need the outer project directory (the one containing manage.py and the inner project directory) to be on the Python path.
However, run.py is two directories deeper than that, in project/a_new_app. Therefore you need to add ../.. to the python path at the top of the module.
import sys
sys.path.append('../..')
from project.a_new_app.foo import Foo
...

Django: how to set the path for the environ variable "DJANGO_SETTINGS_MODULE"

In Django, I used to write populating scripts and put them in the project root directory. For example,
mysite/
mysite/
manage.py
populateA.py
The first few lines of populateA.py:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
import django
django.setup()
...
As there are more and more populating scripts, I would like to move them to another package populate:
mysite/
mysite/
manage.py
populate/
__init__.py
populateA.py
populateB.py
populateC.py
...
However, when I run the populating scripts (python populateA.py), I got the error message: ImportError: No module named 'mysite'. How to properly set the path for DJANGO_SETTINGS_MODULE?
Since populate is already a module, run its submodules.
python -m populate.populateA

No fixture named 'X' found

I'm using Django 1.6 and I use South to handle migrations.
In most of my application I used to have initial_data.json files. I converted them to be loaded with migrations rather than automatically by Django (as this is recommended in the documentation)
I was using version 0.8.2 of South when I ran into a weird behavior / bug where loading fixtures is done according to the model code and not the state of the migration. I saw that the newest version (0.8.4) has since added some bug fixes related to loaddata, so I upgraded to it.
Now I get the following error on all the migration that load fixtures:
UserWarning: No fixture named 'X' found.
When I use Django's loaddata it works fine. Any ideas on how to solve this?
South and fixtures
South simply patches syncdb to skip the fixture loading for models with migrations, and actually loads them after the final migration for an app has been run.
Make sure your initial_data file is located in the correct place
Loading initial_data does not require you do actually do something, but place the fixtures in the correct place as explained in Django's documentation.
To quote the docs:
By default, Django looks in the fixtures directory inside each app for
fixtures. You can set the FIXTURE_DIRS setting to a list of additional
directories where Django should look.
This means that if you have an app called "myapp", you'd create a "fixtures" dir inside it and place the json there, e.g.: myproject/myapp/fixtures.
Django 1.7 and newer
Django 1.7 introduced built-in migrations. These have an interface similar to South; management commands to create migrations makemigrations, run them migrate, and others.
However, initial_data fixtures are no longer auto-loaded on syncdb run; unless it is an existing app, and has no migrations. This is mentioned in the release notes.
The docs now recomend to create a datamigration to handle fixture loading. Luckily, this is pretty easy to do, here's how I usually do it:
1. create an empty data migration
$ python manage.py makemigrations --empty myapp
If you had only the initial migration, you end up with these files (note that I renamed migration 0002 for clarity):
myapp/
├── __init__.py
├── fixtures
│   └── my_initial_data.json
└── migrations
   ├── 0001_initial.py
   ├── 0002_load_fixture.py
└── __init__.py
2. update the code of 0002_load_fixture.py to run loaddata
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
from django.core.management import call_command
def load_my_initial_data(apps, schema_editor):
call_command("loaddata", "my_initial_data.json")
class Migration(migrations.Migration):
dependencies = [
('myapp', '0001_initial'),
]
operations = [
migrations.RunPython(load_my_initial_data),
]
This error also occurs when the filename does not include the '.json' extension.
In order to load a fixture from current working directory it has to end with '.json'.
Why don't you run loaddata comand from your migration then?
import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
class Migration(DataMigration):
def forwards(self, orm):
from django.core.management import call_command
call_command("loaddata", "my_fixture.json")
If your still getting the error:
No fixture named 'X' found
Try using the path to the json file, as manage.py is probably not being run from the project directory (it may even be run from the root directory). Try something like this:
import os
app_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))
json_path = os.path.join(app_path, 'fixtures', 'my_fixture.json')
call_command("loaddata", json_path)

Scrapy project can't find django.core.management

I'm trying to follow the method here to 'Scrapy' data from the web and simultaneously save that data directly to my Django database using Scrapy's item pipeline.
However, when I try to run scrapy crawl spidername, I'm getting the error:
ImportError: No module named django.core.management
At first I thought it was because my Scrapy project was outside of my Django project folder, but even after I moved the whole project into my Django project folder I kept getting the same error. If I open a python shell inside the Scrapy project folder in its new location (inside my Django project folder), import django.core.management works fine. So what's going on?
EDIT: Some additional info: I'm doing this on a Webfaction server, and the path to my Django project is /home/gchorn/webapps/django_app/django_project. I'm using Django version 1.4.1, Scrapy 0.16 and Python2.7. The layout of the Django+Scrapy project is as follows:
django_project/
__init__.py
manage.py
settings.py
urls.py
myproject #folder containing wsgi.py
app1
app2
app3
templates
ScrapyProject/
scrapy.cfg
ScrapyProject/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
my_spider.py
Try setting this in your Spider's settings.py:
import os
import sys
sys.path.append('/home/gchorn/webapps/django_app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
Then you can import your model classes like:
from django_project.app1.models import some_model