Dreamhost - ImportError: No module named _weakrefset - python

Dreamhost upgraded a number of servers this weekend, including the one I was on. It broke my configuration, so as recommended I attempted to delete the virtual environment it was running on and attempted to re-set it up. However, when I try to navigate to the site, I get this:
Traceback (most recent call last):
File "/home/thesp/mysite.com/env/lib/python2.7/site-packages/site.py", line 74, in <module>
__boot()
File "/home/thesp/mysite.com/env/lib/python2.7/site-packages/site.py", line 2, in __boot
import sys, os, os.path
File "/home/thesp/lib/python2.7/os.py", line 400, in <module>
import UserDict
File "/home/thesp/lib/python2.7/UserDict.py", line 83, in <module>
import _abcoll
File "/home/thesp/lib/python2.7/_abcoll.py", line 11, in <module>
from abc import ABCMeta, abstractmethod
File "/home/thesp/lib/python2.7/abc.py", line 8, in <module>
from _weakrefset import WeakSet
ImportError: No module named _weakrefset
I've got Python 2.7.8 installed and running, and from shell access, both in and out of my virtual environment when I run Python, I'm pulling up the correct version (which is different from the native version installed, so it's finding my setup). Other posts which reference this error message seem to think it's a problem with not having an upgraded version of virtualenv, but its version is higher than the troublesome version. (I'm running 1.11.6.)
Weirder still, I can shell into Python, type from _weakrefset import WeakSet, and I don't get import errors. I'm running Django 1.6 and I can python manage.py runserver with no errors, but the web server is throwing up before it ever sees Django.
In the traceback, the first two lines are pulling from my virtual environment, but the remaining ones don't seem to be, and I have no idea why, or even if that's relevant.
Any advice on what I should do next? I've about pulled my hair out on this one! I can post any additional information that would help troubleshoot. Thanks!

Well, I feel silly now. I went to the /home/thesp/lib/python2.7/ directory and downloaded _weakrefset.py, like so:
wget http://svn.python.org/projects/python/trunk/Lib/_weakrefset.py
...and now everything seems to be running fine. There was a _weakrefset.pyo file in the directory, so I'm not sure why _weakrefset.py never made it in, but this seems to have done the trick.
Now, that doesn't solve the mystery of why the stack trace switches directories like it does, but it's running now, so I'll take it for now!

Related

Script works in Anaconda Prompt but not via Command Line

I am unable to run a python script in the command line, and this script works great in Jupyter Notebook and via Anaconda Prompt. This appears to be an issue importing the ssl module during initialization of another module I am importing (mygeotab).
I have googled the error and done as much as I can to diagnose the most common cause which appears to be PATH issues.
I have already diagnosed the PATH to a point, and have added the location of the /lib/ and python.exe to the environment variables. Also, during testing I began my script with the below to protect myself from path issues and printed the path before and after the 'append', which did not have an impact on the problem.
import sys
print(sys.path)
sys.path.append('C:\\Users\\xxxxxx\\Python Scripts')
sys.path.append('C:\\Anaconda3\\python37.zip')
sys.path.append('C:\\Anaconda3\\DLLs')
sys.path.append('C:\\Anaconda3\\lib')
sys.path.append('C:\\Anaconda3')
sys.path.append('C:\\Anaconda3\\lib\\site-packages')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\win32')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\win32\\lib')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\Pythonwin')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\IPython\\extensions')
sys.path.append('C:\\Users\\xxxxxx\\.ipython')
sys.path.append('C:\\Anaconda3\\Lib')
sys.path.append('C:\\Anaconda3\\Lib\\site-packages')
print(sys.path)
import mygeotab
import pandas as pd
import pyodbc as py
from mygeotab.ext import feed
import sqlalchemy
from time import sleep
However, when I attempt to run the script via the standard command line, I get the below error:
Traceback (most recent call last):
File "PYTHON_GEOTAB_TRIP_FEED.py", line 33, in <module>
import mygeotab
File "C:\Anaconda3\lib\site-packages\mygeotab\__init__.py", line 9, in <module>
from .api import Credentials
File "C:\Anaconda3\lib\site-packages\mygeotab\api.py", line 14, in <module>
import ssl
File "C:\Anaconda3\lib\ssl.py", line 98, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: DLL load failed: The specified module could not be found.
The mygeotab module and ssl.py are both in the locations designated in the Traceback. '_ssl' is the only reference I can not seem to diagnose. Again, this works fine in both Notebook and Anaconda Prompt.
Any ideas?
Windows Server 2008 R2
Anaconda 2019.07 x64
Python 3.7.3 x64
This was solved for me by installing a separate instance of Python 3.7, moving the PATH references and other pointers. I installed pip, mygeotab, and the other packages into the native Python 3.7 instance. It just appears you can't use the one baked into anaconda the way I thought. Thanks for the help everyone.

