PyCharm + Flask: Unresolved reference - How do I properly import Python namespaces? - python

I have added Flask and Flask-Script to PyCharm virtual environment. The app runs but PyCharm gives me various errors:
Unresolved reference "Manager" (from flask.ext.script import Manager)
Cannot find reference "script" in "__init__.py" (from flask.ext.script import Manager)
No module named "script" (command = flask.ext.script.Command(app)
As a result code completion doesn't work.
So if I directly import the modules like
import flask_script
code completion works but I am not sure if that's an intended use!
I can fix that if I import flask_script directly instead of flask.ext. Is that a valid workaround?
Is there any other proper way to solve that? I read a few answers but none of them seems to address this issue.

In your venv > lib > python3.x > site-packages mark site-packages as source root.

Go to File -> Default Settings -> Project Interpreter
Add your respective interpreter.
Then, add the 'Flask' package by clicking that plus sign. It will cover up all the module.
It works for me!

I had a similar error stating "Cannot find reference 'Flask' in 'flask.py".
If you're on windows (using Pycharm) then go to
Files -> project interpreter
There will be a list of packages, locate Flask and open it by double clicking it.
Then click install Package, it will install it for your project.
I don't know about it much in detail but this is how I solved my problem.

You need to tell PyCharm which interpreter you are using. Let's say you use some virtualenv, on a Mac (is similar on Linux) go to File > Default Settings > Project Interpreter
Then click on the little mechanic wheel next to the project interpreter dropdown menu and choose 'add local' and pick your interpreter (e.i. the virtual environment you use in which you installed flask).
Hope this helps.

Try giving the file a name other than flask.py. Because there will be a naming conflict during import.

Related

PyCharm wrong notification: no module named cx_Oracle

I've installed cx_Oracle 5.2.1 for Python 2.7.10, and it works (running Win). My problem is though; PyCharm notifies me that the module name does not exists, which is not a problem in runtime. But because of this PyCharm is unable to assist me on the modules different function etc.
Can anyone clarify please?
I already looked into this PyCharm: Python's standard lib's names and functions are underlined as "No module named such" and I though it might be related. I don't see any solution though.
EDIT 1:
I read that it might fix the problem to delete cx-Oracle from the Project Interpreter and add it again. Problem is though that I get an error trying to install cx_Oracle: error: command 'C:\\Program Files (x86)\\Common Files\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\link.exe' failed with exit status 1120. I use PyCharm through a proxy.
EDIT 2:
As mentioned in a comment
import cx_Oracle
print cx_Oracle.__file__
yields C:\Python27\lib\site-packages\cx_Oracle.pyd, and you can see the Project Interpreter Paths here.
Go to File > Invalidate Caches > Invalidate and Restart or Invalidate and check.
If you want to check the library inside pyCharm go to File > Settings > Project > Project Interpreter. Select the interpreter and check the library is listed.
If you want to check the path where the said module is installed.
import cx_Oracle
print cx_Oracle.__file__
Open Pycharm
Go to File -> Settings -> Project interpreter
In the package column, click the pip package and find cx_Oracle from the list. Select it and click the install button, now it will work fine.

Pycharm unresolved reference in virtualenv

When I started a new project in Pycharm using virtual environment, I got an unresolved reference warning message.
But I have installed django to my virtual environment and this code is working correctly.
How can I fix this? I'm using PyCharm 4.5.2 Pro
Open your project in pycharm and follow the step:
click on file(on top) > settings > project interpreter > add local > select virtualenv
OR
Make sure your project is a root directory, make it is the root directory.
I eventually stumbled across a method that worked for me, but I don't know the underlying problem.
Initially I created a virtualenv with PyCharm in the interpreter options. Annoyed that I was having this problem, I deleted it and just created a virtualenv with the virtualenv command, but the problem remained.
Then I went back to the interpreter selector and picked "add local". I navigated to the python binary in the /bin folder of the virtualenv I created and selected it. After this, the unresolved references were fixed.
I think your problem is like mine. I tested import matplotlib in the python shell, and it works, but in Pycharm it shows an error. This is because Pycharm doesn't know the path of site-packages.
To fix this, you can do one of the following:
Add the path of site-packages (for me C:\Program Files\Python 3.5\lib\site-packages) to PATH.
Go to File -> Settings -> Project Interpreter, then choose Python real path instead of venu path.
In your code add the following:
.
import sys
sys.path.append(r"C:\Program Files\Python 3.5\lib\site-packages")
import matplotlib.pyplot as plt
I think it will help you :)
File > Settings > Project > Project Structure
Double Click on the folder link under Content Root, and do OK.
Check your project root directory. Maybe problem in it.
And did you create virtualenv manually or through PyCharm?

