PyDev in Eclipse does not recognize db.add_column from South - django

I have just installed South (0.7.3, python-2.6) and successfully completed the tutorial using the python interpreter. Meaning that I am able to create a model and migrate it without any errors, so South appears to be working fine in the python shell. I used an sqlite3 db for the tutorial.
However, when I open my project in Eclipse, Eclipse does not recognize the functions associated with db in the migration folders: 0001_initial.py and 0002_auto__add_field_knight_dances_whenever_able.py files. I get the specific errors ( Undefined variable from import: add_column, create_table, delete_column, delete_table, send_create_signal)
Up until the South install, Eclipse has been working fine for creating django apps. I did point the PyDev interpreter to the south folder under site-packages (C:\python26\Lib\site-packages\south-0.7.3-py2.6.egg) (Other libraries there such as Django and django-picklefield work fine.)
I ran a simple script from the eclipse project and from the python shell and both appear to have the same sys.path's
Any tips on getting the Eclipse python interpreter happier?

Or, if you don't want to mess changing south source files or retouching all your migration files, you can consider south specific methods as globals in pydev code analysis.
You can change this in:
Preferences > PyDev > Editor > Code Analysis > Undefined
My exceptions list are:
_,tr,create_table,send_create_signal,delete_table,add_column,delete_column,alter_column,create_unique,create_index,delete_index,delete_unique,shorten_name,rename_column,execute

One (far from ideal) solution is to put ##PydevCodeAnalysisIgnore in all of your migrations. If you only have a few so far, you can do this manually. I had heaps, so I ran the following shell command, which will add the comment in as the second line of each file:
find . | grep '^.\/[a-z]*\/migrations\/.*\.py$' | xargs -I FILE sed -i '
1 a\
##PydevCodeAnalysisIgnore
' FILE
(Note: You should probably run find . | grep '^.\/[a-z]*\/migrations\/.*\.py$' to see which files sed will alter, before running the whole command. You can also run the whole command without the -i flag to see the changes themselves.)

Here's a workaround if you want to edit south/db/__init__.py:
--- db/__init__.py.original 2010-12-02 03:00:26.000000000 +1300
+++ db/__init__.py 2011-05-02 14:07:19.353636710 +1200
## -72,5 +72,9 ##
)
sys.exit(1)
-# Finally, to make old migrations work, keep 'db' around as the default database
+# Finally, to make old migrations work, keep 'db' around as the default
+# database. We're setting it explicitly to the generic operations first to
+# avoid pydev errors.
+from south.db import generic
+db = generic.DatabaseOperations(DEFAULT_DB_ALIAS)
db = dbs[DEFAULT_DB_ALIAS]

Related

Pylint and dynamic changes to sys.path

