Best way of using virtualenv, Django and PyCharm

Hi all,

I've just bought PyCharm for my Django-based projects, and having participated in the beta test program, I am looking forward to keep on using this excellent IDE.

But, during the beta-phase, I was not using virtualenv based projects. Now I am, and I'm running into troubles for my unit tests. I can't figure out how to configure it all correctly, for the unit testing to run as it should.

Already from the start, it appears "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py" is being run as the "manage.py" script, whereas it would be more logical should this be the manage.py from my project's directory.

Also, the tests aren't run in my project's directory, which gives "App with label app_name could not be found".

Next to that, I have different settings (dev, acc, prd) inside a settings directory. I can load these when invoking manage.py from the command line, but I can't seem to find a way for PyCharm to take them into account (instead, I see "pycharm django settings imported", which is of course not what I want).

I have the impression that the "only" virtualenv support there is, is the possibility to select the virtualenv-specific python interpreter.

Has anyone succeeded in setting up tests (or more globally: use manage.py as it would from the command line with virtualenv activated) within a virtualenv environment?

I should say I'm primarily working on Windows 7 (which complicates matters further, I'm sure).

Thanks in advance!

(PS: The text editor for creating posts doesn't work as it should on Opera 10).

7 comments
Comment actions Permalink

Hello Mathieu,

The way it's supposed to work is the following:

- django_settings.py is needed to override the TEST_RUNNER setting and to

hook PyCharm's graphical test runner into the Django testing system

- django_settings.py imports your actual Django settings module. You can

tell it which one to import by filling the "Settings file" field in the Django

tests run configuration. By default, it tries to import module 'settings'.

- the working directory can also be specified in the Django tests run configuration.

Hope this helps.

I've just bought PyCharm for my Django-based projects, and having

participated in the beta test program, I am looking forward to keep on

using this excellent IDE.

But, during the beta-phase, I was not using virtualenv based projects.

Now I am, and I'm running into troubles for my unit tests. I can't

figure out how to configure it all correctly, for the unit testing to

run as it should.

Already from the start, it appears "C:\Program Files

(x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py" is being

run as the "manage.py" script, whereas it would be more logical should

this be the manage.py from my project's directory.

Also, the tests aren't run in my project's directory, which gives "App

with label app_name could not be found".

Next to that, I have different settings (dev, acc, prd) inside a

settings directory. I can load these when invoking manage.py from the

command line, but I can't seem to find a way for PyCharm to take them

into account (instead, I see "pycharm django settings imported", which

is of course not what I want).

I have the impression that the "only" virtualenv support there is, is

the possibility to select the virtualenv-specific python interpreter.

Has anyone succeeded in setting up tests (or more globally: use

manage.py as it would from the command line with virtualenv activated)

within a virtualenv environment?

I should say I'm primarily working on Windows 7 (which complicates

matters further, I'm sure).

Thanks in advance!

(PS: The text editor for creating posts doesn't work as it should on

Opera 10).

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0
Comment actions Permalink

Thanks for clarifying that bit.

I still can't load my specific settings though; could it be that the path is not added when loading them?

The directory structure is as follows:

apps/

    app1

    app2

    ...

    settings/

        __init__.py

        common.py

        development.py

        ...

Even when pointing the test runner config to c:/(...)/apps/settings/development.py, these settings are not loaded.

The workaround I'm using now is through  Python run commmands (manage.py test, etcetera), but of course this way I lose the nice test integration :-)

0
Comment actions Permalink

Hello Mathieu,

You can edit django_settings.py and print out the value of settings_file

to see what exactly is being imported. The code does support specifying the

path to a settings file in a subdirectory.

Thanks for clarifying that bit.

I still can't load my specific settings though; could it be that the

path is not added when loading them?

The directory structure is as follows:

apps/

app1

app2

...

settings/

__init__.py

common.py

development.py

...

Even when pointing the test runner config to

c:/(...)/apps/settings/development.py, these settings are not loaded.

The workaround I'm using now is through  Python run commmands

(manage.py test, etcetera), but of course this way I lose the nice

test integration :-)

---

Original message URL:

http://devnet.jetbrains.net/message/5282028#5282028

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0
Comment actions Permalink

It still seems my settings aren't being loaded correctly. django_settings.py reports that they are (see the traceback, below), but debugging a django test run points out that settings.INSTALLED_APPS is an empty list (which it isn't when running manage.py test items from the shell - that works fine).


Here's the traceback:

