diff --git a/contrib/check-code.py b/contrib/check-code.py
--- a/contrib/check-code.py
+++ b/contrib/check-code.py
@@ -465,9 +465,10 @@ py3pats = [
     (r'os\.sep', "use pycompat.ossep instead (py3)"),
     (r'os\.pathsep', "use pycompat.ospathsep instead (py3)"),
     (r'os\.altsep', "use pycompat.osaltsep instead (py3)"),
-    (r'os\.getenv', "use pycompat.osgetenv instead (py3)"),
     (r'sys\.platform', "use pycompat.sysplatform instead (py3)"),
     (r'getopt\.getopt', "use pycompat.getoptb instead (py3)"),
+    (r'os\.getenv', "use encoding.environ.get instead"),
+    (r'os\.setenv', "modifying the environ dict is not preferred"),
   ],
   # warnings
   [],
diff --git a/hgext/largefiles/lfutil.py b/hgext/largefiles/lfutil.py
--- a/hgext/largefiles/lfutil.py
+++ b/hgext/largefiles/lfutil.py
@@ -19,6 +19,7 @@ from mercurial.i18n import _
 
 from mercurial import (
     dirstate,
+    encoding,
     error,
     httpconnection,
     match as matchmod,
@@ -74,19 +75,19 @@ def _usercachedir(ui):
     if path:
         return path
     if pycompat.osname == 'nt':
-        appdata = pycompat.osgetenv('LOCALAPPDATA',\
-                        pycompat.osgetenv('APPDATA'))
+        appdata = encoding.environ.get('LOCALAPPDATA',\
+                        encoding.environ.get('APPDATA'))
         if appdata:
             return os.path.join(appdata, longname)
     elif platform.system() == 'Darwin':
-        home = pycompat.osgetenv('HOME')
+        home = encoding.environ.get('HOME')
         if home:
             return os.path.join(home, 'Library', 'Caches', longname)
     elif pycompat.osname == 'posix':
-        path = pycompat.osgetenv('XDG_CACHE_HOME')
+        path = encoding.environ.get('XDG_CACHE_HOME')
         if path:
             return os.path.join(path, longname)
-        home = pycompat.osgetenv('HOME')
+        home = encoding.environ.get('HOME')
         if home:
             return os.path.join(home, '.cache', longname)
     else:
diff --git a/mercurial/profiling.py b/mercurial/profiling.py
--- a/mercurial/profiling.py
+++ b/mercurial/profiling.py
@@ -12,8 +12,8 @@ import time
 
 from .i18n import _
 from . import (
+    encoding,
     error,
-    pycompat,
     util,
 )
 
@@ -120,7 +120,7 @@ def profile(ui):
     Profiling is active when the context manager is active. When the context
     manager exits, profiling results will be written to the configured output.
     """
-    profiler = pycompat.osgetenv('HGPROF')
+    profiler = encoding.environ.get('HGPROF')
     if profiler is None:
         profiler = ui.config('profiling', 'type', default='stat')
     if profiler not in ('ls', 'stat', 'flame'):
diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -46,7 +46,6 @@ if ispy3:
     ospathsep = os.pathsep.encode('ascii')
     ossep = os.sep.encode('ascii')
     osaltsep = os.altsep
-    osgetenv = os.getenvb
     if osaltsep:
         osaltsep = osaltsep.encode('ascii')
     # os.getcwd() on Python 3 returns string, but it has os.getcwdb() which
@@ -169,7 +168,6 @@ else:
     sysargv = sys.argv
     sysplatform = sys.platform
     getcwd = os.getcwd
-    osgetenv = os.getenv
     sysexecutable = sys.executable
     shlexsplit = shlex.split
 
diff --git a/mercurial/url.py b/mercurial/url.py
--- a/mercurial/url.py
+++ b/mercurial/url.py
@@ -15,10 +15,10 @@ import socket
 
 from .i18n import _
 from . import (
+    encoding,
     error,
     httpconnection as httpconnectionmod,
     keepalive,
-    pycompat,
     sslutil,
     util,
 )
@@ -81,7 +81,7 @@ class passwordmgr(object):
 class proxyhandler(urlreq.proxyhandler):
     def __init__(self, ui):
         proxyurl = (ui.config("http_proxy", "host") or
-                        pycompat.osgetenv('http_proxy'))
+                        encoding.environ.get('http_proxy'))
         # XXX proxyauthinfo = None
 
         if proxyurl:
@@ -99,7 +99,7 @@ class proxyhandler(urlreq.proxyhandler):
             no_list.extend([p.lower() for
                             p in ui.configlist("http_proxy", "no")])
             no_list.extend([p.strip().lower() for
-                            p in pycompat.osgetenv("no_proxy", '').split(',')
+                            p in encoding.environ.get("no_proxy", '').split(',')
                             if p.strip()])
             # "http_proxy.always" config is for running tests on localhost
             if ui.configbool("http_proxy", "always"):