OpenAI Gym - AttributeError: module 'contextlib' has no attribute 'nullcontext' - python

I'm running into this error when trying to run a command from docker a docker container on google compute engine.
Here's the stacktrace:
Traceback (most recent call last):
File "train.py", line 16, in <module>
from stable_baselines.ppo1 import PPO1
File "/home/selfplay/.local/lib/python3.6/site-packages/stable_baselines/__init__.py", line 3, in <module>
from stable_baselines.a2c import A2C
File "/home/selfplay/.local/lib/python3.6/site-packages/stable_baselines/a2c/__init__.py", line 1, in <module>
from stable_baselines.a2c.a2c import A2C
File "/home/selfplay/.local/lib/python3.6/site-packages/stable_baselines/a2c/a2c.py", line 3, in <module>
import gym
File "/home/selfplay/.local/lib/python3.6/site-packages/gym/__init__.py", line 13, in <module>
from gym.envs import make, spec, register
File "/home/selfplay/.local/lib/python3.6/site-packages/gym/envs/__init__.py", line 10, in <module>
_load_env_plugins()
File "/home/selfplay/.local/lib/python3.6/site-packages/gym/envs/registration.py", line 269, in load_env_plugins
context = contextlib.nullcontext()
AttributeError: module 'contextlib' has no attribute 'nullcontext'

It seems like this is an issue with python 3.6 and gym. Upgrading my container to python 3.7 fixed the issue.

Degrading the version of gym may also solve this problem. I met this error with python 3.6, too. And I degraded gym's version from 0.21.0 to 0.15.7, then it was fixed.

You need a specific gym version (==0.15.7) to run contextlib,
I also have this error, and resolved by set the specific pytest version to pytest==7.0.1
Traceback (most recent call last):
File "setup.py", line 24, in <module>
tests_require=['pytest-mock', 'pytest-cov', 'pytest==7.1.2', 'mock', 'tomli==1.2.2', 'coverage==6.4']
File "/opt/teamcity/work/dd131e9564caa7d5/venv/lib64/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
return distutils.core.setup(**attrs)
File "/usr/lib64/python3.6/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib64/python3.6/distutils/dist.py", line 955, in run_commands
self.run_command(cmd)
File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "/opt/teamcity/work/dd131e9564caa7d5/.eggs/pytest_runner-6.0.0-py3.6.egg/ptr/__init__.py", line 203, in run
return self.run_tests()
File "/opt/teamcity/work/dd131e9564caa7d5/.eggs/pytest_runner-6.0.0-py3.6.egg/ptr/__init__.py", line 214, in run_tests
result_code = __import__('pytest').main()
File "/opt/teamcity/work/dd131e9564caa7d5/.eggs/pytest-7.1.2-py3.6.egg/pytest/__init__.py", line 28, in <module>
from _pytest.logging import LogCaptureFixture
File "/opt/teamcity/work/dd131e9564caa7d5/.eggs/pytest-7.1.2-py3.6.egg/_pytest/logging.py", line 7, in <module>
from contextlib import nullcontext
ImportError: cannot import name 'nullcontext'

Related

pipenv virtual environment error with package install

