I had successfully installed nltk from this site. And just to validate i am able to import it from the terminal. But when i execute my python script from the Spyder it gives me following error in Spyders terminal
File "/Prateek/Python/RC_ISSUES/algorithm_RC.py", line 9, in <module>
import nltk
ImportError: No module named nltk
Below output is from the terminal
I know there might be similar questions but i thought it is different from rest of the other questions
When you execute a python script, the operating system is looking for the interpreter as specified on the first line of the script, which most of the time is:
#!/usr/bin/python
On Mac OS X, this is the python as distributed with the system when you installed it, which is the one distributed with the system. It is usually very likely to be the one that has the older compilation date:
2.7.10 (default, Jun 1 2015, 09:45:55) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
If you do type python in your shell, you're very likely to see another path to that interpreter, e.g. if you installed the brew version of python:
% type python
python is /usr/local/bin/python
So you have two ways around it, either you explicitly launch your script with python:
python algorithm_RC.py
in doubt, use the full path you found out with type:
/usr/local/bin/python algorithm_RC.py
or, you can change your script first line with:
#!/usr/bin/env python
which will use the same python as the one you're reaching from your shell. You can also use the full path to your manually installed python, by making that line:
#!/usr/local/bin/python
or whatever the type command gave. But I would advise you against that, as the /usr/bin/env solution is more flexible and makes sure you're using in both cases the same python from the shell and within the script.
Finally, you can also use the system's python by calling explicitly easy_install from /usr/bin:
sudo /usr/bin/pip nltk
And if you don't have pip there, then you'll have to install it first:
sudo /usr/bin/easy_install pip
HTH
try 1:
>>>import nltk
and hit enter and now try
>>>nltk.download()
Related
Problem having two versions of python on my Mac and them interacting. I have the following python locations
python is /anaconda3/envs/fenicsproject/bin/python
python is /usr/bin/python
and when I try to run a script. I get the following error:
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
I have googled some solutions and have found some posts saying I should try
env PYTHON_CONFIGURE_OPTS="--enable-framework" #or
env PYTHON_CONFIGURE_OPTS="--enable-shared"
This is they type of code I try to run
#Import packages
import dolfin as dl
I installed the env fenics by following the directions here
The google search possibly found Homebrew + Python on mac os x 10.8: Fatal Python error: PyThreadState_Get: no current thread importing mapnik however I was unable to find the library that links to a wrong version of python using otool.
I also found https://github.com/enthought/mayavi/issues/552 which suggests pinning to a different version of python.
Based on the install notes for hippy, https://hippylib.readthedocs.io/en/latest/installation.html then where they say conda create -n fenicproject ... you need to substitute the following:
conda create -n fenicsproject python==3.5.1
conda install -n fenicsproject -c conda-forge fenics==2017.2.0 \
mpi4py matplotlib scipy sympy==1.1.1 jupyter
After this, python -c 'import dolfin' no longer fails. It might also be possible to use a later version of python (I only tried 3.5.1).
What OS are you using? That will largely determine the specifics of how to go about correcting this issue, but the key here is which Python version the system path points to and what Python version your IDE is pointing to.
What you largely want to avoid is a situation where you are running Python scripts via your native system Python (2, likely).
Check which version of Python your IDE is using (from: How do I check what version of Python is running my script?):
import sys
print(sys.version)
Is the first number a 2? Did you want to use Python 2?
Next, let's check what version your system currently defaults to. If Ubuntu/Linux, use:
python -V
Is this expected? If not, you may need to change your system environmental variables to point to the correct Python version. The solution to this is OS dependent. If Windows, search "Edit Environmental Variables for Your Account" -> "Environmental Variables" -> "Path" , be sure it either points to Anaconda or the correct Python version; if Ubuntu/Linux, check your .bashrc file:
gedit ~/.bashrc
to see if the system points to the correct Python variable. If using a Mac, I formally apologize.
I recently installed python 3.3.2
Im trying to install NumPy, when I call import NumPy from the command line I get the following error:
import numpy
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named 'numpy'
From what I've read it may have to do with having multiple version of Python installed.
The only problem is when I try run the Which Python command I get another error?
which python
File "<console>", line 1
which python
^
SyntaxError: invalid syntax
Any Ideas?
Thanks
What you have to do depends on the operating system you are using. I'me assuming you are using Linux. If you are using Linux with some kind of package manager, you should use a numpy package that is expressly for the python version you want to us it with. The same goes if you are using windows.
Note that you can have different versions of python installed, but in general only one is symlinked to python. Running ls -l /usr/local/bin/python should tell you what is the default version on your machine. If you have multiple versions, there should also be programs named python2 and python3. Using ls -l /usr/local/bin/python2 and ls -l /usr/local/bin/python3 will show you the which versions you really have.
On linux and other UNIX-like systems, you can usually find Python's files in a subdirectory of /usr/local/lib. For python 2.7 this will be /usr/local/lib/python2.7, for 3.2 it will be /usr/local/lib/python3.3. These directories will have a subdirectory site-packages. In those site-packages you should look for a subdirectory numpy. If you find /usr/local/lib/python2.7/site-packages/numpy but not /usr/local/lib/python3.3/site-packages/numpy, then numpy was not installed for 3.2.
Edit: In Windows, Python usually installs itself in the root of the C: drive, like C:\Python27 or C:\Python33. I don't have a windows machine handy, but there should be a site-packages subdirectory in both of them as well. Look for the numpy subdirectory in there.
For windows, you can find precompiled binaries for mumpy here. You just need to know is you have a 32 bit (win32) or 64 bit version (amd64) of windows. E.g. for python 3.3 and 2 32-bit windows I would suggest numpy-MKL-1.7.1.win32-py3.3.exe.
Type open a command prompt and type python It will then tell you what version you are running as it opens the interactive python editor.
Otherwise get your numpy from here These are compiled binaries and should be the most straightforward to install for a windows user
I have a Django (1.5.1) site running on Python 2.7.3 in a virtualenv at PythonAnywhere. As far as I remember, everything used to work fine. Lately, although I haven't changed anything except some Django code, I get the following message when I run pip:
(venv)11:34 ~ $ pip
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Traceback (most recent call last):
File "/*~*//venv/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
File "/*~*//venv/lib/python2.7/site-packages/distribute-0.6.34-py2.7.egg/pkg_resources.py", line 16, in <module>
import sys, os, zipimport, time, re, imp, types
ImportError: No module named time
Needless to say, pip does not work at all after producing the above error.
When I run python with the virtualenv activated, I again get the following error:
(venv)11:34 ~ $ python
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Python 2.7.3 (default, Apr 29 2013, 15:12:04)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "/*~*//.pythonstartup.py", line 1, in <module>
import rlcompleter
ImportError: No module named rlcompleter
>>>
But, thereafter, the Python console appears to be in working order.
Without the virtualenv activated, python runs fine without any errors.
All was well a couple weeks ago when I last ran pip and installed some packages, but now, pip is not working, although all of my Django site's features run just fine. I appreciate any ideas that could get pip to work again.
There's definitely something fishy going on if it can't find module time - as far as I'm aware that module is actually built in to the Python binary itself so it's not as if there's a time.py or time.so file which could have been deleted. It looks as if either Python in your virtualenv has become broken somehow, or there's something about the environment that's messing it up.
Setting a dubious PYTHONHOME variable can cause Python all sorts of trouble as it can't find necessary files, but I think in this case it's the fact that the virtualenv has become broken. In fact, I just tried an old virtualenv on PythonAnywhere myself and got the same issue. It looks as if the symlinks that virtualenv creates have become broken as the result of an upgrade, possibly the recent upgrade from Debian to Ubuntu.
If at all possible I would simply re-create a new virtualenv and run things from there. In principle you could use pip to write a requirements file so you can create a new virtualenv with exactly the same versions of the code, but the problem is that you can't run pip to create this file, QED. There's probably a clever way to use the system Python to run pip and get the dependencies from your virtualenv, but it's going to be tricky - it's not designed to work that way.
Alternatively you could contact the PA devs - I know that some users did have problems with their virtualenvs around the time of the upgrade and they may have cunning scripts which can fix the problem. Even if you just build a new virtualenv and use it, I would contact them and make sure they know about this issue so they're aware of it for future upgrades.
I had this happen to me this afternoon. I upgraded from Mint 14 to Mint 15, and it appears that the system python on the former is 2.7.3 and the latter uses 2.7.4. I fixed this by deleting my virtualenv (which used python 2.7.3) and then recreated it using python 2.7.4.
Check my quick screencast. In it, I show that:
the python environment is broken
recreating the python env makes it work
I get this error this day.Because i use virtualenv,and enter this,but ,my python script still contains this
#!/usr/bin/python2.6
delete this sentence will ok
I'm relatively new to programming and have searched 'til my fingertips were blue, but can't seem to find a solution to the problem I'm having.
I have homebrew-installed versions of Python 2 and Python 3 on OSX and I can't seem to get the proper PATH/PYTHONPATH in my .bash_profile in order to be able to import modules properly in both versions in IDLE. I can, however, import modules when running Python 2 or Python 3 directly in a shell window. I am launching IDLE via terminal so it should properly initialize the paths.
Here is my .bash_profile:
export PATH=/bin:/usr/local/bin:$PATH
export PYTHONPATH=/Users/maverett/Documents/PyModules:/Users/maverett/Dropbox/matrix/:$PYTHONPATH
Here's what happens in all four cases, using numpy as an example module.
Importing when running python2 in terminal works:
$ python2
Python 2.7.5 (default, Jun 28 2013, 19:06:25)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
Launching IDLE for Python 2 from terminal, I can also import numpy. However, when I compare sys.path in IDLE vs sys.path in terminal, they are different. I compared the lists to generate the differences and found:
>>> InIdleNotInTerm
['/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/bin']
>>> InTermNotInIdle
[]
So there is one extra directory in the IDLE path when running Python 2.
The story is quite different for Python 3.
$ python3
Python 3.3.2 (default, Jul 1 2013, 10:53:26)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
However, if I launch IDLE for Python 3 (by typing idle3 in terminal) and then try to import numpy, I get
>>> import numpy as np
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import numpy as np
ImportError: No module named 'numpy'
>>>
Again, I compared sys.path in terminal and in IDLE and this time there are major differences:
>>> pp(InIdleNotInTerm)
['/Library/Frameworks/Python.framework/Versions/3.3/bin',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python33.zip',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages']
>>> pp(InTermNotInIdle)
['/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/distribute-0.6.45-py3.3.egg',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pip-1.3.1-py3.3.egg',
'/usr/local/lib/python3.3/site-packages/distribute-0.6.45-py3.3.egg',
'/usr/local/lib/python3.3/site-packages/pip-1.3.1-py3.3.egg',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python33.zip',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/plat-darwin',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info',
'/usr/local/lib/python3.3/site-packages',
'/usr/local/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info']
Any idea what's going on? A few other things I've tried:
Changing .bash_profile to
export PATH=/bin:/usr/local/bin:$PATH
export PYTHONPATH=/Users/maverett/Documents/PyModules:/Users/maverett/Dropbox/matrix/:/usr/local/lib/python3.3/site-packages/:$PYTHONPATH
results in being able to import modules in Python 3 in terminal and in IDLE, but breaks imports for Python 2 (because it tries to import the Python 3 versions!)
Launching IDLE for Python 3 via terminal from python3.3/site-packages/ results in being able to properly import everything, but I don't want to do this every time I launch Python 3.
Any thoughts or ideas you have would be greatly appreciated! Thanks :)
Ok, here's what I'd like you to do:
Stop using easy_install, if you're still using it for package management. Use pip instead.
$> easy_install pip
Next, get virtual environments.
$> pip install virtualenv
$> mkdir ~/venvs
$> virtualenv ~/venvs/numpy_project --python=python2.7 --no-site-packages
Make sure to pass in a well-named directory to virtualenv. The standard usage is to organize your virtual environments by project, so I named this virtual environment "numpy_project". You should probably come up with a better name. I also told it to use python 2.7, but you can choose to use 3.3 if you want.
Basically, this is going to create a well-insulated bubble for a dedicated copy of python to live for one specific purpose. I also told it to use no-site-packages, as to ensure a clean slate. It makes it easier to get set up for work on this project, which can be done by running:
$> source ~/venvs/numpy_project/bin/activate
This will switch your python environment from the global "main" python to this protected copy. You can then run:
$> pip install numpy
And it will install it just for that copy of python.
Be sure to run
$> pip freeze > requirements.txt
In the root of your project, where your README.md and stuff would go, so that others can simply run:
$> pip install -r PROJECT_ROOT/requirements.txt
And it will grab all the things you've put in your virtual environment (i.e., numpy). If they're using virtual environments as well, you can be certain they have an exact match to your working environment. There should be no overlap or confusion from other packages and versions of python.
Try this and see if you get better results. Remember to never run pip with sudo!
If this works, just uninstall your "global" install of numpy, and just use it in your virtual environments.
I have a program here that I would like to convert to 2.7. This code works well in Python 3.x, however, for my needs it must be 2.7. Could someone 'convert' this to python 2.7 for me? I have heard of a 3to2.py tool but I do know how to get/use it. Anyway, here is the code I have for 3.3.
def compiler(program):
import os, win32com.client, time
os.startfile("C:\\Windows\\System32\\cmd.exe")
time.sleep(2)
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate('C:\\Windows\\System32\\cmd.exe')
setup(program)
shell.SendKeys("py MyCompiling.py.setup("+program+") py2exe\n")
def setup(program):
from distutils.core import setup
import py2exe
setup(console=[program + ".py"])
compiler('test1')
EDIT: When I try to run I get
ImportError: No module named win32com.client
Do I have to install this module seperately? If so, could someone please post the link.
Yes, you must install the library separately. In fact, if you visit the SourceForge page, you will see that there is an entirely different binary available for 2.7. You will want pywin32-218.win32-py2.7.exe if you are using 32-bit Python or pywin32-218.win-amd64-py2.7.exe if you are using 64-bit Python.
You can install it via the GUI interface (which comes up when you try to execute the file), or you can call easy_install on it (if you have setuptools or distribute installed) at the command line:
C:\> C:\Python27\Scripts\easy_install pywin32-218.win32-py2.7.exe
Using easy_install is the only way if you want to install the library in a virtual environment created with virutalenv.