ImportError: No module named 'bottle' in PyCharm

I installed bottle on Python 3.4 with pip install. In the terminal, when I do:
$ python3.4
>>>import bottle # shows no import error
>>>
but when I do it in PyCharm, it says:
import bottle ImportError: No module named 'bottle'
in your PyCharm project:
press Ctrl+Alt+s to open the settings
on the left column, select Project Interpreter
on the top right there is a list of python binaries found on your system, pick the right one
eventually click the + button to install additional python modules
validate
In some cases no "No module ..." can appear even on local files. In such cases you just need to mark appropriate directories as "source directories":
The settings are changed for PyCharm 5+.
Go to File > Default Settings
In left sidebar, click Default Project > Project Interpreter
At bottom of window, click + to install or - to uninstall.
If we click +, a new window opens where we can decrease the results by entering the package name/keyword.
Install the package.
Go to File > Invalidate caches/restart and click Invalidate and Restart to apply changes and restart PyCharm.
Settings:
Install package:
I am using Ubuntu 16.04. For me, it was the incorrect interpreter, which was by default using the virtual interpreter from the project.
So, make sure you select the correct one, as the pip install will install the package to the system Python interpreter.
PyCharm 2019.3, my solution is below:
For me, none of the above worked, and curiously even within one file some imports worked, some didn't:
from folder1.folder2.folder3.my_python_file import this_function # worked
from folder1.folder2.folder3.my_python_file import that_function # didn't work
Follow the above advice, but if it doesn't fix it additionally, (in PyCharm) click File >> Repair IDE and confirm all the 6 steps one after another.
I had virtual env site package problem and this solved it:
In the case where you are able to import the module when using the CLI interpreter but not in PyCharm, make sure your project interpreter in PyCharm is set to an actual interpreter (eg. /usr/bin/python2.7) and not venv (~/PycharmProject/venv/...)
I had the same problem, I tried all fixes like installing from the project interpreter and installing from python console, nothing worked. What worked was just going to the project folder from the terminal and installing it from there.

Adding python modules to pydev in eclipse results in import error?

