ImportError: No module named twisted.persisted.styles - python

From IDLE, I tried to run a script with a newly installed scrapy 1.0.3.
I'm using a script from a friend whom it worked for (but on Windows, I'm on a Mac).
From the import of scrapy on the first line, I get this error when running the program:
ImportError: No module named twisted.persisted.styles
The whole script, if it's helpful, points to this:
Traceback (most recent call last):
File "/Users/eliasfong/tutorial/tutorial/spiders/medspider.py", line 1, in <module>
import scrapy
File "/Library/Python/2.7/site-packages/scrapy/__init__.py", line 27, in <module>
from . import _monkeypatches
File "/Library/Python/2.7/site-packages/scrapy/_monkeypatches.py", line 20, in <module>
import twisted.persisted.styles # NOQA
ImportError: No module named twisted.persisted.styles
Any suggestions on how to tackle this problem?

Just try to force the update of twisted :
pip install twisted --upgrade
That works for me with python3.4 and Scrapy==1.1.0rc1

Either twisted is installed on your mac (I highly doubt it since it's not a standard library) and for whatever reason the IDE (i'm assuming that's what you mean since you typed "idle") or the terminal you are in doesn't have your updated environment variables, meaning it doesn't understand where your default python libraries are (again I highly doubt it), or you simple do not have twisted installed on your mac. If it's not installed you have a couple of options:
The easiest way to install a python package is through pip.
If that not an option you can try homebrew which is another package manager for macs. It offers an easy way to install packages correctly.
If that still is not an option for you or you simply don't want to attempt that you can download twisted directly from here (the .bz2 since you're on a mac), click on it and it should unzip it for you. Then just run setup.py and it should install it in the correct location on your mac.
If that still doesn't work and you have decent knowledge of unix. Use the "locate" command on the terminal and find out where your dist-packages directory is and put the source for twisted in there directly and then attempt to import twisted in your IDE or in the python interpreter to verify that it is installed.
note: If you're still having problems after it is installed trying restarting your IDE or messing with some setting to make sure your IDE has the right environment and python path. Hope that helps!

It could be related to having installed Python without bzip2. I had the same error and this helped me, see the accepted answer here:
Installing Twisted through pip broken on one server

Had this exact thing on FreeBSD. Solution (as root/sudo):
chmod -R go+rX /usr/local/lib/python2.7/site-packages
Some directory permissions weren't set up right on install.

Related

Trouble importing python packages using PyCharm

I've been trying to install simple packages for python such as numpy and pandas, and while I am able to install the package manually from the command line using pip as soon as I try to import it PyCharm can't find it. I have manually configured the path, with no luck.
PyCharm can also not find the packages I want to install itself and produces a connection timeout, this could be because I am working from a secure network.
Still pretty new to python and software dev in general, I apologise in advance if I'm being stupid, but no other similar threads are solving the issue.
I am operating on windows 7. The error I get is:
C:\Users\james.alexander\PycharmProjects\Test.py\venv1\Scripts\python.exe C:/Users/james.alexander/PycharmProjects/Test.py/testfile.py
Traceback (most recent call last):
File "C:/Users/james.alexander/PycharmProjects/Test.py/testfile.py", line 1, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
I think PyCharm use a virtualenv, installing via pip won't work since it will install the package in your local env.
Check https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html
Try This:
Step 1: Ctrl+Alt+s
Step 2: Look for Project Interpreter Under Project
Step 3: Look for + In the right corner of prompt box.
Step 4: Search and select your required package. And Install button is just below.
First check the Project Interpreter in your pycharm.
File > Settings >Project >Project Interpreter
Check the version of python there (python2 or python3)
And check if you are using any virtual environment
And see what version python you have installed
For python3 you may have to use "pip3 install pkgname"
For python2 use "pip install pkgname"
In PyCharm, you can install modules from within the app. Simply access your preferences, then under 'Project Interpreter', you will find a table containing your installed modules. To install a new one, click the + symbol on the bottom - left, then search for the appropriate package, e.g. numpy. Once you've found it, simply click 'Install Package'. The module is now installed and ready to use!
EDIT: I've just seen that someone else has already posted this answer, sorry about that :)

Python packages are imported on terminal but not on IDLE

