Instalation python2.7 in virtualenv - python-2.7

Some day I lost access to Trac which is Python2.7 software. (I suppose, that deprecation of Python2.x is clue). There is Python3 unstable version, but I can't use it, because there's no plugins yes, which are key for my Trac installation.
So created virtual environment:
virtualenv -p /usr/bin/python2.7 env
And finally installation is succesful!
pip install trac
But... Stil can't run it by wsgi apache2:
Traceback (most recent call last):
File "/var/lib/trac/apache/trac.wsgi", line 13, in <module>
import trac.web.main
File "/var/lib/trac/env/lib/python2.7/site-packages/trac/web/__init__.py", line 19, in <module>
from trac.web.api import *
File "/var/lib/trac/env/lib/python2.7/site-packages/trac/web/api.py", line 18, in <module>
from BaseHTTPServer import BaseHTTPRequestHandler
I tried to install pip install http but still the same problem
my wsgi file:
import sys
sys.stdout = sys.stderr
import os
os.environ['PKG_RESOURCES_CACHE_ZIP_MANIFESTS'] = '1'
os.environ['TRAC_ENV_PARENT_DIR'] = '/var/lib/trac'
os.environ['PYTHON_EGG_CACHE'] = '/var/lib/trac/eggs'
#import trac.db.postgres_backend
#trac.db.postgres_backend.PostgreSQLConnection.poolable = False
import trac.web.main
application = trac.web.main.dispatch_request
How to change wsgi or how to install missing module?

Related

flask_bcrypt, werkzeug.security.safe_str_cmp fails after upgrade to latest flask 2.1

I've upgraded my project to latest flask (2.1.2 and now I'm getting following error:
Traceback (most recent call last):
File "/home/ff/conveyors/run.py", line 1, in <module>
from conveyors import create_app
File "/home/ff/conveyors/conveyors/__init__.py", line 4, in <module>
from flask_bcrypt import Bcrypt
File "/home/ff/.cache/pypoetry/virtualenvs/conveyors-SV5d9Vx2-py3.9/lib/python3.9/site-packages/flask_bcrypt.py", line 21, in <module>
from werkzeug.security import safe_str_cmp
ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security'
In my __init__.py I have
...
from flask_bcrypt import bcrypt
from flask_login import LoginManager
...
and then below in create_app:
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
...
I've read Werkzeug release note about safe_str_cmp being removed from Werkzeug 2.1 and needs to be replaced with equivalent hmac functions, but I'm not sure what I need to do in my code to fix this.
Any help greatly appreciated.
Thanks.
It turned out to be a poetry update problem. Whatever I tried it wouldn't upgrade flask_bcrypt to the latest version 1.0.1 and stayed with 0.7.1
I had to start kind of from scratch and do
poetry add flask
...
for every package needed for this app

why does it say "module does not exist" despite multiple installations

I am running the following steps in Mac Terminal:
sudo easy_install pip
sudo pip install virtualenv
virtualenv NameOfFolder
cd NameOfFolder
source bin/activate
sudo pip install django
django-admin startproject NameOfFolderSub1
cd NameOfFolderSub1
python manage.py runserver
At this last steps, it communicates this msg occurred:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
import django
ModuleNotFoundError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
Attempted to check the Django version using this command in Terminal:
python -m django --version
it confirmed that django is not there with the following msg:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3: No module named django
What did I do wrong int he step-by-step installation above ?
Appreciate the help.
The code in manage.py is as follows:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
You've done sudo pip install django. Don't do that. You're in a virtualenv; just install directly without sudo; and use pip3 instead of pip.

how to Install xvfb and run with python and selenium?