I have a problem getting PyDev on eclipse to recognize already installed modules. Here is my detailed approach. The machine is a Mac (Snow Leopard).
In terminal the command
python --version
shows Python 2.6.6.
import unidecode
and
from unidecode import unidecode
work both fine!
I installed PyDev in Eclipse and went to configured the python interpreter (Auto Config). I selected all the proposed packages and hit Apply and Ok.
But eclipse keeps complaining
Traceback (most recent call last):
File "/Users/me/Documents/workspace/myproject/python/pythontest.py", line 12, in <module>
from unidecode import unidecode
ImportError: No module named unidecode
The python file looks like this
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
from unidecode import unidecode
def main():
print unidecode(u"Ă…got Aakra")
if __name__ == '__main__':
main()
When I remove the first line in the script
#!/usr/bin/env python
it results into the same error.
Does someone know where the problem lies?
This is the solution to my problem:
Find out the path to the folder ../site-packages/ of your corresponding python version. ( For me it was /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/ for python 2.6 on my Mac with Snoe Leopard.)
Open eclipse preferences and go to PyDev -> Interpreter - Python.
On the left side of the lower box, click on New Folder.
Add the navigate to ../site-packages/ of your corresponding python version.
Hit open.
Hit Apply.
Hit Ok.
And you should be good to go. =)
Thanks #all particionts, who provided hints into the right direction in the comments.
I ran into the same problem just today. I am using pydev and had a working project with a number of sub-packages. Suddenly after having created a new module I was not able to use this module in a different package. The puzzling feature was that I could use another module in the same sub-package...
Finally after
eclipse restart
remove/add python interpreter and all site-packages
annoyed head-scratching
I deleted all compiled classes with the following script:
import os
def clean_folder(folder):
for file in os.listdir(folder):
path = os.path.join(folder,file)
if os.path.isdir(path):
clean_folder(path)
if '.pyc' == file[-4:]:
print 'deleting: ' + str(path)
os.remove(path)
if __name__ == '__main__':
folder = 'YOUR_PROJECT_SRC_PATH'
clean_folder(folder)
and finally I can do 'actual' work :)
Hope it helps somebody...
You can simply add the module to the pydev path. Go to project properties (from the context menu) -> PyDev -> PYTHONPATH -> External Libraries. Depending on whether the module is in a source folder or a zip/egg file, select either Add source folder or Add zip/jar/egg. Navigate to the site-packages directory and point to the relevant file or folder (mine is: /usr/local/lib/pythonx.x/site-packages)
Try preferences > pydev > interpreter - python and removing and re-adding the python interpreter (make sure you know the path to it before you delete it), when you re-add it tick all the boxes.
When Eclipse gets 'lost' with respect to what packages exists on your system or in your project, from the context menu of your project, choose 'Properties' menu item, then the 'PyDev - PYTHONPATH' item in the treeview on the left of the dialog, then the 'Force restore internal info' button. Seemingly, PyDev keeps a computed cache of the info and when for any reason the cache becomes incoherent, you can force PyDev to recompute.
In my case I was not getting this error before compiling, but when I compile I got the error ImportError: No module named myant.core. I tried to add the files from PyDev-PYTHONPATH, but again I got the same error. Then I realized that I actually do not have to add the path exactly to the folder where my .py files are located. Infact I have to add the folder where myant.core is located. After doing this I did a restart when I recompiled my project again, the problem was fixed. I would share that I have:
Python 2.7 Eclipse kepler 4.3, PyDev 3.9.2 and on my ubuntu 14.04
.py files location:/${PROJECT_DIR_NAME}/src/myant/core, therefore I added /${PROJECT_DIR_NAME}/src
#Aufwind your answer above helped but didn't solve for me.
Find the path to the folder ../site-packages/ ....
Open eclipse preferences and go to PyDev -> Interpreter - Python.
On the left side of the lower box, click on New Folder.
---> here I departed from your instructions. I added the egg for the module that wasn't being recognized. Adding the site-packages folder didn't fix it.
Hit open.
Hit Apply.
Hit Ok.
And then I was good to go. =)
Open eclipse window -> preferences and go to PyDev -> Interpreter.
click on 'Check if interpreters are synchronized with environment'
This did it for me. No Eclipse restart was required.
I fixed this problem by going to the project properties -> PyDev Django
and setting the Django settings module.
For Oxygen 2 (I think it worked on earlier versions, too)...
Right click on project folder and select "Properties"
Select "PyDev - Interpreter/Grammar"
Click on "Click here to configure an interpreter not listed"
Select any existing interpreter from the top list of configured interpreters
A "Selection Needed" dialog should appear where you must select one or more interpreters to restore. Check all that apply
Click "Ok" and PyDev will rescan, and I assume, rebuild some internal view of your site-packages
Click "Apply and Close" to close all dialogs
To make the import error markup disappear in my code editor, I need to type a space after the offending import then save the change. The import error then disappears because PyDev can now find the offending import module.
Suppose your eternal module is in /.
Launch Eclipse and go to the project option. Select "PyDev-PYTHONPATH"
and on the right you will see a tabbed window. Select External Libraries there.
Click on Add Source Folder and select your library from the above path.

