PythonAnywhere + virtualenv: "Could not find platform dependent libraries <exec_prefix>..." - python

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

Related

Python installation error using pyenv Tkinter not found MAC

I had to uninstall and reinstall python for other reasons.
Right now I downloaded python 3.8.2 through pyenv but running:
import tkinter
The error I get is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/cagava/.pyenv/versions/3.8.2/lib/python3.8/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
why is that so? Am I missing some modules in python installation?
Note that I am working on a Mac
I've tried the solution proposed on the issue (Python not configured for Tk) and others suggesting the same method but it didn't solve my problem
Struggling with this myself. For all the talk about the "annual ritual" to include tkinter with Python, it sure isn't well integrated, especially when using pyenv.
I'm about to take the plunge one site suggested: removing pyenv completely, then installing tcl-tk, then compiling a number of post-tcl-tk dependencies that come up after the tcl-tk install, then reinstalling pyenv (which should, supposedly, then add tkinter support - even though tkinter was installed on the default system python of 2.7.16, then reinstalling the versions of Python I've already installed (3.7 and 3.8 and which seem to work for everything but tkinter). Like I said - not well integrated. Were it not for the many comments about how well tkinter has been expanded and maintained I'd be tempted to (and actually, was tempted to) try something else.
Linux Ubuntu and PopOS (Ubuntu derivative) both display the same problem, on two different machines. I'll give the above a try and report back on the fireworks.

ImportError: No module named twisted.persisted.styles

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.

Problems setting up Python

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

Detect python package installation path from within setup.py

After installation, I would like to make soft-links to some of the configuration & data files created by installation.
How can I determine the location of a new package's files installed from within the package's setup.py?
I initially hard-coded the path "/usr/local/lib/python2.7/dist-packages", but that broke when I tried using a virtual environment. (Created by virtualenv.)
I tried distutils.sysconfig.get_python_lib(), and that works inside the virtualenv. When installed on the real system, however, it returns "/usr/lib/python2.7/dist-packages" (Note the "local" directory isn't present.)
I've also tried site.getsitepackages():
Running a Python shell from the base environment:
import site
site.getusersitepackages()
'/home/sarah/.local/lib/python2.7/site-packages'
site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
Running a Python shell from a virtual environment "testenv":
import site
site.getsitepackages()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute 'getsitepackages'
I'm running "Python 2.7.3 (default, Aug 1 2012, 05:14:39)" with "[GCC 4.6.3] on linux2" on Ubuntu. I can probably cobble something together with try-except blocks, but it seems like there should be some variable set / returned by distutils / setuptools. (I'm agnostic about which branch to use, as long as it works.)
Thanks.
I haven't found the "correct" way of doing this, but I have found a couple tricks that seem almost-correct. One method only works on install; the other only works if the package is already installed.
For install, I use the object returned by setuptools.setup():
from setuptools import setup
s = setup([...])
installation_path = s.command_obj['install'].install_lib
(This only works during install since you need a valid Distribution object for those attributes to exist. AFAIK, the only way to get such an object is to run setup().)
On uninstall, I use the file attribute of the package, as suggested by #Zhenya above. The only catch is that when I run ./setup.py uninstall to get rid of package, I usually have directories ./package/, ./build, ./dist, and ./package.egg-info/. (The "uninstall" option is caught by my code without calling setup(). It runs a manually-created script to delete the package files.) These can redirect the python interpreter to some place other than the globally-accessible repository I'm trying to get rid of. Here's my hack to handle that:
import imp
import sys
from subprocess import Popen
from os import getcwd
Popen('rm -r build dist *.egg-info', shell=True).wait()
oldpath = sys.path
rundir = getcwd()
sys.path.remove(rundir)
mod = imp.find_module(PACKAGE)
p = imp.load_module(PACKAGE, mod[0], mod[1], mod[2])
sys.path = oldpath
installation_path = p.__file__
(This doesn't work during install since - I think - Python only inventories modules when it starts, so find_module() won't find the just-installed package unless you exit python and come back in.)
I've tested both install and uninstall on a bare environment and a virtual environment (from virtualenv 1.9.1). I'm running Ubuntu 12.04 LTS, Python 2.7.3, setuptools 0.6c11 (in the bare environment) and setuptools 0.7.4 (in virtualenv).
This will probably not answer your question, but if you need to access the source code of a package you have installed, or any other file within this package, the best way to do it is to install this package in develop mode (by downloading the sources, putting it wherever you want and then running python setup.py develop in the base directory of the package sources). This way you know where the package is found.

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

Categories