I have a very simple python program that I am trying to run from PyCharm
from collections import Counter
import my_ds
my_list = my_ds.names
a = Counter(my_list)
print(a)
I am getting the following error.
from collections import Counter
ImportError: cannot import name 'Counter'
However I am able to run this program using the same python interpreter from the commandline. What could be the reason for this?
I am using python 3.4
Make sure you selected a python version newer or equal as 2.7.
In fact, Counter is not available in python versions earlier than 2.7.
Go to settings, project, project interpreter.
I am working also with pycharm latest 2018 version.
For me it was something else, i created by mistake or not (we can argue on that) a file named collections.py, once i created this file with this name pycharm was unable to import the real (one that comes with python 3.6 or anaconda) package.
Only after renaming the file from collections.py to something else all worked.
I also filed this as a bug https://youtrack.jetbrains.com/issue/PY-29254
Related
I'm running Python code in VS Code (1.28.2, with the Python, and Python Extension Pack extensions) and am using the 'Run Selection/Line in Python Terminal' (Shift+Enter) functionality to just run selected code in the Python terminal.
This has always worked well, but today I'm getting a new line added between each line of code in the terminal, i.e. if I ran:
import heapq
import pickle
the output in the terminal would be:
>>>import heapq
>>>
>>>import pickle
At first, this just seems like an annoyance, but any for loops or functions now come out with an indentation error...so essentially I can't successfully run any code.
I've tried re-installing VS Code as well as installing an older version of VS Code but all give the same problem.
It's so odd because it was working fine and then all of a sudden it changed. The only thing I can think of that has possibly changed is I installed the JSON Tools extension, but I don't believe this would change anything within Python (and I've tried uninstalling this, and not loading it again when reinstalling VS Code from scratch)
Any help would be greatly appreciated!
It's a bug that will be fixed in the 2018.9.1 release of the Python extension.
I'm getting the error
Traceback (most recent call last):
File "ghs.py", line 1, in <module>
import stdio
ImportError: No module named stdio
When I try to run my script. I can run my script on other machines just fine. I have installed python using homebrew. And I've tried everything I can think of to get it to recognize my modules! I've uninstalled and reinstalled using brew. I've tried changing the path (though I don't fully understand this). I get no issues using brew doctor.
I've also tried using a python virtual environment but to no avail.
Any ideas on how to fix this issue or else 'start fresh' from a fresh version of python?
When you import a module, Python looks for it at the directory your code is, and the directory in which the built-in libraries are (C:\Users\pc\AppData\Local\Programs\Python\Python35-32\Lib in my case, I'm using Windows 10 and Python 3.5). If it can't find it, it raises ImportError.
I couldn't find a module named stdio in my computer. I also know some C++ and as far as I know, stdio is the library for inputs and outputs(prints). In python, there is no need to import such a library.
You can use try,except statement to test if your code works without importing the module like this.
try:
import stdio
except:
#rest of your code goes here
You will need to indent your whole code however this can be done easily with a text editor in which you can edit more than one line at a time.
I recently wrote a Python 2.7 script (using PyDev on Eclipse) that took advantage of the built-in ConfigParser module, and the script works perfectly. But when I exported it and sent it to a colleague, he could not get it to work. He keeps getting an "unresolved import: ConfigParser" error even though we are using the exact same settings. This isn't supposed to happen as ConfigParser is built-in.
I've Googled everywhere but could not seem to find any working solution. Any help would be appreciated.
ConfigParser was renamed to configparser in python 3. Chances are he's using 3 and cannot find the old py2 name.
You can use:
try:
import configparser as ConfigParser
except ImportError:
import ConfigParser
To see what's happening it may be nice comparing on both computers which sys.path is being used (i.e.: put at the start of the module being run the code below and compare the output in each case):
import sys
print '\n'.join(sorted(sys.path))
Now, if the error is not when running the code (i.e.: it runs fine and you get no exceptions), and he gets the error only in PyDev, probably the interpreter configuration in his side is not correct and one of the paths printed through the command above is not being added to the PYTHONPATH (it could be that he's on a virtual env and didn't add the paths to the original /Lib or has added some path that shouldn't be there -- or even has some ConfigParser module somewhere else which is conflicting with the one from the Python standard library).
I've got a copy of Lion with all the dev tools installed. Both Python (2.7) and Ruby (1.8) are running just fine. I've installed the Natural Language Tool Kit for Python and tried it out in the Python interpreter and it works
import nltk
>>true
So that works. I've also installed the RubyPython gem, and it seems to work too, but it can't find the nltk module. It's possible I'm doing something wrong. In irb:
require "RubyGems"
require "rubypython" #both true
RubyPython.start # true
n = RubyPython.import "nltk" # RubyPython::PythonError: ImportError: No module named nltk
c = RubyPython.import "cPickle" # works!
RubyPython.stop
I can't figure this one out. The PythonError seems to indicate to me (because it's just a call to the direct C APIs) that the nltk module can't be found by any form of python. But the interpreter finds it just fine. RubyPython, however, cannot.
I've also tried forcing RubyPython to use python2.7 but no change.
What am I missing?
It's simple! For some reason, RubyPython was looking in the wrong place for my Python modules. This was verified by importing sys in both the RubyPython script and in Python, and comparing sys.path. I ended up fixing it by taking the path list of sys in the Ruby script and adding what was missing from the pure Python's path. Then I could load NLTK.
I'm using simplejson to get data from the New York Time API. It works when I run the file through the terminal with the command "python test.py" but not when I run through TextMate using command + R. I'm running the exact same file. Why is this?
I am running Snow Leopard 10.6.4, TextMate 1.5.10, and Python 2.6.4.
Edit: Sorry for forgetting to include this: by "doesn't work," I mean it says "No module named simplejson". I also noticed that this happens for PyMongo as well ("No module named pymongo").
What doesn't work? You should provide more information like error messages and what-not. However, I assume that the version of python is different, and simplejson isn't on your PYTHONPATH when launched from textmate.
Just so you know, simplejson was incorporated into the Python 2.6 distribution's standard library as json. So if you don't feel like wrestling with the import problem, try simply changing all your references to simplejson to json instead.
But, as suggested, this is going to turn out to be a PythonPath issue. Run these lines in the Python interpreter and from TextMate and compare the results.
import sys
print sys.path
To find out where simplejson is installed (if you don't know), do this in the Python interpreter:
import simplejson
print simplejson.__file__
If you want/need to set PYTHONPATH manually for TextMate, you can do that by adding it under Preferences > Advanced > Shell Variables.