PyCharm module importing stopped working properly (OS X) - python

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.

Related

Unable to import modules in pycharm in spite being installed in the used python interpreter

Even after installing dash - getting module import error.
Pycharm does not throw any compilation errors on the line as well.
How could this be resolved?
I tried everything as suggested in comments and stuff known to me but could not resolve this - have done this a thousand times earlier but don't know what went wrong this time.
The only solution the=at worked was to create a new project, copy files over manually and then create a fresh interpreter and VE.

PyCharm tells me it cannot find modules although they are there

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.

In django 2.0, writing a script to delete models

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

Pycharm import RuntimeWarning after updating to 2016.2

After updating to new version 2016.2, I am getting
RuntimeWarning: Parent module 'tests' not found while handling absolute import
import unittest
RuntimeWarning: Parent module 'tests' not found while handling absolute import
import datetime as dt
'tests' is a package inside my main app package, and I receive these warnings when I try to execute unit tests inside this folder. This issue only came up after updating to 2016.2. Besides the warnings, the remaining code works fine.
Edit: This is a known issue - https://youtrack.jetbrains.com/issue/PY-20171. They are suggesting to replace utrunner.py in PyCharm installation folder.
This is a known issue introduced with the 2016.2 release. Progress can be followed on the JetBrains website here. According to this page it is due to be fixed in the 2017.1 release. You can follow the utrunner.py workaround that others have mentioned in the meantime - a copy of this file is attached to the linked ticket.
The latest recommendation (Dec. 19, 2016) is to put this line at the top of your unit test script:
from __future__ import absolute_import
On OS X I've fixed this by replacing
Applications/PyCharm.app/Contents/helpers/pycharm/utrunner.py
with an older version that can be found at
http://code.metager.de/source/xref/jetbrains/intellij/community/python/helpers/pycharm/utrunner.py
On Ubuntu 16.04 Bobby's solution also works:
Just replace your local utrunner.py file at
/usr/local/pycharm-edu-3.0/helpers/pycharm/utrunner.py
by the one from the Jetbrains website: http://code.metager.de/source/xref/jetbrains/intellij/community/python/helpers/pycharm/utrunner.py
On Windows 10 Bobby's solution also works:
Just replace your local utrunner.py file at
C:\Program Files (x86)\JetBrains\PyCharm 2016.3\helpers\pycharm
with the one from the Jetbrains website: http://code.metager.de/source/xref/jetbrains/intellij/community/python/helpers/pycharm/utrunner.py

Importing using pydev and .pth files

related to:
Unresolved Import Issues with PyDev and Eclipse
Recently I moved my django package to django_trunk so I can have a development version elsewhere. I have no issue importing django, runserver, or any of that.
My issue (annoyance?) is that all of the imports are coming up as underscored red indicating an error.
Pydev clearly knows of the imports, but for whatever reason it still imports as red.
I've noticed this with a few other packages, which use a .pth file instead of a direct import. In the past I've just ignored the red, but with django it's half my screen and I'm loosing critical information.
To demonstrate:
import django #note that django is underscored as red
import sys
print django
print sys.path
module 'django' from 'C:\Python27\Lib\site-packages\django_trunk\django__init__.pyc'
[...,'C:\Python27\Lib\site-packages\django_trunk', ...]
Try reconfiguring the Interpreter by going to Window -> Preferences -> PyDev -> Interpreter-Python and removing and adding the Python interpreters again.

Categories