I am new to using pipenv virtual environments. I am using one with a Django project. Recently I tried to start the environment with pipenv shell. I got a message that something like there was no environment available even though I have been using it for several months.
I exited the directory and re-entered. Then ran pipenv shell again. This time the env was activated. However when I try to install a package or run pipenv lock --clear I get the following error:
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✘ Locking Failed!
Traceback (most recent call last):
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/resolver.py", line 764, in <module>
main()
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/resolver.py", line 758, in main
_main(parsed.pre, parsed.clear, parsed.verbose, parsed.system, parsed.write,
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/resolver.py", line 741, in _main
resolve_packages(pre, clear, verbose, system, write, requirements_dir, packages, dev)
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/resolver.py", line 695, in resolve_packages
from pipenv.core import project
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/core.py", line 33, in <module>
from .project import Project
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/project.py", line 30, in <module>
from .vendor.requirementslib.models.utils import get_default_pyproject_backend
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/requirementslib/__init__.py", line 9, in <module>
from .models.lockfile import Lockfile
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/requirementslib/models/lockfile.py", line 14, in <module>
from ..utils import is_editable, is_vcs, merge_items
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/requirementslib/utils.py", line 8, in <module>
import pip_shims.shims
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/pip_shims/__init__.py", line 26, in <module>
from . import shims
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/pip_shims/shims.py", line 12, in <module>
from .models import (
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/pip_shims/models.py", line 790, in <module>
Command.add_mixin(SessionCommandMixin)
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/pip_shims/models.py", line 704, in add_mixin
mixin = mixin.shim()
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/pip_shims/models.py", line 752, in shim
result = self.traverse(top_path)
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/pip_shims/models.py", line 744, in traverse
result = shim.shim()
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/pip_shims/models.py", line 590, in shim
imported = self._import()
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/pip_shims/models.py", line 615, in _import
result = self._import_module(self.calculated_module_path)
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/vendor/pip_shims/models.py", line 365, in _import_module
imported = importlib.import_module(module)
File "/Users/mainuser/.pyenv/versions/3.9.2/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/patched/notpip/_internal/cli/req_command.py", line 15, in <module>
from pipenv.patched.notpip._internal.index.package_finder import PackageFinder
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/patched/notpip/_internal/index/package_finder.py", line 21, in <module>
from pipenv.patched.notpip._internal.index.collector import parse_links
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/patched/notpip/_internal/index/collector.py", line 12, in <module>
from pipenv.patched.notpip._vendor import html5lib, requests
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/patched/notpip/_vendor/requests/__init__.py", line 97, in <module>
from pipenv.patched.notpip._vendor.urllib3.contrib import pyopenssl
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/pipenv/patched/notpip/_vendor/urllib3/contrib/pyopenssl.py", line 46, in <module>
import OpenSSL.SSL
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/OpenSSL/__init__.py", line 8, in <module>
from OpenSSL import rand, crypto, SSL
File "/Users/mainuser/anaconda/lib/python3.6/site-packages/OpenSSL/rand.py", line 213, in <module>
_lib.ERR_load_RAND_strings()
AttributeError: module 'lib' has no attribute 'ERR_load_RAND_strings'
I am not sure how to interpret this but it looks like python3.6 is being used when the shell and project python version is 3.9.
Any ideas about what is causing this and how to fix it?

KeyError: 'packaging' auto-fill the config file spacy BERT model

i'm following this tutorial https://towardsdatascience.com/how-to-fine-tune-bert-transformer-with-spacy-3-6a90bfe57647
and in the part where i have to use this command
!python -m spacy init fill-config drive/MyDrive/config.cfg drive/MyDrive/config_spacy.cfg
i adapted the path according to my colab to run this
!python -m spacy init fill-config "drive/MyDrive/EAD/for TL/base_config.cfg" "drive/MyDrive/EAD/for TL/config_spacy.cfg"
i had this error
Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/lib/python3.7/runpy.py", line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/usr/lib/python3.7/runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "/usr/local/lib/python3.7/dist-packages/spacy/__init__.py", line 10, in <module>
from thinc.api import prefer_gpu, require_gpu, require_cpu # noqa: F401
File "/usr/local/lib/python3.7/dist-packages/thinc/__init__.py", line 5, in <module>
from .config import registry
File "/usr/local/lib/python3.7/dist-packages/thinc/config.py", line 14, in <module>
import srsly
File "/usr/local/lib/python3.7/dist-packages/srsly/__init__.py", line 4, in <module>
from ._msgpack_api import read_msgpack, write_msgpack, msgpack_dumps, msgpack_loads
File "/usr/local/lib/python3.7/dist-packages/srsly/_msgpack_api.py", line 3, in <module>
from . import msgpack
File "/usr/local/lib/python3.7/dist-packages/srsly/msgpack/__init__.py", line 13, in <module>
from ._msgpack_numpy import encode_numpy as _encode_numpy
File "/usr/local/lib/python3.7/dist-packages/srsly/msgpack/_msgpack_numpy.py", line 19, in <module>
import cupy
File "/usr/local/lib/python3.7/dist-packages/cupy/__init__.py", line 12, in <module>
_environment._preload_libraries() # NOQA
File "/usr/local/lib/python3.7/dist-packages/cupy/_environment.py", line 286, in _preload_libraries
if (config is None) or (config['packaging'] == 'conda'):
KeyError: 'packaging'
Sorry you ran into that, we've had one report of that error before. It seems like something is weird with cupy on colab specifically. Based on the previous report, you should start with a clean Python environment and should not install cupy directly. I think colab uses a special version or something.

Can't launch Jupyter Notebook ModuleNotFoundError: No module named 'resource'

I've been using Jupyter Notebook from the command line for a month now, today I didn't shut the running notebook and put my laptop to sleep for 4 hours. After turning my laptop back on I tried to run the notebook again and I'm getting the same error ever since. Please help me out.
Traceback (most recent call last):
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\IPython\utils\timing.py", line 27, in <module>
import resource
ModuleNotFoundError: No module named 'resource'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\Chinmay\AppData\Local\Programs\Python\Python38-32\Scripts\jupyter-notebook.EXE\__main__.py", line 9, in <module>
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\jupyter_core\application.py", line 270, in launch_instance
return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\traitlets\config\application.py", line 663, in launch_instance
app.initialize(argv)
File "<decorator-gen-7>", line 2, in initialize
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\traitlets\config\application.py", line 87, in catch_config_error
return method(app, *args, **kwargs)
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\notebook\notebookapp.py", line 1769, in initialize
self.init_webapp()
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\notebook\notebookapp.py", line 1451, in init_webapp
self.web_app = NotebookWebApplication(
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\notebook\notebookapp.py", line 153, in __init__
settings = self.init_settings(
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\notebook\notebookapp.py", line 269, in init_settings
nbextensions_path=jupyter_app.nbextensions_path,
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\notebook\notebookapp.py", line 1120, in nbextensions_path
from IPython.paths import get_ipython_dir
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\IPython\__init__.py", line 49, in <module>
from .terminal.embed import embed
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\IPython\terminal\embed.py", line 19, in <module>
from IPython.terminal.ipapp import load_default_config
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\IPython\terminal\ipapp.py", line 30, in <module>
from IPython.core.magics import ScriptMagics
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\IPython\core\magics\__init__.py", line 21, in <module>
from .execution import ExecutionMagics
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\IPython\core\magics\execution.py", line 46, in <module>
from IPython.utils.timing import clock, clock2
File "c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\IPython\utils\timing.py", line 64, in <module>
clocku = clocks = clock = time.clock
AttributeError: module 'time' has no attribute 'clock'```
Open this file: c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\IPython\utils\timing.py
change import time to import timeit
change time.clock to timeit.default_timer
This worked for me, should work for you.
conda update --all
this working
For me this happened while trying to install some other pip libraries. Looks like one of my pip libraries got downgraded and that caused this error. So upgrading it back resolved the issue.
pip3 install --upgrade jupyter-console
pip3 install --upgrade jupyter
To solve this, I went in the file execution.py and commented the row from timing import clock. In your machine this file is in the folder:
c:\users\chinmay\appdata\local\programs\python\python38-32\lib\site-packages\IPython\core\magics\
try this:
conda update --force conda
conda update ipython
I uninsatlled and installed the jupyter from conda..Worked fine for me

Pip installation causes RecursionError

I am trying to install a software called grizli (https://github.com/gbrammer/grizli/) in a conda environment. In one step I have to install another software called drizzle (https://github.com/gbrammer/drizzle.git). Thís installation always results in a RecursionError. As explained in the installation instructions I run
pip install git+https://github.com/gbrammer/drizzle.git
resulting in
Collecting git+https://github.com/gbrammer/drizzle.git
Cloning https://github.com/gbrammer/drizzle.git to /tmp/pip-req-build-ncjb9u8_
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-req-build-ncjb9u8_/setup.py", line 61, in <module>
cmdclassd = register_commands(PACKAGENAME, VERSION, RELEASE)
File "/tmp/pip-req-build-ncjb9u8_/astropy_helpers/astropy_helpers/setup_helpers.py", line 140, in register_commands
from .commands.build_sphinx import AstropyBuildSphinx, AstropyBuildDocs
File "/tmp/pip-req-build-ncjb9u8_/astropy_helpers/astropy_helpers/commands/build_sphinx.py", line 16, in <module>
from sphinx.setup_command import BuildDoc as SphinxBuildDoc
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/sphinx/setup_command.py", line 23, in <module>
from sphinx.application import Sphinx
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/sphinx/application.py", line 34, in <module>
from sphinx.environment import BuildEnvironment
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/sphinx/environment/__init__.py", line 38, in <module>
from sphinx.util.websupport import is_commentable
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/sphinx/util/websupport.py", line 11, in <module>
from sphinxcontrib.websupport.utils import is_commentable # NOQA
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/sphinxcontrib/websupport/__init__.py", line 15, in <module>
from sphinxcontrib.websupport.core import WebSupport # NOQA
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/sphinxcontrib/websupport/core.py", line 17, in <module>
from jinja2 import Environment, FileSystemLoader
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/jinja2/__init__.py", line 33, in <module>
from jinja2.environment import Environment, Template
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/jinja2/environment.py", line 15, in <module>
from jinja2 import nodes
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/jinja2/nodes.py", line 19, in <module>
from jinja2.utils import Markup
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/site-packages/jinja2/utils.py", line 486, in <module>
MutableMapping.register(LRUCache)
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/abc.py", line 158, in register
if issubclass(subclass, cls):
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/abc.py", line 209, in __subclasscheck__
ok = cls.__subclasshook__(subclass)
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/typing.py", line 884, in __extrahook__
if issubclass(subclass, scls):
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/typing.py", line 1154, in __subclasscheck__
return super().__subclasscheck__(cls)
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/abc.py", line 209, in __subclasscheck__
ok = cls.__subclasshook__(subclass)
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/typing.py", line 884, in __extrahook__
if issubclass(subclass, scls):
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/typing.py", line 1154, in __subclasscheck__
return super().__subclasscheck__(cls)
...
...
...
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/abc.py", line 209, in __subclasscheck__
ok = cls.__subclasshook__(subclass)
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/typing.py", line 884, in __extrahook__
if issubclass(subclass, scls):
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/typing.py", line 1154, in __subclasscheck__
return super().__subclasscheck__(cls)
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/abc.py", line 209, in __subclasscheck__
ok = cls.__subclasshook__(subclass)
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/typing.py", line 884, in __extrahook__
if issubclass(subclass, scls):
File "/home/username/anaconda3/envs/grizli-dev/lib/python3.6/typing.py", line 1154, in __subclasscheck__
return super().__subclasscheck__(cls)
RecursionError: maximum recursion depth exceeded while calling a Python object
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-req-build-ncjb9u8_/
The three points in the code stand for the repetition of the three lines that always appear in the same order and probably cause the RecursionError.
I have already tried different pip versions but it didn't change anything. Does anybody have an idea?
I just got the same issue. The only solution that worked for me was to downgrade sphinx to a version <=1.5.6 as mentioned here: https://github.com/ericmandel/pyds9/issues/53 (note that I already had astro-helpers >=2.0 when I got the issue, so the solution is on downgrading sphinx)
Just do: pip install sphinx==1.5.6
and then hopefully drizzle will install correctly.

Failed install after installing Myysql-python

Problem using MySQLdb after installing MySQLdb-python. I followed the instructions I followed the instruction that had me delete the build and delete the egg then modify the my_config and then run the install again. I also tried installing the 64 bit version still no luck
Traceback (most recent call last):
File "/Users/nick.leclair/Documents/Aptana Studio 3 Workspace/Tester/driveByScript.py", line 1, in <module>
import MySQLdb
File "build/bdist.macosx-10.9-intel/egg/MySQLdb/__init__.py", line 19, in <module>
File "build/bdist.macosx-10.9-intel/egg/_mysql.py", line 7, in <module>
File "build/bdist.macosx-10.9-intel/egg/_mysql.py", line 4, in __bootstrap__
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 882, in resource_filenameself, resource_name
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 1351, in get_resource_filename self._extract_resource(manager,self._eager_to_zip(name))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 1382, in _extract_resource outf, tmpnam = _mkstemp(".$extract", dir=os.path.dirname(real_path))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 2576, in _mkstemp
from tempfile import mkstemp
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 35, in <module>
ImportError: cannot import name Random
Problem solved, it was problem with Aptana not picking up the new library.

Categories

Resources