I'm trying to use Xvfb to run headless browser. following process which I followed so far
1. Installed xvfb sudo apt-get install xvfb
2. Created virtualenv,
3. Installed xvfbwrapper
4. run following code
import unittest
from selenium import webdriver
from xvfbwrapper import Xvfb
class TestPages(unittest.TestCase):
def setUp(self):
self.xvfb = Xvfb(width=1280, height=720)
self.addCleanup(self.xvfb.stop)
self.xvfb.start()
self.browser = webdriver.Firefox()
self.addCleanup(self.browser.quit)
def testUbuntuHomepage(self):
self.browser.get('http://www.ubuntu.com')
self.assertIn('Ubuntu', self.browser.title)
def testGoogleHomepage(self):
self.browser.get('http://www.google.com')
self.assertIn('Google', self.browser.title)
if __name__ == '__main__':
unittest.main(verbosity=2)
But I'm getting following error, Even I tried installing this with sudo but no effect.
Traceback (most recent call last):
File "xvfbwrapper.py", line 4, in <module>
from xvfbwrapper import Xvfb
File "/home/ubuntu/unclescrooz/src/robinhood/xvfbwrapper.py", line 4, in <module>
from xvfbwrapper import Xvfb
ImportError: cannot import name Xvfb
Same issue with pyvirtualdisplay
Traceback (most recent call last):
File "pyvirtualdisplay.py", line 1, in <module>
from pyvirtualdisplay import Display
File "/m4k/projects/scrapper/stock/robinhood/pyvirtualdisplay.py", line 1, in <module>
from pyvirtualdisplay import Display
ImportError: cannot import name Display
With following code
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()
display.stop()
Rename your files xvfbwrapper.py and pyvirtualdisplay.py to something else than the name of the module you import.

Pandas + Django + mod_wsgi + virtualenv

Pandas is producing 'module' object has no attribute 'core' when being imported under django and mod_wsgi inside a virtual environment. It works fine running under the django development server inside the virtual environment.
Other modules e.g.: numpy have no problems so I assume this means the virtual environment is set up correctly with mod_wsgi. Any advice would be appreciated.
staging.wsgi
import os
import sys
import site
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
site_packages = os.path.join(PROJECT_ROOT, 'env/openportfolio/lib/python2.7/site-packages')
site.addsitedir(os.path.abspath(site_packages))
sys.path.insert(0, PROJECT_ROOT)
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
os.environ['DJANGO_SETTINGS_MODULE'] = 'openportfolio.settings_staging'
import pandas #triggers error
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Error
Traceback (most recent call last):
File "/usr/local/web/django/www/staging/openportfolio/apache/staging.wsgi", line 22, in <module>
import pandas
File "/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/pandas/__init__.py", line 12, in <module>
from pandas.core.api import *
File "/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/pandas/core/api.py", line 6, in <module>
import pandas.core.datetools as datetools
AttributeError: 'module' object has no attribute 'core'
Python Path
['/usr/local/web/django/www/staging/openportfolio',
'/usr/local/web/django/www/staging',
'/Library/Python/2.7/site-packages/pip-1.0.2-py2.7.egg',
'/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg',
'/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages',
'/usr/local/web/django/www/staging/env/openportfolio/lib/python2.7/site-packages']
So it turns out this was a Python path order issue. By running sys.path.reverse() in my wsgi config file, the code now runs.
Due to the order of Python path, the built in OS X numpy library must have been imported first over the virtual environment one causing the issue.
'RuntimeError: module compiled against API version 6 but this version of numpy is 4' was the error line I missed in my original post which could have helped debug the answer.

Pydev and Django: Shell not finding certain modules?

I am developing a Django project with PyDev in Eclipse. For a while, PyDev's Django Shell worked great. Now, it doesn't:
>>> import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
C:\Python26\python.exe 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]
>>>
>>> from django.core import management;import mysite.settings as settings;management.setup_environ(settings)
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named mysite.settings
>>>
The dev server runs just fine. What could I be doing wrong?
The models module is also conspicuously absent:
>>> import mysite.myapp.models
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named mysite.myapp.models
On the normal command line, outside of PyDev, the shell works fine.
Why could this be happening?
Seems like a simple path issue. What's the output of this:
import sys; print sys.path
I don't know anything about PyDev, but there's probably a setting somewhere to add paths to the PYTHONPATH setting. If not, you can do it directly in the shell:
sys.path.insert(0, '/path/to/directory/containing/mysite/')
I had a similar problem to this a while ago while moving my project from Django 1.3 and having the settings.py file at the root of my source and then moving it down into the application.
For example what happened was that I had the following:
rootOfSource/
- settings.py
- myapp
and I changed it to be:
rootOfSource/
- myapp
- myapp/settings.py
and I also changed my settings file to be the following:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
However when I debugged into the os.eviron I found that the DJANGO_SETTINGS_MODULE was not as expected, I then changed my manage.py to be the following:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
os.environ.__setitem__("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Which then allowed me to run from PyDev.
Hope this helps.
I fixed this problem by going to the project properties -> PyDev Django and setting the Django settings module.