I started a django project, it's still very basic. There are three apps in it. The app "eventmanager" builds on the app "locationmanager". So I try to import like this:
from locationmanager.models import Location
PyCharm claims that the reference "locationmanager" cannot be resolved. However, when I run my code with the django testserver, it runs just fine. Did I do something wrong while setting up PyCharm?
EDIT: Screenshot of my project structure in PyCharm
When you import a django project into pycharm, you have to set the django project folder in pycharm as the Sources Root for the reference resolving to work properly.
Related
I have tried every answer on the (Django script to access model objects without using manage.py shell) stack question, and I always get error "no module name 'project_name'".
My project name is called snapbackend.
I have an __init__.py setup. I know I can write django command, but that is somewhat overkill to run one function.
I am using django 2.0, and I wanted to write a script to delete old models.
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "snapbackend.settings.production"
import django
django.setup()
import snapbackend
from snapbackend.models import deleteCapsuleModels
deleteCapsuleModels()
This is because your module isn't installed, and it is not being run from the directory which it is inside of.
If you are using setuptools (a setup.py file), then the proper way to solve this is to symlink your project into your site packages with python setup.py develop. This will make your module available throughout your project.
If you aren't using setuptools then this is a bit trickier. If you are able to choose your current working directory when running the script, you can solve this problem by executing cd YOUR_PROJECT_DIRECTORY before running your script.
In cases where you can't mess with the current working directory, you should se the PYTHONPATH environment variable to the root of your project. This environment variable is used to add additional paths for Python to find modules within.
It's important that you don't use PATH or sys.path for security reasons. Specifically, you don't want to accidentally introdduce any executables into your system which you are unaware of.
Hope this helps!
This is an issue with you PYTHON_PATH which seems simple but is often more tricky to figure out.
Python cannot find your django root so you need to tell python where it can find that module (not sure why it doesn't just work) but you can do this when running the script:
PYTHONPATH=../../. python your_script.py
Or the fly with:
import sys
sys.path.append('../../.')
In your case:
import os
import sys
sys.path.append('../path/to/snapbackend/.')
os.environ["DJANGO_SETTINGS_MODULE"] = "snapbackend.settings.production"
import django
The directory would depend on the relative path to your django root
I've been working on a Django project for months, all has been fine, then I did a big code merge and PyCharm has stopped recognizing module imports. The following work:
from django.utils.timezone import *
from django.test.testcases import TransactionTestCase
from kimsim_app.views import *
But these no longer do:
from django.utils import timezone
from django.test import TransactionTestCase
from kimsim_app import views
By work, I mean the latter timezone and TransactionTestCase are highlighted with red underlining. Lines of code that are missing imports are not underlined (although perhaps the parser gives up). Optimize imports does nothing. Update: a few minutes later and testcases import TransactionTestCase is again highlighted in red (edit - working again).
I have restored an old .idea directory from before the problems. I have recreated from scratch the virtual environment. I have duplicated the source, removed .idea and recreated the PyCharm project - nothing seem to help, yet running the Django unit tests from the command line work fine. When I navigate to the source of django/utils/__init__.py the code is not syntax highlighted. I have tried setting the settings file in Run Configurations and 'Settings, Languages and Frameworks, Django'. I have tried upgrading to PyCharm Pro 5.0.4. A colleague with identical source code has no problems.
Only when I uninstalled, deleted all the directories listed on this page and below, then reinstalled, did the import validation start working again. On this latter attempt I did recreate the PyCharm project (ie, the .idea directory), I did not enter my license code for fear of it downloading saved settings or something, I did not recreate the virtual environment or VCS settings.
The directories listed for OS X:
Configuration: ~/Library/Preferences/<PRODUCT><VERSION>
Caches: ~/Library/Caches/<PRODUCT><VERSION>
Plugins: ~/Library/Application Support/<PRODUCT><VERSION>
Logs: ~/Library/Logs/<PRODUCT><VERSION>
For Unix systems (I'm not sure if this bug affects Unix or Windows): ~/.<PRODUCT><VERSION>
Windows Vista, 7, 8: <SYSTEM DRIVE>\Users\<USER ACCOUNT NAME>\.<PRODUCT><VERSION> eg, c:\Users\Bert\.PyCharm45\
Windows XP: <SYSTEM DRIVE>\Documents and Settings\<USER ACCOUNT NAME>\.<PRODUCT><VERSION>
In addition to the above, I also tried uninstalling all versions of PyCharm then reinstalling, creating new everything (virtual env, .idea project settings, etc) except the source code, but I still got the error - only when I deleted all the directories listed did the bug disappear. I haven't dared re-import my settings. The jetbrains bug hasn't been assigned after two days.
Seem to be having a problem here. Been working off my netbook for a while and brought all my code across to my desktop. Now, I'm getting 'No module named x.urls' on every single page. I don't know why. Works fine on the netbook. All I did was copy and paste all the code across. Also, I'm running 1.2.1 on the netbook and moved across to 1.3 on the desktop. Using DevServer on Debian Testing. Anything I'm missing here?
Everything is in the one urls.py file at the project root. Pastebin: http://paste2.org/p/1419080
My Settings.py (obviously in project root): http://paste2.org/p/1419084
Anything else I should post?
Thanks for all the help!
Name the directory where you put you project 'x' and it will work.
I have an old project, it written under Python 2.5/2.6, Windows.
We had Python 2.6/Win7/x64 now, and I tried to start it.
I got the old project that running nondebug mode in a server, and
copied into local folder.
When I tried to yesterday start it, I got this error:
15:44:58,038 DEBUG [pylons.configuration] Loaded None template engine
as the default template renderer
I see the google, but they are points to config.init_app, that is does
not exists.
TOday I reinstalled Python, but with Py2.7, pylons and mako.
But when I tried to stat it, I got only this message:
07:36:36,377 DEBUG [pylons.configuration] Initializing configuration,
package: 'x'
And no more information about die... :-(
So what do you meaning, how can I raise this "undead" project to debug
some things?
( it was good experience with Python/Pylons, but I'm sad now that I
not choose PHP previously, because of package changes).
Thanks:
dd
might be obvious but did you run "python setup.py develop" on the application package so that the dependencies could be installed?
I'm developing a gae application on a windows machine. to have session handling I downloaded gaeutilities and added its path (C:\Python25\Lib\site-packages\gaeutilities-1.2.1) to the registry ("PythonPath" item under python25).
in my code this is how I import the gaeutilities Session class:
from appengine_utilities.sessions import Session
when gae engine (dev_appserver.py) tries to import it, an exception is raised, stating an importerror and "no module named appengine_utilities.sessions"
on the other hand, pyscripter can find the module (autocomplete becomes available for the Session class), and I can import the module within the python interpreter (the same one that dev_appserver uses, python 2.5.4).
for a remedy, I created a PYTHONPATH environmental variable and also added the path to it. nothing changes.
I'm lost. what am I doing wrong?
important edit: I have found myself to be totally unable to import any 3rd party gae modules. PYTHONPATH is correct, sys.path is correct, registry is correct, still dev_appserver complains of importerror.
Strange.
I would start troubleshooting by making 100% sure that the sys.path that dev_appserver.py uses does include C:\Python25\Lib\site-packages\gaeutilities-1.2.1.
I suggest you display sys.path in a HTML view served by dev_appserver.py.
Check permissions on gaeutilities-1.2.1 directory and subdirectories. Perhaps the python interpreter is unable to create *.pyc files or something like that.
Another suggestion:
Put the appengines_utilities folder in your application directory (the directory that contains your app.yaml file). I guess you need all third-party stuff there anyway if you want to upload the code to google's servers.