So I am new in Python. I downloaded Anaconda (and Homebrew) and using Terminal on my Mac I confirmed that, say, Numpy is installed:
pip install numpy
to get as a result
Requirement already satisfied: numpy in ./anaconda/lib/python3.6/site-packages
Then opening Python within Terminal I can indeed import the package. The story is not the same once I open IDLE. So when I open it and try to import, say, Numpy I get the following message:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
I guess there is something I do not know, I assume it is a common rookie problem. Can you help me with this?
Since your IDLE is using Python 3 you should use
pip3 install numpy
to get it installed for the correct Python. If you want to use your default installed Python (2.x) instead, IDLE is located at /usr/bin/. From your terminal you can open it with
/usr/bin/idle
this should have the numpy you installed with pip install numpy
See when you are working with Anaconda it creates its own virtual environment.
Now, If you are new in python on MAC then you might not familiar with Virtual Environment.
I suggest you to download python from the official website www.python.org
or Click here to directly download Python 3.6.2 on your MAC.
Then, Download Pycharm(The Best IDE for Python)
Download the community version for beginner and it is also free.
Pycharm Community Version -> Click Here
And do whatever you want and also must read about Virtualenv
According to me, Pycharm is better then Anaconda's Jupyter Notebook
Wish you good luck and show your creativity in python ! !
Erm... well it appears as though - since you have 2 versions on your computer - that it might be because it imported it into the other file path directory. Now, I've never worked with Mac, but I think if you just specify which pip you want to download from, it might work.
For example, on Windows:
C:\Python34\Scripts\pip.exe install numpy
or if you were doing it for 3.6, you would follow the path to the folder, find pip and install. So, I suggest to install the normal IDLE before any other platform built upon it just because it is easier to import modules and is not as bad to break like yours has.
There can be two issues -
You are using python 2 as a kernel for IDLE, since numpy is installed for python3.6. This will raise an error.
The issue is with anaconda's configuration with IDLE. Anaconda installs numpy in ./anaconda/lib/python3.6/site-packages. If IDLE uses systems default python instead of anaconda's. You will not be able to import numpy.
You can try running this snippet -
import sys
print('\n'.join(sys.path))
to track the location of python exactly.
One of the workaround that I can think of is -
create a virtual environment using anaconda. Something like
conda create -n py352 python=3.5.2 anaconda
and then fire IDLE from your terminal. Though I am not entirely sure, if this works for mac. If it doesn't, let me know the output of -
import sys
print('\n'.join(sys.path))

Python - problem in importing new module - libgmail

I downloaded Python module libgmail from sourceforge and extracted all the files in the archive. The archive had setup.py, so I went to that directory in command prompt and did
setup.py install
I am getting the following error message
I:\libgmail-0.1.11>setup.py install
Traceback (most recent call last):
File "I:\libgmail-0.1.11\setup.py", line 7, in ?
import libgmail
File "I:\libgmail-0.1.11\libgmail.py", line 36, in ?
import mechanize as ClientCookie
ImportError: No module named mechanize
This may be trivial, but I am new to python. So plz guide what to do.
please note, I am using python 2.4 and using Windows-XP.
Thank you
MicroKernel
I think this lib depends on this one:
http://wwwsearch.sourceforge.net/mechanize/
Try installing it first.
You need to download and install the module called mechanize. Depending on your operating system (ie. Linux), your package manager probably has something for this, otherwise you will need to google it, and follow it's installation instructions.
easy_install mechanize
If this doesn't work, you need to fix your PATH environment variable to include path to your python installation directory\scripts. easy_install will save you a lot of time in future.
P.S.: Python 2.4 is 6 years old, you should really consider at least 2.6.

ImportError: No module named Foundation