Unresolved import: models

I'm doing my VERY first project using python/django/eclipse/pydev following this guide
http://docs.djangoproject.com/en/dev/intro/tutorial01/
My only addition is the use of Eclipse/pydev.
I'm getting many errors related to "Unresolved imports". I can remove the errors using "remove error markers" and my site runs perfect (I can browse it) but I want to get rid definitively of this problem since errors pop up again after I removed them.
Any ideas?
EDIT
Using Ubuntu 9.1
Check your pythonpath. You need to include the parent directory of django, usually Lib/site-packages.
I'm on Mac OS X, but all I had to do was to add:
/Library/Python/2.6/site-packages
to my System PYTHONPATH (Found in: Preferences > Pydev > Interpreter - Python)
The equivalent for Ubuntu 10.04 would be:
/usr/lib/pymodules/python2.6
I'm guessing that the equivalent for Ubuntu 9.x could be:
/usr/lib/python2.6/site-packages
If it isn't and you're tired of looking, just upgrade to Ubuntu 10.04 and you should be fine.
I was able to get rid of the import errors executing this cmd
sudo ln -s /usr/lib/pymodules/ /usr/lib/python2.6/pymodules
I was lucky, testing different things I could resolve it but I'm not sure why I have to do this and how I could avoid doing it.
"Unresolved imports" occur when Eclipse/Pydev does not know what you want to import. Check your Pydev settings in the Eclipse preferences > Interpreter Python. Your site-packages folder and things you want to import should be there in the Pythonpath.
Also see http://pydev.org/manual_101_interpreter.html
Even if Lib/site-packages is added to the PYTHONPATH, this problem may happen when using modules from egg packages installed using easy_install. The problem with those, and that might actually depend on the easy_install version, is that by default they don't get installed directly inside Lib/site-packages but rather under a folder containing the full package name and having the .egg suffix. For example: Lib/site-packages/django_celery-3.0.23-py2.7.egg
Each module coming from packages installed as above needs an individual entry in PYTHONPATH. If the packages are installed after PyDev has been installed on the system, the system PYTHONPATH needs to be updated in PyDev. That is done automatically by going to Window -> Preferences -> PyDev -> Select your intepreter -> in the python interpreters panel remove and re-add your current python interpreter (usually, this should be the python executable from the system). Then a PyDev restart (File -> Restart) should solve the "Unresolved import" errors.
If you are facing problem of unused imports then I must say use Eclipse as a IDE as it is providing functionality to remove unused imports by pressing keys ctr+shift+O. Or In Eclipse there is plugin also available which is doing the same thing automatically when you save your code.You can get that plugin from eclipse plugin site easily and free of cost.
In my case of the modules were dependent on setuptools-14.3.1 , which was causing all these problems. After installation of setuptools-14.3.1 rest of the modules automatically resolved.
I was having a single import error when working in PyDEV in eclipse.
I was importing it like
from xyz_module import abc
So I clicked "Ctl + 1" and "select Unresolved import error" in eclipse and it created a class
file in a python file.
It turned out that I had created an application called "xyz_module" and a "xyz_module.py" file (with the same name that is) and this was causing an import error. I changed the python file to a new name and this resolved the error.
I had the same error and none of the answers worked for me as there was no PYTHONPATH options under Window -> Preferences -> PyDev -> Python.
Instead I added PYTHONPATH setting in the menu:
Project -> PyDev-PYTHONPATH -> External Libraries -> Add Source folder
I added the path of site-packages like
/home/Documents/hcx/venv/lib/python3.5/site-packages
Now PyDev stopped complaining.

Categories

Resources