E:\Development\django_projects\penguinproject\Scripts\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py" --client 127.0.0.1 --port 49898 --file "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py" test items
Testing started at 20:36 ...
pydev debugger: warning: psyco not available for speedups (the debugger will still work correctly, but a bit slower)
pydev debugger: starting
E:\Development\django_projects\
penguinproject\lib\site-packages\path.py:32: DeprecationWarning: the md5 module is deprecated; use hashlib instead
import sys, warnings, os, fnmatch, glob, shutil, codecs, md5
settings file:  development
pycharm django settings imported
Manager file:  manage
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py", line 1165, in <module>
debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pydev\pydevd.py", line 929, in run
execfile(file, globals, locals) #execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm 1.1\helpers\pycharm\django_manage.py", line 15, in <module>
run_module(manage_file, None, '__main__')
File "c:\python26\Lib\runpy.py", line 140, in run_module
fname, loader, pkg_name)
File "c:\python26\Lib\runpy.py", line 34, in _run_code
exec code in run_globals
File "E:\Development\django_projects\
penguinproject\yabe\manage.py", line 11, in <module>
execute_manager(settings)
File "E:\Development\django_projects\
penguinproject\lib\site-packages\django\core\management\__init__.py", line 438, in execute_manager
utility.execute()
File "E:\Development\django_projects\
penguinproject\lib\site-packages\django\core\management\__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "E:\Development\django_projects\
penguinproject\lib\site-packages\django\core\management\base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "E:\Development\django_projects\
penguinproject\lib\site-packages\django\core\management\base.py", line 220, in execute
output = self.handle(*args, **options)
File "E:\Development\django_projects\
penguinproject\lib\site-packages\django\core\management\commands\test.py", line 37, in handle
failures = test_runner.run_tests(test_labels)
File "E:\Development\django_projects\
penguinproject\lib\site-packages\django\test\simple.py", line 396, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "E:\Development\django_projects\
penguinproject\lib\site-packages\django\test\simple.py", line 285, in build_suite
app = get_app(label)
File "E:\Development\django_projects\
penguinproject\lib\site-packages\django\db\models\loading.py", line 140, in get_app
raise ImproperlyConfigured("App with label %s could not be found" % app_label)
django.core.exceptions.ImproperlyConfigured: App with label items could not be found

0
Comment actions Permalink

Hi, was this post ever resolved? It seems as though this is still an issue.

0
Comment actions Permalink

I am running into a similiar issue with this test tool.  It seems to ignore my django version and everything, as the error that I am getting when using the test runner is below.  This is the same error that I get when trying to run the newer django 1.4 on my site instead of the specified django version.

Traceback (most recent call last):

  File "/home/mikewright/.IntelliJIdea11/config/plugins/python/helpers/pycharm/django_test_manage.py", line 105, in <module>

    utility.execute()

  File "/home/mikewright/.IntelliJIdea11/config/plugins/python/helpers/pycharm/django_test_manage.py", line 73, in execute

    PycharmTestCommand().run_from_argv(self.argv)

  File "/home/mikewright/.virtualenvs/musicpeeps/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 49, in run_from_argv

Creating test database for alias 'default'...

    super(Command, self).run_from_argv(argv)

  File "/home/mikewright/.virtualenvs/musicpeeps/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv

    self.execute(*args, **options.__dict__)

  File "/home/mikewright/.virtualenvs/musicpeeps/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute

    output = self.handle(*args, **options)

  File "/home/mikewright/.IntelliJIdea11/config/plugins/python/helpers/pycharm/django_test_manage.py", line 60, in handle

    failures = TestRunner(test_labels, verbosity=verbosity, interactive=interactive)

  File "/home/mikewright/.IntelliJIdea11/config/plugins/python/helpers/pycharm/django_test_runner.py", line 125, in run_tests

    return DjangoTeamcityTestRunner().run_tests(test_labels, extra_tests=extra_tests)

  File "/home/mikewright/.virtualenvs/musicpeeps/local/lib/python2.7/site-packages/django/test/simple.py", line 381, in run_tests

    old_config = self.setup_databases()

  File "/home/mikewright/.virtualenvs/musicpeeps/local/lib/python2.7/site-packages/django/test/simple.py", line 317, in setup_databases

    self.verbosity, autoclobber=not self.interactive)

  File "/home/mikewright/.virtualenvs/musicpeeps/local/lib/python2.7/site-packages/django/db/backends/creation.py", line 256, in create_test_db

    self._create_test_db(verbosity, autoclobber)

  File "/home/mikewright/.virtualenvs/musicpeeps/local/lib/python2.7/site-packages/django/db/backends/creation.py", line 321, in _create_test_db

    cursor = self.connection.cursor()

  File "/home/mikewright/.virtualenvs/musicpeeps/local/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in complain

    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "

django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

0
Comment actions Permalink

Hi Michael,

Sorry, I didn't understand your issue. Pycharm uses Django from interpreter configured in Settings->Project Interpreter.

0

Please sign in to leave a comment.