I am trying to follow the instructions for the accepted answer to "PyObjC development with Xcode 3.2". I will repost them here since I don't have enough rep to comment on the actual question:
Here's what I have done to get PyObjC working in Snow Leopard:
Using the Finder, I went to Go > Connect to Server... and connected to http://svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode/ as a guest.
I then made a folder called Xcode on my local system at ~Library/Application Support/Developer/Shared/Xcode/. (You may already have this folder, but I hadn't customized anything for myself yet).
I copied the File Templates folder from the red-bean server into my new Xcode folder.
Copied the Project Templates folder to some other place, for example, the Desktop.
Using the Terminal, navigated to the temporary Project Templates folder on my Desktop and ran this command to "build" the template.:
$ cd ~/Desktop/Project\ Templates/
$ ./project-tool.py -k -v --template ~/Desktop/Project\ Templates/Cocoa-Python\ Application/CocoaApp.xcodeproj/TemplateInfo.plist Cocoa-Python\ Application ~/Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/Cocoa-Python\ Application
When I try to run the line that starts with ./project-tool.py, I get the following error in Terminal:
Traceback (most recent call last):
File "./project-tool.py", line 22, in <module>
from Foundation import NSDictionary
ImportError: No module named Foundation
I am running Snow Leopard and have installed Xcode 3.2.1 and have read that this module should already be installed and working. I've read that you can test if the PyObjC modules are working by running >>> import objc in the Python command-line. When I run this, I get:
>>> import objc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named objc
Could anyone help me dispel this error? It seems like I should be able to do all of this automatically with my Snow Leopard installation, but I can't.
I had the same problem. Mine was caused I think by using homebrew to install my own Python to tinker with.
Because I was worried about mixing python versions, rather than creating the link as described above, I installed a new pyobjc using:
$ pip install pyobjc
For interest, from (http://pythonhosted.org/pyobjc/)
The PyObjC project aims to provide a bridge between the Python and Objective-C programming languages.
Okay, it turned out that, amending mjv's answer, I was able to get it working by typing
export PYTHONPATH="/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/PyObjC/"
before executing the ./project-tool.py line. I still find it ridiculous that I had to do this and if anyone can see why, I would be delighted to know.
Doing this also got the
>>> import objc
line working.
It's because PyObjC is there :
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC
Edit :
I found how to make "import objc" work, just :
export PYTHONPATH="/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/"
It will add all the directories to the python path (sys.path)
for python 2.7
export PYTHONPATH="/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/"
One of two things:
Either the Fundation module doesn't exists
Or Python interpretor doesn't know when to find this file
Python looks for modules in the PythonPath
See this SO question for more details on how Python Path is created etc.
Run python -v to trace import statements, this work for interactive mode too.
I could access a Python installation with Foundation on my OSX by running /usr/bin/python file-to-run.py
remove your python or remove site-packages/Foundation | site-packages/foundation
pip3 install pyobjc
the name Foundation is in conflict with https://pypi.org/project/foundation/
I found the foundation folder in /usr/local/lib/python3.9/site-packages/ next to the AppKit folder. After renaming it to Foundation (with uppercase F), the import worked. The Filesystem is not case-sensitive but it seems some part of the import implementation is.
Saw it mentioned in another comment and I too ran into this problem due to installing Python via homebrew. My pyobjc installation wound up going to the Python homebrew installation, yet my pythonpath was linked to the Python that comes bundled with macOS, so there was this big disconnect and I had no luck getting pythonpath re-routed in .zshrc or .zprofile.
In the end, these steps resolved the issue:
brew uninstall python
pip3 install -U pyobjc

How to get _sqlite3.so file?

I have installed Python 2.6.2.. I did it "locally" since I do not have root permissions. With this version of Python I wanted to use module called "sqlite3" (it is called "pysqlite" in earlier versions). In theory I had to be able to use this module without any problems since it is supposed to be included by default in the considered version of Python. However, I have some troubles. When I type:
from sqlite3 import *
I get:
Traceback (most recent call last):
File "", line 1, in File "/home/verrtex/opt/lib/python2.6/sqlite3/init.py", line 24, in
from dbapi2 import *
File "/home/verrtex/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in
from _sqlite3 import *
ImportError: No module named _sqlite3
As I have already told to, the possible reason of this problem is that the module in tries to import _sqlite3 and fails, so it's not finding _sqlite3.so. This explanations is supported by the fact that I do not have _sqlite3.so file in my "/home/verrtex/opt/lib/python2.6/lib-dynload" directory. So, this is the problem I have to solve (I have to get this file to this directory).
I found out that to solve this problem I have to "install sqlite3 and recompile Python". I also found out that the problem can be solved by "building from source and moving the library to /usr/lib/python2.5/lib-dynload/".
However, it is not clear to me what exactly should I do. Should I install python module called "sqlite3" or I should install sqlite-database? By the way, I have already sqlite-database installed globally by the administrator. Can I use it or I still have to install my own database? By the way, I do not have root permissions. Can it be a problem? Or I need to install a python module? Is absence of root permissions a problem, in this case?
I also has been told to, to take source files from SQLite Download Page, extract archive, move to expanded directory and execute:
./configure
make
make install
Then I have to copy newly compiled files to my Python directory. Should I copy all newly compiled files? And to which exactly directory should I copy (my Python directory have some subdirectories)?
Would very appreciate any help, because I stack with this problem for a wile.
P.S. My OS is CentOS release 5.3 (Final).
Your sys.path is likely not pointing to your locally installed copy, or you're not running the Python 2.6.2 you think you are.
If none of that is the case, you need the SQLite development headers (sqlite-dev or whatever), and then recompile Python. You need to pay attention at the end of the compile, because it complains about what it didn't build due to missing dependencies.
EDIT: Reread question.
EDIT 2: Also, please don't do this:
from module import *
Do this:
from module import what_i_need
import module2
Although you might have found your solution, I just wrote mine down for someone who are stuck in the same problem.
My OS is CentOS 6.3(Final) with python2.6.
I install python2.7.3 in my system, but the problem's still there. (_sqlite3.so should be in /path/to/python2.7.3/lib/python2.7/lib-dynload after python2.7.3 has been installed. Because before python2.7 was installed, sqlite-autoconf-3071502.tar.gz was installed.)
I then copy the /path/to/python2.6/lib/python2.6/lib-dynload/_sqlite3.so to the python2.7's path. And type in the python-shell:
>>> import sqlite3
or
>>> import _sqlite3
No error reports.
Unfortunately, the damn error appeared as before when I run my python script.
I install sqlite-devel(sudo yum install sqlite-devel or download here), and then reinstall python2.7.3 again. Run my python script again. Thank goodness! The damn error finally solved.

Categories