I've a project in which a python script must get the path to some of its imports from an environment variable. I'm handling this with
sys.path.insert(0, os.environ["PYDIR"])
which works fine when I run the application.
Pylint (in my Vim editor) is flagging import errors on all the modules I import from "PYDIR". I hate to disable the error message in the module since it's usually useful. OTOH, I also hate the visual clutter in my editor.
I suspect there's no good solution since Pylint doesn't execute the code but thought I'd ask anyway.
EDIT: See my below comment on difference between this question and PyLint "Unable to import" error - how to set PYTHONPATH?
For my particular case, the following seems to work satisfactorily. (I'm developing on OS X, I start a customized Terminal shell when working on this project, and my editor is 'mvim' (MacVim))
Add to the Terminal startup shell command list:
source .myprojectshellenv;
where .myprojectshellenv contains:
export PYDIR=path-to-myproject-python-modules
alias pmvim="env PYTHONPATH=$PYDIR mvim"
Then all I need remember is to edit the files with pmvim instead of mvim. (If I forget the error indicators from pylint (via syntastic) make it immediately obvious)

Why does Pycharm still refer to the old directories for my project

Am new to Flask development and am using an IDE like Pycharm for the first time, so please pardon the ignorance.
So while playing around with a project that I am working on to understand Flask, I created a virtual environment by the name venv. So the tree of my project would look something like :
my_project_directory
Project_specific_directories_and_files
requirements.txt
venv
To activate the virtual env, I used to do venv/bin/activate and my project would run as needed.
I came across virtualevnwrapper later and decided to use that. After installing it and setting it up, I moved over to the virtualenvwrapper way of working with virtual envs and completely removed(deleted) the venv directory from my project structure. The new project structure after deleting venv is as :
my_project_directory
Project_specific_directories_and_files
requirements.txt
Everything works fine. The project runs as expected.
However, now after the venv directory was removed, when I open my project in Pycharm, for my import statements like :
from flask import Flask, render_template, redirect, url_for, request, session, flash # , g
from flask_sqlalchemy import SQLAlchem
all the packages that I try importing gets underlined as error in the Pycharm IDE. I opened the Python console, in Pycharm, trying to debug the error and it gives the following error :
Error:Cannot run program "/Users/my_user_name/Desktop/some_parent_directory/my_project_directory/venv/bin/python" (in directory "/Users/my_user_name/Desktop/some_parent_directory/my_project_directory"): error=2, No such file or directory
Now I thought that apparently Pycharm is still using it's cache. So I tried invalidating the cache and restarting Pycharm as explained here, but the problem was still there.
Now I have 2 questions:
Why is Pycharm behaving this way? I removed the venv directory completely etc. Why is it still referencing the old (venv) location?
If it really is not able to resolve the dependencies, how exactly then is my project running all fine? Shouldn't it break the app?
Ok. After some further digging now, I was able to get the errors flagged in Pycharm rectified. I went to Pycharm -> Preferences -> Project: my_project_directory -> Project Interpreter & found that it was 'for some reason' still pointing to the older venv directory. From the drop down of the 'Project Interpreter:' section, I chose the path to my virtualenv created by virtualenvwrapper & saved the changes by Apply->Ok. This removed the error indications from the code in Pycharm. Although solved, the mystery of what/why was this happening is still unsolved for me. – qre0ct Jul 12 at 17:13
Moreover, I also removed the unused/deleted interpreters from the list of project interpreters by going to the 'more' section in the 'Project Interpreter' part and using the '-' at the bottom to remove the ones not needed.

Postgres.App + Django and proper path to libgeos_c.dylib on OSX?

I have succesfully installed Postgres.App. It works just fine if I have this in settings.py:
GEOS_LIBRARY_PATH="/Applications/Postgres.app/Contents/Versions/latest/lib/libgeos_c.dylib"
If I remove the above line and execute manage.py loaddata mydata, I get:
ImportError: Could not find the GEOS library (tried "geos_c", "GEOS"). Try setting GEOS_LIBRARY_PATH in your settings.
The question: is it possible to make things work without GEOS_LIBRARY_PATH set in settings.py? The reason is that in our team not all are using OSX + Postgres.App and the path is not the same for all developers. Django Docs mention usage of LD_LIBRARY_PATH, but having the next line exported in bash did nothing:
LD_LIBRARY_PATH="/Applications/Postgres.app/Contents/Versions/latest/lib/"
I still get the same error. Am I having a wrong syntax or is there another solution so that everyone can have individual path to the library?

No module named django.core when creating project in virtualenv

So I have looked around at a lot of questions similar to mine, however I couldn't find a concrete answer. My comp specifications are Windows 7 64-bit.
My problem is as such:
1) I installed virtualenv using pip:
pip install virtualenv
2) After that I created and activated a new environment:
path/virtualenv env
...
path/to/env/Scripts/activate
3) While running the new environment, I installed django:
(env) path/pip install django
4) After installing successfully, I am ready to make a project. However, upon trying it out:
path/django-admin.py startproject test
I get the following error:
File "C:/path/env/Scripts/django-admin.py", line 2, in (module)
from django.core import management
ImportError: No module named django.core
I have tried out various solutions people have posted, including using the full path:
python C:/path/to/django-admin.py startproject test
I have also checked to make sure the versions of Python it is referencing are correct,as both inside and outside the virtualenv it is associated with Python27. Many other solutions talked about PYTHONPATH or the syspath, however, when I import django or managemnet in the python shell, those work fine.
I have a feeling it may have something to do with the paths, but I'm not sure how a virtualenv interacts with the system paths. Since it is self-contained and the system paths are system wide, is it necessary to have something in the path specifically?
As an aside, my django-admin.py file is in both
path/env/Scripts
and
path/env/Lib/site-packages/django/bin
and the django folder is in
path/env/Lib/site-packages
How to fix this problem?
I solved this problem by using this command as following instead:
django-admin startproject
just remove the ".py" attached to "django-admin"
I could not get any other stack overflow answers to work either. Getting a venved Django stack running on Win64 is a bit of an ordeal.
But, I found an answer that worked for me here: http://samudranb.com/2012/06/02/how-to-setup-a-djangopython-development-env-on-windows/
Try running from an admin command prompt:
ftype Python.File="[your venv path]\Scripts\python.exe" "%1" %*
Just be sure to set it back to the original value when you're done.
This will help you understand why your facing that problem and there is also simple solution for that:
http://blog.jayteebee.org/2009/07/importerror-no-module-named-djangocore.html
I've literally searched for hours to a solution for this issue... I came across this video randomly: (https://www.youtube.com/watch?v=lPmkl4jtYgA) where he put "python .\Script\django-admin.py startproject" into the command prompt while in a virtual environment, so I tried the same with the following modification to point to the correct path on my machine "python .\env\Script\django-admin.py startproject". Voila!
Hopefully this helps someone as it seems there are multiple reasons for this issue.
Windows server 2003 provides the Where command
where python.exe
will show the full path of the current python.exe found on the path, use that to check it's using the correct one for your virtualenv.
The association issue comes into play because running
file.py
so the .py is argv[0] passes it through the windows association, which won't follow your venv.
python file.py
will not find file.py unless it's in the current directory.
So the solution is -
python %VIRTUAL_ENV%\scripts\django-admin.py startproject myproject
This runs python from the current active venv
and uses the venv env variable so it points to the correct location of django-admin.py (or you could give it an absolute path yourself of course)
I had the same problem. I solved that using this command:
(env)C:\environment directory>python Scripts\django-admin.py
This link was helpful for me:
enter link description here
i had the same problem i am running both python 3.4 and 2.7, so i pip installed Django globally on my machine and when i returned to my virtual environment i was able to create a project with no problems.
In Windows, you set:
set PATH=C:\virtualenv\python2.7\Scripts REM Scripts folder contains python.exe, pip.exe, django-admin.exe,...
set PYTHONPATH=C:\virtualenv\python2.7\Lib\site-packages REM site-packages folder contains packages of python such as django,...
After, create project mysite by
django-admin.py startproject mysite
I had this same problem using virtualenv in Terminal in MacOSX (Snow Leopard). My solution to the problem was to change the first line of django-admin.py from
#!/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
to
#!/path-to-your-virtualenv-directory/bin/python
Hope this helps someone.
I had the same problem and the way I resolve it was by activating my project directory before making migrations and running my server "python manage.py runserver".
Activate project Directory: source /path/bin/active
Path meaning where your project is stored.
Had same error and This solved it out for me.
source /path/to/virtualwrapper/activate
pip install django
This fix tries to re-install and configure django
I had the same problem, but I solved it, first I activated the virtual env, then ran:
django-admin.exe startproject project_name