Sudden Import Error for Previously Working Module

I have a long code with a series of imports. For days now I've been running and rerunning this code, including all the imports, for every small change I make (all variable-related, not touching the modules). I keep doing this when suddenly, one time, I get a ImportError: cannot import name _distributor_init upon running import numpy as np. I am, of course, 100% certain that whatever change I made has nothing to do with this import, especially as this is the first executable line in the file, but regardless I reverted that change yet the error continues.
Following some other threads on StackOverflow I tried reinstalling by doing conda install numpy --force-reinstall, this is because in PyCharm and the conda prompt I can clearly see that numpy is installed and up to date.
According to this thread, I tried running from the console, and according to a comment here I tried commenting out the respective line in the __init__.py file, but somewhat expectantly none of that worked.
I am using Miniconda3 and the environment runs Python 3.7. IDE is PyCharm in which the interpreter is the aforementioned environment.
The full error is:
Traceback (most recent call last):
File "<input>", line 7, in <module>
File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "C:\Users\OfirL1\Miniconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
ImportError: cannot import name _distributor_init
Aside of having numpy import as usual, which would be nice, I'd love to know what went wrong. This is not the first time that during normal work one module suddenly stops working all of a sudden for no apparent reason.

Missing Python Packaging Tools on Python Interpreter - Django

I've been working on a basic Django project and I deployed it to AWS Elastic Beanstalk. I'm not sure if that's causing this, but I'm just telling you this so that you know what I might have been done with my project. Then, my python project interpreter got kinda messy and when I try to run the application it started to give me some errors about missing packages. After that, I checked my project interpreter, and it was like this:
Then, I tried to install the required packages using PyCharm, however, it kept giving me the error below. I also tried to build some of those packages manually, like Django, on my own terminal but the error was same. Further, I was able to install packages on other python versions on my mac, which sorta tells I might have messed my default python interpreter somehow.
40:357: execution error: Traceback (most recent call last): File
"/var/folders/f1/k7c234sx5fn9j84yt130_znr0000gn/T/tmp2zDEZTpycharm-management/pip-9.0.1/setup.py",
line 6, in
from setuptools import setup, find_packages File "/Users/bkaankuguoglu/Library/Python/2.7/lib/python/site-packages/setuptools/init.py",
line 12, in
import setuptools.version File "/Users/bkaankuguoglu/Library/Python/2.7/lib/python/site-packages/setuptools/version.py",
line 1, in
import pkg_resources File "/Users/bkaankuguoglu/Library/Python/2.7/lib/python/site-packages/pkg_resources/init.py",
line 72, in
import packaging.requirements File "/Users/bkaankuguoglu/Library/Python/2.7/lib/python/site-packages/packaging/requirements.py",
line 59, in
MARKER_EXPR = originalTextFor(MARKER_EXPR())("marker") TypeError: call() takes exactly 2 arguments (1 given) (1)
I'd really appreciate any help here and please ask me if any extra detail is needed to answer this question before downvoting.
pip install setuptools==33.1.1

Occasional ctypes error importing numpy from mod_wsgi django app

