pyramid.config.Configurator() does not work with 'with' context manager - pyramid

Is trying the quick start guide in pyramid website: https://trypyramid.com
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.request import Response
def hello_world(request):
return Response('Hello World')
if __name__ == "__main__":
with Configurator() as config:
config.add_route('hello','/')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0',6543,app)
Gives the following error:
/anaconda3/bin/python /Users/user1/Desktop/play/pyramid_play/tut_1/exp1.py
Traceback (most recent call last):
File "/Users/user1/Desktop/play/pyramid_play/tut_1/exp1.py", line 12, in <module>
with Configurator() as config:
AttributeError: __enter__
Process finished with exit code 1

Thanks Steve Piercy. This was an installation problem. I let pycharm install Pyramid while creating the project. pycharm used conda to install the package, which installed v1.5.7 (latest on conda repos) instead of the 1.9.1 (current version).
I installed pip to install the latest version which fixed the issue.

Related

MetaData Generation Failed while installing PyQt5

I have a similar problem as AttributeError: module 'sipbuild.api' has no attribute 'prepare_metadata_for_build_wheel' for PyQt5 5.15.0. Most of the solutions proposed there is to install the latest version of pip. But I have the latest pip version (22.0.2) and it is still not working. I am trying to install it in MacOS 12.1, python version 3.8.9.
Command Output:
Collecting PyQt5
Using cached PyQt5-5.15.6.tar.gz (3.2 MB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... error
error: subprocess-exited-with-error
× Preparing metadata (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [29 lines of output]
Traceback (most recent call last):
File "/Users/ashuadhikari/Library/Python/3.8/lib/python/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 156, in prepare_metadata_for_build_wheel
hook = backend.prepare_metadata_for_build_wheel
AttributeError: module 'sipbuild.api' has no attribute 'prepare_metadata_for_build_wheel'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/ashuadhikari/Library/Python/3.8/lib/python/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module>
main()
File "/Users/ashuadhikari/Library/Python/3.8/lib/python/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/Users/ashuadhikari/Library/Python/3.8/lib/python/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 160, in prepare_metadata_for_build_wheel
whl_basename = backend.build_wheel(metadata_directory, config_settings)
File "/private/var/folders/2_/r0h48ntn7x9dz67r08nh7rv80000gn/T/pip-build-env-tp6yyadf/overlay/lib/python3.8/site-packages/sipbuild/api.py", line 51, in build_wheel
project = AbstractProject.bootstrap('pep517')
File "/private/var/folders/2_/r0h48ntn7x9dz67r08nh7rv80000gn/T/pip-build-env-tp6yyadf/overlay/lib/python3.8/site-packages/sipbuild/abstract_project.py", line 83, in bootstrap
project.setup(pyproject, tool, tool_description)
File "/private/var/folders/2_/r0h48ntn7x9dz67r08nh7rv80000gn/T/pip-build-env-tp6yyadf/overlay/lib/python3.8/site-packages/sipbuild/project.py", line 587, in setup
self.apply_user_defaults(tool)
File "project.py", line 63, in apply_user_defaults
super().apply_user_defaults(tool)
File "/private/var/folders/2_/r0h48ntn7x9dz67r08nh7rv80000gn/T/pip-build-env-tp6yyadf/overlay/lib/python3.8/site-packages/pyqtbuild/project.py", line 70, in apply_user_defaults
super().apply_user_defaults(tool)
File "/private/var/folders/2_/r0h48ntn7x9dz67r08nh7rv80000gn/T/pip-build-env-tp6yyadf/overlay/lib/python3.8/site-packages/sipbuild/project.py", line 234, in apply_user_defaults
self.builder.apply_user_defaults(tool)
File "/private/var/folders/2_/r0h48ntn7x9dz67r08nh7rv80000gn/T/pip-build-env-tp6yyadf/overlay/lib/python3.8/site-packages/pyqtbuild/builder.py", line 67, in apply_user_defaults
raise PyProjectOptionException('qmake',
sipbuild.pyproject.PyProjectOptionException
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
Note: Installing PyQt5 with brew worked but brew installed the latest python version as a dependency and I want to install PyQt5 inside my venv.
I was also having problems installing PyQt5 on an M1 Mac but found another alternative if you want to use venv without miniconda. Using brew to install python doesn't fix the pip errors when trying to install PyQt5 inside a venv.
The bug actually seems related to using the Python universal installer, and then there appears to be another bug with PyQt5 5.15.6.
First: install Python 3.9.13 MacOS Intel-only installer from python.org
After I did this I tried using pip to install the latest version of PyQt (5.15.6) inside the venv but pip flagged a hash mismatch:
ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
PyQt5-Qt5>=5.15.2 from https://files.pythonhosted.org/packages/62/09/99a222b0360616250fb2e6003a54e43a2a06b0774f0f8d5daafb86a2c375/PyQt5_Qt5-5.15.2-py3-none-macosx_10_13_intel.whl#sha256=76980cd3d7ae87e3c7a33bfebfaee84448fd650bad6840471d6cae199b56e154 (from PyQt5):
Expected sha256 76980cd3d7ae87e3c7a33bfebfaee84448fd650bad6840471d6cae199b56e154
Got d97d141facbdb3af4f5593b5068fb1d57a2e01ccace3758b897636700f2a2640
So Second: use pip install PyQt5==5.15.5
That successfully allowed me to use venv as normal with PyQt5.
So people can find this answer outside of the comments :
Using Brew on macOS works, install both Python and PyQT5 from there, and be sure to change your python command to Brew's directory if you want it to work properly.
I have no idea why, but pip is currently broken on macOS (perhaps all platforms) when installing from python.org
1- check last updates python/pip version
2- install last version package (pip install pyqt6)
3- if you need run file QtDesigner just run main python , not ui script
example :
main.py:
from PyQt6 import uic
from PyQt6.QtWidgets import QApplication
Form, Window = uic.loadUiType("untitled.ui")
app = QApplication([])
window = Window()
form = Form()
form.setupUi(window)
window.show()
app.exec()
untitled.ui:
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
QRadialGradient)
from PySide2.QtWidgets import *
class Ui_Frame(object):
def setupUi(self, Frame):
if Frame.objectName():
Frame.setObjectName(u"Frame")
Frame.resize(640, 480)
self.pushButton = QPushButton(Frame)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(250, 210, 75, 23))
self.retranslateUi(Frame)
QMetaObject.connectSlotsByName(Frame)
# setupUi
def retranslateUi(self, Frame):
Frame.setWindowTitle(QCoreApplication.translate("Frame", u"Frame", None))
self.pushButton.setText(QCoreApplication.translate("Frame", u"PushButton", None))
# retranslateUi
and save you ui code in file untitled.ui (dont run ui file format .py)
;D

dnspython: module not found in pycharm

I'm trying to connect to MongoDB from the pyCharm environment.
I'm using python 3.8 and I installed pymongo, dnspython and dnspython3.
My settings for the project are:
My code is:
from pymongo import MongoClient
import argparse
import dnspython
if __name__ == "__main__":
client = MongoClient("mongodb+srv://rajnesh:<myPassword>#cluster0-chffs.mongodb.net/test?authSource=admin&replicaSet=Cluster0-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true")
print("Hello there!")
However, I get the following Error:
Traceback (most recent call last): File
"/Users/rajnesh/pyProgram.py", line 17, in
import dnspython ModuleNotFoundError: No module named 'dnspython'
Process finished with exit code 1
Thanks in advance for your help.
For the dnspython package, the import name is "dns". More info about this can be found below:
http://www.dnspython.org/docs/1.16.0/
http://www.dnspython.org/examples.html

windows 10 python modulenotfounderror

I have an issue with Python 3.6.3 on Windows 10.
I have installed flask_bootstrap. Here is my (edited) pip freeze:
C:\Users\pablo\willwriter2>pip freeze
Flask==0.12.2
Flask-Bootstrap==3.3.7.1
When I run a very simple script:
from flask import Flask
from bootstrap import Bootstrap
app = Flask(__name__)
bootstrap = Bootstrap(app)
#app.routes('/')
def homepage():
return "<h1>Hello World</h1>"
I get the following error message:
File "C:\Users\pablo\willwriter2\main2.py", line 2, in <module>
from bootstrap import Bootstrap
ModuleNotFoundError: No module named 'bootstrap'
This error occurs whether I use a virtual environment or the global python install. It has occurred for other modules as well.
Does anyone know why this is happening?
Thanks,
Paul.

ImportError pyramid hello world program

source code:
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('<h1>Hello world!</h1>')
if __name__ == '__main__':
config = Configurator()
config.add_view(hello_world)
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
When I ran the sample hello_world program using pyramid, I got the following error.
Traceback (most recent call last):
File "application.py", line 2, in <module>
from pyramid.config import Configurator
File "/usr/local/lib/python2.7/dist-packages/pyramid/config/__init__.py", line 12, in <module>
from pyramid.interfaces import (
File "/usr/local/lib/python2.7/dist-packages/pyramid/interfaces.py", line 3, in <module>
from zope.interface import (
ImportError: No module named interface
You've installed something improperly - more than likely by using python setup.py develop somewhere instead of using pip install -e .. If you mix tools you're going to have some issues. This particular one seems to be due to namespace packages not being configured correctly which almost always is a symptom of using easy_install and pip in the same environment. You need to pick one (preferably pip), and sometimes the decision about which one to use was already made by whatever tool installed python for you.

SSHTunnel not installed correctly?

I'm using anaconda python 3.5.2 in PyCharm using windows 10.
I'm pretty new to python and a complete noob to PyCharm!
I just want to use SSHtunnel.
I have intalled the package and it shown in the project interpreter page:
However when I try to run the code which I cut and pasted from example 1 here https://github.com/pahaz/sshtunnel:
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
'pahaz.urfuclub.ru',
ssh_username="pahaz",
ssh_password="secret",
remote_bind_address=('127.0.0.1', 8080)
)
server.start()
print(server.local_bind_port) # show assigned local port
# work with `SECRET SERVICE` through `server.local_bind_port`.
server.stop()
I get the following error:
C:\Users\HP\Anaconda3\python.exe C:/Users/HP/PycharmProjects/SSH_Downloader/SSH_Downloader
Traceback (most recent call last):
File "C:/Users/HP/PycharmProjects/SSH_Downloader/SSH_Downloader", line 1, in <module>
from sshtunnel import SSHTunnelForwarder
ImportError: cannot import name 'SSHTunnelForwarder'
Process finished with exit code 1
How can I sort this out?
Just run the command below:
pip install sshtunnel
sshtunnel for python and sshtunnel Anaconda-python are different modules.
To install sshtunnel for Anaconda; you need to use the Anaconda repository. use below pip command; Hope this helps:
pip install -i https://pypi.anaconda.org/pypi/simple sshtunnel
More Description Here
I think your name of file is sshtunnel.py
i face this problem on window 10 and fix it buy this line
easy_install sshtunnel

Resources