How do I preload imports into Django's manage.py shell command?

When I run manage.py shell on my Django project to take a peek at something there are common imports that I always want to run at the start of the shell (e.g. I always want to import my model files.) How can I have these run automatically everytime I run the shell command?
2nd related question, when I hit the up arrow I get the "^A" character instead of the previously run command in the manage.py shell (and in my regular python shell), how can I fix this so it loads the previous command like on the Linux/Unix command line?
For the first question, look at the manage.py shell_plus command provided by the django-extensions project. It will load all your model files on shell startup. The project has got a whole load of other useful tricks too, so it's definitely worth checking out.
For the second question, I can only guess that you need to install readline.
you can replicate what
python manage.py shell
does by just doing:
from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
and you will have the environment all set up for the rest of that script. There are some other ways to do this here too: http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/
One single thing which can solve both of your problem is ipython. ipython stores the previous executions and it can give you completion as well.
Auto importing frequently used packages in python manage.py shell_plus
Example:
# local_settings
SHELL_PLUS_PRE_IMPORTS = (
('<app_name>.models', '*'),
('<app_name>.forms', '*'),
('<app_name>.views', '*'),
('django.core.urlresolvers', '*'),
('django.db', 'connection'),
('django.db', 'reset_queries'),
)
SHELL_PLUS_DONT_LOAD = ['<app_name>', '<app_name>']
Reference:
https://github.com/django-extensions/django-extensions/blob/master/docs/shell_plus.rst
Adding extra stuff to the django shell can be done using as a starting point the shell_plus command provided in the django-extesions app, and modifying it by adding whatever you want to make available to the 'imported_objects' dictionary.
Eg if you duplicate the shell_plus.py file and add these two lines at the end:
# .......
alist = range(1000)
imported_objects['alist'] = alist
code.interact(local=imported_objects) # <-- this is the original final line
When you run the shell using the new file the 'alist' reference will be available.
I put a longer example here: http://www.michelepasin.org/techblog/?p=1012