Error adding 'import lettuce' on PyCharm - python

I have a problem with Python and lettuce.
I already install the pip and lettuce in Python 2.7, and I'm trying to create a .features file and a .py file for the steps. The problem is that when I put the line from lettuce import * I have an error that says:
"Unresolved reference lettuce..." and then: "this inspection detects names that should resolved but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items".
I really donĀ“t know how to fix this problem. Can anyone can help me please?

I just found the problem. What I did, just in case anyone has the same problem is:
In my 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
Just in case you are not able to add an other module (that happened to me as well) you can change the project interpreter.
Hope this works!

Related

How to solve ModuleNotFoundError: No module named 'string' error in Python 3?

I was trying to add the Strings library into the Robot Framework Interpreter folder in PyCharm, where first it showed me error
Command errored out with exit status: 1
So, I googled out this issue first, and this link suggested me to delete 'strings.py' file from the libraries. I did so and now nothing is working.
Anything I do now, it shows error of "ModuleNotFoundError: No module named 'string'".
I could not even install string using
pip install strings
command.
Whatever I try to do with pip, now shows this error.
Can anyone please suggest me the solution for this?
I am using Python version 3.10.
Several notes:
The library in question is 9 years old, is not maintained, is known by the author to be buggy, was written as a joke, and does not contain anything useful. There is no good reason why you should be trying to install it for your project. If you think you need it for something, then you have some other misconception that needs to be cleared up.
The link you found did not tell you to delete strings.py from "the libraries". It said something about deleting string.py - notice, no s at the end - from a local project folder. The reason for this is because that name conflicts with the library name. The page author's own source file, named string.py, sought to import the standard library file string.py (as it clearly says import string in the screen shot), but it cannot - because it finds itself first. This is a common problem for new Python users.
DO NOT EVER MANUALLY EDIT THE CONTENTS OF YOUR INSTALLATION DIRECTORY ON THE ADVICE OF SOME RANDOM WEB PAGE.
Ideally, don't ever do it at all. That content is not intended to be touched. Installers exist for a reason. If for some incredibly specific reason you feel the need to do this, make sure you have backups of everything and that you are 100% sure you can restore everything to its initial state if anything goes wrong.
The person writing that web page was incorrect. The installation error had nothing to do with the string.py file.
The actual cause of the problem is that the package is broken and cannot be installed properly on anyone else's machine. Again, this is no big loss as there is no use for the package anyway.
The reason it is broken is that the setup script for the package tries to import the code that's being installed, in order to get version and author information. This seems to work locally, but fails for everyone else.
To reiterate: the person writing that article wrote nonsense. (I'm not surprised; the page formatting is awful and the grammar isn't particularly great either.) Looking further, it appears that the entire website is authored by the one person, who is clearly just trying to self-promote (with a Youtube channel as well) while lacking the necessary expertise. Browsing around the rest of the site a bit, I see articles that are pedantic and not very insightful, and occasionally inaccurate - but all very SEO optimized.
I recommend ignoring that website entirely.
To reiterate: the string module comes with your Python. You cannot reinstall it with pip - not with the strings package you found, nor any other package. Your options are:
Find the correct string.py contents (possibly from a backup, assuming you thought to make a backup before deleting something from an installation directory) and restore them. This is the official repository for the reference implementation of Python. You might be able to find it in there somewhere. I don't recommend trying. There is a lot to go through and it is possible to damage things further.
Reinstall Python completely.
The error that you are getting is because you deleted the string.py file. There is no string module to be imported from PyPi. This is why pip install string doesn't work. Restore the deleted file as it is not the cause of your problem.
If you try to install Strings library it will fail because you are using Python 3.10 and the Strings library that you want to import and install is quite old and not supported for this Python version. Therefore you get the ModuleNotFoundError: No module named 'strings' that you see in the link that you attached.
For the setup.py file from the library I see that it recommends using Python 3.3, so I will recommend you to use that version of Python if you want to use this specific library.
Please note that the link that you provided is probably in case that you have created your own string.py file.
I solved this question by deleting any __pycache__ folder in the project directory.