Here's the setup:
Django (1.2) app on mod_wsgi that imports ctypes
Python 2.6.5
Apache 2.2.3
SELinux disabled
RedHat EL 5 64bit
some of the file system is mounted over nfs
Occasionally, when I restart apache I get an import error when it's trying to import ctypes. Every incoming request fails with a 500 error. If I restart apache usually everything just starts working again.
Here's the stack trace of the error:
Traceback (most recent call last):
File "/home/appfirst/django/django/core/handlers/base.py", line 80, in get_response
response = middleware_method(request)
-------------- A BUNCH OF DJANGO MIDDLEWARE STUFF HERE -------------
File "/home/appfirst/django/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/appfirst/backend/backend/streamer/views.py", line 6, in <module>
import appfirst.main.models as FEmodels
File "/home/appfirst/frontend/appfirst/main/models.py", line 27, in <module>
import numpy, math, mpmath
File "/usr/lib64/python2.6/site-packages/numpy/__init__.py", line 43, in <module>
import ctypeslib
File "/usr/lib64/python2.6/site-packages/numpy/ctypeslib.py", line 9, in <module>
import ctypes
File "/usr/lib64/python2.6/ctypes/__init__.py", line 546, in <module>
CFUNCTYPE(c_int)(lambda: None)
MemoryError
I thought it might be related to this bug, but I have SELinux turned off which I thought would mean this case could never occur:
http://bugs.python.org/issue5504
Any suggestions on how to reproduce it consistently and/or fix it? This is really stumping me!
I've run into this bug too. In my case it occurs when I exec a Python script from within a PHP script running under Apache on a 64-bit Linux system. [The Python code being run is the front-end to a pypy sandbox.] The same bit of code works fine on a 32-bit system and even works fine when the PHP script is executed directly from the command line. My "fix" has been simply to comment out that line "CFUNCTYPE(c_int)(lambda: None)" in ctypes/init.py. It's the last line of the file and is preceded by the following comment, showing that the programmer doesn't understand what's going on, either!
# XXX for whatever reasons, creating the first instance of a callback
# function is needed for the unittests on Win64 to succeed. This MAY
# be a compiler bug, since the problem occurs only when _ctypes is
# compiled with the MS SDK compiler. Or an uninitialized variable?
CFUNCTYPE(c_int)(lambda: None)
Clearly there's a deeper problem somewhere in cpython, but the fix works for me.
In case others happen to run into this issue, my fix was to upgrade from Python 3.7 to 3.8.
This offending line: https://github.com/python/cpython/blob/3.7/Lib/ctypes/__init__.py#L273
Was removed in 3.8: https://github.com/python/cpython/blob/3.8/Lib/ctypes/__init__.py#L261-L270
Hopefully, it can save someone the headache of trying to monkey patch ctype/__init__.py which was unsuccessful for me due to the way conda pack handles standard Python libraries.
Hope that helps 🤞
Consider turning SELinux off.
It should solve the problem.

Why can't I import this Zope component in a Python 2.4 virtualenv?

I'm trying to install Plone 3.3rc4 with plone.app.blob and repoze but nothing I've tried has worked so far. For one attempt I've pip-installed repoze.zope2, Plone, and plone.app.blob into a virtualenv. I have this version of DocumentTemplate in the virtualenv's site-packages directory and I'm trying to get it running in RHEL5.
For some reason when I try to run paster serve etc/zope2.ini in this environment way Python gives the message ImportError: No module named DT_Util? DT_Util.py exists in the directory, __init__.py is there too, and the C module it depends on is there. I suspect there's some circular dependency or failure when importing the C extension. Of course this module would work in a normal Zope install...
>>> import DocumentTemplate
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "DocumentTemplate/__init__.py", line 21, in ?
File ".../lib/python2.4/site-packages/DocumentTemplate/DocumentTemplate.py", line 112, in ?
from DT_String import String, File
File ".../lib/python2.4/site-packages/DocumentTemplate/DT_String.py", line 19, in ?
from DocumentTemplate.DT_Util import ParseError, InstanceDict
ImportError: No module named DT_Util
I must say I doubt DocumentTemplate from Zope will work standalone. You are welcome to try though. :-)
Note that DT_Util imports C extensions:
from DocumentTemplate.cDocumentTemplate import InstanceDict, TemplateDict
from DocumentTemplate.cDocumentTemplate import render_blocks, safe_callable
from DocumentTemplate.cDocumentTemplate import join_unicode
You'll need to make sure those are compiled. My guess is that importing the cDocumentTemplate module fails and thus the import of DT_Util fails.

Categories