Print [Python] seen as a keyword in Pycharm

I'm using Pycharm (2019.1.2 x64) from quite a while and I never changed the standard settings.
The standard settings diplay keywords (def, class, if) in orange, pressing tab will auto complete the keyword and add a space, build-in functions (sum(), map(), print()) are displayed in purple and pressing tab will auto complete the function name, add brackets and move the cursor inside the brackets.
This is the desired behavior and it's what I get when opening any project in Pycharm except one.
In a single project this behavior suddently changed, I did nothing but writing and running code.
I noticed because tab suddently started indenting by 8 spaces instead of 4, no big deal I changed it back to 4 in the bottom of the IDE, but I also noticed that print was seen as a keyword (so orange with no brackets) instead as a built-in function, just like in Python2 (I never used it and the project's interpreter was Python 3.7). from __future__ import print_function would turn print into a function again but it's Python2 stuff.
I fixed it by creating a new virtual environment and setting up the interpreter again, so my question is:
What causes this unexpected behavior?
I would like to avoid this when working on my most "serious" projects. Thanks all.
NEW ANSWER
Well, this happened to me just yesterday while I was working on my tkinter project. It showed tkinter in from tkinter import * "Module not supported in Python 2.7", even though I was using Python 3.8.
Solution One
To fix this issue, I just simply went to
File > Settings > Appearance and Behavior > System Settings > Updates
And updated PyCharm to the latest one. It worked for me.
Solution Two
Another method I found out is to go to
Press Shift Key Twice > Typing "Restore Default Settings"
Doing this will also restore all the settings partially and fixes the problem for me.
Why Did This Happen?
This is due to a bug in older versions of PyCharm (probably). I got this bug while testing some Google API that was in Python 2.7. If you ever opened a project containing Python 2.7 Interpreter and opened another project that is in Python 3 and above, PyCharm thinks that the code that is written is in Python 2.7 and just formats the guide according to it. Some example can be "f" in print(f"{some_variable_here}") being shown as "Not Supported In Python 2.7". This is just my theory I may be completely wrong here.
OLD ANSWER
Are You Using macOS? if yes, macOS comes with Python 2 preinstalled. So probably this may be a bug. I suggest going to the bottom right corner and there would be written "Python (Version)". If it is something other than 3 or above (like 2), click on it, and go to "Interpreter Settings" and there, from the drop down menu, select the other Python interpreter that is installed on your device.
If this does not work, then it may be a theme bug, or something to do with PyCharm itself (probably). Reinstalling and clearing the cache should probably work.

Pydev on Eclipse running wrong (deleted) file which shadowed numpy library

I just committed a major noob blunder while playing around with basic functionalities of NumPy. I created a small file called "numpy.py" just so that play around with problems related to numpy. I could also later come back for reference - and the name would have helped. Obvious error :-
import numpy
File "C:\Users\USERNAME\workspace\StackOverflow\python\numpy.py", line 25, in <module>
AttributeError: 'module' object has no attribute 'random'
I realized the problem - python was considering my current code as actual numpy and looking for random. I deleted that file and created a new one.
And the problem persists. This image explains it better than my words would.
Now there's a completely new file with a different name - but still it's looking for some methods in the deleted file.
I've already tried:
Cleaning the Project
Restarting Eclipse
Run another .py (successfully) and then this one again
Removed all the conflicting files, created a totally new file. Still same. If the program contains import numpy, it would give same error.
I'd like to know (1) What actually happened wrong here? Why is eclipse so confused (2) How to resolve? (3) What are the best practices in such situations?
I'm the OP. Found a solution after a few minutes of posting this question.
Browsed to the folder containing the code. I noticed that there was a .pyc file for the numpy.pyc I had created earlier. Deleted that one and things are working file now.
However I'd still like to know how to avoid situations like this or resolve directly through eclipse. Though it works now!
This is called an orphaned .pyc file. Eclipse/PyDev now has a setting to automatically handle these (as of Oxygen / v4.7):
PyDev > Builders > "How to handle .pyc/$py.class deletion?" :
Delete any orphaned .pyc file
Knowing Eclipse, presumably that action only gets triggered when you or it refreshes or restarts the workspace, or triggers a rebuild. There are some switches to try to make that automatic too. Or Eclipse > Restart if all else fails.

Accessing GRASS modules through python IDE

I am trying to access GRASS modules and functions from within a python IDE. (I am using WingIDE)
My googling search tells me it can be done (i think) but I cant find any clear instructions on how to do it. Is anyone able to give me some clear instructions on what I need to do?
Simply typing this line of code does not work. I dont think it is locating the grass modules. I have arcgis 10.1
import sys
import grass.script as grass
The ultimate goal is to be able to use both the argis toolbox and GRASS tools.
Anyway hopefully it is something easy and obvious to someone out there,
Any help would be great,
Cheers,
Dan
Wing IDE may be picking up another Python installation as the default, rather than the one where you have grass installed. You can check that in Show Python Environment in the Source menu, and configure it in Project Properties in the Project menu (or Configure Python dialog in Wing 101). If for some reason you didn't install grass into Python but have it in a separate place, you may need to configure Python Path as well or instead.
Once that's done, the script should be able to run in the debugger and successfully import the module (or you can also do this in the Python Shell).
If this doesn't help please let me know or email support#wingware.com.
Beyond #Wingware's important point, and presuming that your "grass.script" is a python module, your PYTHONPATH needs to include the directory that contains "grass.script" for the python interpreter to be able to find/import the desired script. You should be able to set this via the IDE or from the OS prior to starting the IDE.

PyDev bugs with imports

I am using PyDev/Eclipse for several monthes and I get ever and ever the same bugs with imports:
PyDev underline in red an import and say Unresolved import xxx ; Found at yyy. When I click on yyy eclispe find and open the implementation of the module.
(PyDev just inform me that it can't find the module xxx and in the same message that it can find it !)
The module xxx is in the PYTHONPATH of eclipse. When I "explore" the interpreter of the project, I can find it without any problems.
When I try to execute (from eclipse), I don't get any error and it works fine.
Sometimes, the error message will stay for several days and will disappear. Sometimes, it won't. I've tried to refresh the projects but it has not impact on that.
Somtimes, it works well on a project and I can use autocompletion and it don't work in another project (same interpreter) ...
I just can't understand what is happenning ?
So far, I have ignored these bugs because everything was fully fonctionnal but sometimes, it is a bit disturbing to have red markers "errors" when you are working.
Did you find a way to avoid these bugs in PyDev ? Is it "normal" ? Is there a way to force PyDev to "refresh" ?
Thank you.
References :
python 2.4.4 (built from sources)
PyDev v 1.6.0 2010071813
This can happen if new modules are not cached by PyDev. For example, on my new laptop I first set up PyDev/Eclipse and later installed the Django package. That's why Django imports were marked as unresolved. You can refresh it using Pydev > Interpreter - Python > Libraries > Apply. Select the interpreter you want to "restore" (they could have chosen a better word) and click OK. PyDev will then reparse all installed modules.
I'm using the nightly version of PyDev, but any 1.6.x version should work correctly. If that doesn't help, remove the interpreter configure and create a new one.
You can try refreshing your PYTHONPATH in Preferences > Pydev > Interpreter - Python and selecting AutoConfig for your interpreter, then manually choosing the libraries for your PYTHONPATH.
This is pretty radical solution though. Making an insignificant change (like adding a space) and saving file should work in most cases. If not, you can also try temporary delete and then re-add the imports in file that is causing problems.
Just in case anyone else runs into this thread but above answers don't solve the issue, make sure that your script does not have the same name as the library that you are trying to import.

Categories