danimtb: Tried to make unit tests for this but I ended up with a lot of objects with values pre loaded just to test the loader methods... 🙄 So maybe it is better to add just a functional test for conanfile.txt and check the environment prepended values with the virtualenv generator output
diff --git a/conans/client/loader.py b/conans/client/loader.py
index 525ef71b8..9bd73cab0 100644
--- a/conans/client/loader.py
+++ b/conans/client/loader.py
@@ -174,7 +174,6 @@ class ConanFileLoader(object):
 
         # imports method
         conanfile.imports = parser.imports_method(conanfile)
-        conanfile._conan_env_values.update(processed_profile._env_values)
         return conanfile
 
     def load_virtual(self, references, processed_profile, scope_options=True,
diff --git a/conans/client/output.py b/conans/client/output.py
index 9c18a4941..655e6770d 100644
--- a/conans/client/output.py
+++ b/conans/client/output.py
@@ -58,18 +58,22 @@ class ConanOutput(object):
                 data = decode_text(data)  # Keep python 2 compatibility
 
         if self._color and (front or back):
-            color = "%s%s" % (front or '', back or '')
-            end = (Style.RESET_ALL + "\n") if newline else Style.RESET_ALL  # @UndefinedVariable
-            data = "%s%s%s" % (color, data, end)
-        else:
-            if newline:
-                data = "%s\n" % data
-
-        try:
-            self._stream.write(data)
-        except UnicodeError:
-            data = data.encode("utf8").decode("ascii", "ignore")
-            self._stream.write(data)
+            data = "%s%s%s%s" % (front or '', back or '', data, Style.RESET_ALL)
+        if newline:
+            data = "%s\n" % data
+
+        # https://github.com/conan-io/conan/issues/4277
+        # Windows output locks produce IOErrors
+        for _ in range(3):
+            try:
+                self._stream.write(data)
+                break
+            except IOError:
+                import time
+                time.sleep(0.02)
+            except UnicodeError:
+                data = data.encode("utf8").decode("ascii", "ignore")
+
         self._stream.flush()
 
     def info(self, data):
diff --git a/conans/util/progress_bar.py b/conans/util/progress_bar.py
index 51f64f4b2..295fb2f7c 100644
--- a/conans/util/progress_bar.py
+++ b/conans/util/progress_bar.py
@@ -1,4 +1,3 @@
-
 import os
 from contextlib import contextmanager
 
@@ -18,17 +17,15 @@ class _FileReaderWithProgressBar(object):
 
     def __init__(self, fileobj, output, desc=None):
         pb_kwargs = self.tqdm_defaults.copy()
-        self._ori_output = output
 
         # If there is no terminal, just print a beat every TIMEOUT_BEAT seconds.
         if not output.is_terminal:
             output = _NoTerminalOutput(output)
             pb_kwargs['mininterval'] = TIMEOUT_BEAT_SECONDS
 
-        self._output = output
         self._fileobj = fileobj
         self.seek(0, os.SEEK_END)
-        self._pb = tqdm(total=self.tell(), desc=desc, file=output, **pb_kwargs)
+        self._tqdm_bar = tqdm(total=self.tell(), desc=desc, file=output, **pb_kwargs)
         self.seek(0)
 
     def seekable(self):
@@ -43,15 +40,11 @@ class _FileReaderWithProgressBar(object):
     def read(self, size):
         prev = self.tell()
         ret = self._fileobj.read(size)
-        self._pb.update(self.tell() - prev)
+        self._tqdm_bar.update(self.tell() - prev)
         return ret
 
     def pb_close(self):
-        self._pb.close()
-
-    def pb_write(self, message):
-        """ Allow to write messages to output without interfering with the progress bar """
-        tqdm.write(message, file=self._ori_output)
+        self._tqdm_bar.close()
 
 
 class _NoTerminalOutput(object):

Virtualenv generator prepends duplicated values in env vars from profile
With a *conanfile.txt* empty and a profile with:

```
[env]
PREPEND_VAR = ['kk','pp']
```

Commands:

```
$ conan install . -g virtualenv
```

In activate.sh there is:
```
PREPEND_VAR="kk":"pp":"kk":"pp"
```
and in *activate.bat* you alaso have:
```
SET PREPEND_VAR=kk;pp;kk;pp
```

So at least variables that should be pretended are applied twice.


diff --git a/conans/test/functional/configuration/profile_test.py b/conans/test/functional/configuration/profile_test.py
index e1ce97765..c7c8d7c26 100644
--- a/conans/test/functional/configuration/profile_test.py
+++ b/conans/test/functional/configuration/profile_test.py
@@ -1,4 +1,5 @@
 import os
+import platform
 import unittest
 from collections import OrderedDict
 from textwrap import dedent
@@ -27,7 +28,6 @@ class AConan(ConanFile):
             self.run("SET")
         else:
             self.run("env")
-
 """
 
 
@@ -44,23 +44,37 @@ class ProfileTest(unittest.TestCase):
     def setUp(self):
         self.client = TestClient()
 
+    def profile_conanfile_txt_test(self):
+        """
+        Test prepended env variables are applied correctrly from a profile
+        """
+        self.client.save({"conanfile.txt": ""})
+        create_profile(self.client.cache.profiles_path, "envs", settings={},
+                       env=[("A_VAR", "A_VALUE"), ("PREPEND_VAR", ["new_path", "other_path"])],
+                       package_env={"Hello0": [("OTHER_VAR", "2")]})
+        self.client.run("install . -pr envs -g virtualenv")
+        content = load(os.path.join(self.client.current_folder, "activate.sh"))
+        self.assertIn(":".join(["PREPEND_VAR=\"new_path\"", "\"other_path\"", "$PREPEND_VAR"]),
+                      content)
+        if platform.system() == "Windows":
+            content = load(os.path.join(self.client.current_folder, "activate.bat"))
+            self.assertIn(";".join(["PREPEND_VAR=new_path", "other_path", "%PREPEND_VAR%"]),
+                          content)
+
     def test_profile_relative_cwd(self):
-        client = TestClient()
-        client.save({"conanfile.txt": "",
-                     "sub/sub/profile": ""})
-        client.current_folder = os.path.join(client.current_folder, "sub")
-        client.run("install .. -pr=sub/profile2", assert_error=True)
-        self.assertIn("ERROR: Profile not found: sub/profile2", client.out)
-        client.run("install .. -pr=sub/profile")
-        self.assertIn("conanfile.txt: Installing package", client.out)
+        self.client.save({"conanfile.txt": "", "sub/sub/profile": ""})
+        self.client.current_folder = os.path.join(self.client.current_folder, "sub")
+        self.client.run("install .. -pr=sub/profile2", assert_error=True)
+        self.assertIn("ERROR: Profile not found: sub/profile2", self.client.out)
+        self.client.run("install .. -pr=sub/profile")
+        self.assertIn("conanfile.txt: Installing package", self.client.out)
 
     def base_profile_generated_test(self):
         """we are testing that the default profile is created (when not existing, fresh install)
          even when you run a create with a profile"""
-        client = TestClient()
-        client.save({CONANFILE: conanfile_scope_env,
-                     "myprofile": "include(default)\n[settings]\nbuild_type=Debug"})
-        client.run("create . conan/testing --profile myprofile")
+        self.client.save({CONANFILE: conanfile_scope_env,
+                          "myprofile": "include(default)\n[settings]\nbuild_type=Debug"})
+        self.client.run("create . conan/testing --profile myprofile")
 
     def bad_syntax_test(self):
         self.client.save({CONANFILE: conanfile_scope_env})
@@ -148,11 +162,15 @@ class ProfileTest(unittest.TestCase):
         files["conanfile.py"] = conanfile_scope_env
 
         create_profile(self.client.cache.profiles_path, "envs", settings={},
-                       env=[("A_VAR", "A_VALUE")], package_env={"Hello0": [("OTHER_VAR", "2")]})
+                       env=[("A_VAR", "A_VALUE"),
+                            ("PREPEND_VAR", ["new_path", "other_path"])],
+                       package_env={"Hello0": [("OTHER_VAR", "2")]})
 
         self.client.save(files)
         self.client.run("export . lasote/stable")
         self.client.run("install Hello0/0.1@lasote/stable --build missing -pr envs")
+        self._assert_env_variable_printed("PREPEND_VAR", os.pathsep.join(["new_path", "other_path"]))
+        self.assertEqual(1, str(self.client.out).count("PREPEND_VAR=new_path"))  # prepended once
         self._assert_env_variable_printed("A_VAR", "A_VALUE")
         self._assert_env_variable_printed("OTHER_VAR", "2")
 
diff --git a/conans/test/unittests/client/conan_output_test.py b/conans/test/unittests/client/conan_output_test.py
new file mode 100644
index 000000000..77838157c
--- /dev/null
+++ b/conans/test/unittests/client/conan_output_test.py
@@ -0,0 +1,30 @@
+# coding=utf-8
+
+import unittest
+from types import MethodType
+
+from six import StringIO
+
+from conans.client.output import ConanOutput
+from mock import mock
+
+
+class ConanOutputTest(unittest.TestCase):
+
+    def test_blocked_output(self):
+        # https://github.com/conan-io/conan/issues/4277
+        stream = StringIO()
+
+        def write_raise(self, data):
+            write_raise.counter = getattr(write_raise, "counter", 0) + 1
+            if write_raise.counter < 2:
+                raise IOError("Stdout locked")
+            self.super_write(data)
+        stream.super_write = stream.write
+        stream.write = MethodType(write_raise, stream)
+        out = ConanOutput(stream)
+
+        with mock.patch("time.sleep") as sleep:
+            out.write("Hello world")
+            sleep.assert_any_call(0.02)
+        self.assertEqual("Hello world", stream.getvalue())
diff --git a/conans/test/unittests/client/generators/virtualenv_test.py b/conans/test/unittests/client/generators/virtualenv_test.py
new file mode 100644
index 000000000..458160b04
--- /dev/null
+++ b/conans/test/unittests/client/generators/virtualenv_test.py
@@ -0,0 +1,22 @@
+import platform
+import unittest
+
+from conans import ConanFile, Settings
+from conans.client.generators.virtualenv import VirtualEnvGenerator
+from conans.model.env_info import EnvValues
+from conans.test.utils.tools import TestBufferConanOutput
+
+
+class VirtualenvGeneratorTest(unittest.TestCase):
+
+    def prepend_values_test(self):
+        """
+        Check list values are only prepended once
+        """
+        conanfile = ConanFile(TestBufferConanOutput(), None)
+        conanfile.initialize(Settings({}), EnvValues.loads("PATH=[1,2,three]"))
+        gen = VirtualEnvGenerator(conanfile)
+        content = gen.content
+        self.assertIn("PATH=\"1\":\"2\":\"three\":$PATH", content["activate.sh"])
+        if platform.system() == "Windows":
+            self.assertIn("PATH=1;2;three;%PATH%", content["activate.bat"])
diff --git a/conans/test/unittests/client/loader_test.py b/conans/test/unittests/client/loader_test.py
new file mode 100644
index 000000000..5ae0a4d96
--- /dev/null
+++ b/conans/test/unittests/client/loader_test.py
@@ -0,0 +1,69 @@
+import os
+import textwrap
+import unittest
+
+from conans import Settings
+from conans.client.graph.python_requires import ConanPythonRequire
+from conans.client.loader import ConanFileLoader
+from conans.model.env_info import EnvValues
+from conans.model.profile import Profile
+from conans.model.ref import ConanFileReference
+from conans.test.utils.conanfile import MockSettings
+from conans.test.utils.runner import TestRunner
+from conans.test.utils.test_files import temp_folder
+from conans.test.utils.tools import TestBufferConanOutput
+from conans.util.files import save
+
+
+class LoadConanfileTxtTest(unittest.TestCase):
+
+    def setUp(self):
+        settings = Settings()
+        self.profile = Profile()
+        self.profile._settings = settings
+        self.profile._user_options = None
+        self.profile._env_values = None
+        self.conanfile_txt_path = os.path.join(temp_folder(), "conanfile.txt")
+        output = TestBufferConanOutput()
+        self.loader = ConanFileLoader(TestRunner(output), output, None)
+
+    def env_test(self):
+        env_values = EnvValues()
+        env_values.add("PREPEND_PATH", ["hello", "bye"])
+        env_values.add("VAR", ["var_value"])
+        self.profile._env_values = env_values
+        save(self.conanfile_txt_path, "")
+        conanfile = self.loader.load_conanfile_txt(self.conanfile_txt_path, self.profile)
+        self.assertEquals(conanfile.env, {"PREPEND_PATH": ["hello", "bye"], "VAR": ["var_value"]})
+
+
+class LoadConanfileTest(unittest.TestCase):
+
+    def setUp(self):
+        settings = Settings()
+        self.profile = Profile()
+        self.profile._settings = settings
+        self.profile._user_options = None
+        self.profile._env_values = None
+        self.profile._dev_reference = None
+        self.profile._package_settings = None
+        self.conanfile_path = os.path.join(temp_folder(), "conanfile.py")
+        output = TestBufferConanOutput()
+        self.loader = ConanFileLoader(TestRunner(output), output, ConanPythonRequire(None, None))
+
+    def env_test(self):
+        env_values = EnvValues()
+        env_values.add("PREPEND_PATH", ["hello", "bye"])
+        env_values.add("VAR", ["var_value"])
+        self.profile._env_values = env_values
+        save(self.conanfile_path,
+             textwrap.dedent("""
+                from conans import ConanFile
+                
+                class TestConan(ConanFile):
+                    name = "hello"
+                    version = "1.0"
+             """))
+        ref = ConanFileReference("hello", "1.0", "user", "channel")
+        conanfile = self.loader.load_conanfile(self.conanfile_path, self.profile, ref)
+        self.assertEquals(conanfile.env, {"PREPEND_PATH": ["hello", "bye"], "VAR": ["var_value"]})

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov
apt-get update apt-get install -y gcc
3.6
conans/requirements.txt conans/requirements_server.txt conans/requirements_dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
astroid==2.11.7
attrs==22.2.0
beautifulsoup4==4.12.3
bottle==0.12.25
certifi==2021.5.30
charset-normalizer==2.0.12
codecov==2.1.13
colorama==0.4.5
-e git+https://github.com/conan-io/conan.git@256c14775b6281dec546aec1aa4c5973fe0d3ac8#egg=conan
coverage==4.2
deprecation==2.0.7
dill==0.3.4
distro==1.1.0
fasteners==0.19
future==0.16.0
idna==3.10
importlib-metadata==4.8.3
importlib-resources==5.4.0
iniconfig==1.1.1
isort==5.10.1
lazy-object-proxy==1.7.1
mccabe==0.7.0
mock==1.3.0
node-semver==0.6.1
nose==1.3.7
packaging==21.3
parameterized==0.8.1
patch==1.16
pbr==6.1.1
platformdirs==2.4.0
pluggy==1.0.0
pluginbase==0.7
py==1.11.0
Pygments==2.14.0
PyJWT==1.7.1
pylint==2.13.9
pyparsing==3.1.4
pytest==7.0.1
pytest-cov==4.0.0
PyYAML==3.13
requests==2.27.1
six==1.17.0
soupsieve==2.3.2.post1
tomli==1.2.3
tqdm==4.64.1
typed-ast==1.5.5
typing_extensions==4.1.1
urllib3==1.26.20
waitress==2.0.0
WebOb==1.8.9
WebTest==2.0.35
wrapt==1.16.0
zipp==3.6.0

name: conan
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - astroid==2.11.7
      - attrs==22.2.0
      - beautifulsoup4==4.12.3
      - bottle==0.12.25
      - charset-normalizer==2.0.12
      - codecov==2.1.13
      - colorama==0.4.5
      - coverage==4.2
      - deprecation==2.0.7
      - dill==0.3.4
      - distro==1.1.0
      - fasteners==0.19
      - future==0.16.0
      - idna==3.10
      - importlib-metadata==4.8.3
      - importlib-resources==5.4.0
      - iniconfig==1.1.1
      - isort==5.10.1
      - lazy-object-proxy==1.7.1
      - mccabe==0.7.0
      - mock==1.3.0
      - node-semver==0.6.1
      - nose==1.3.7
      - packaging==21.3
      - parameterized==0.8.1
      - patch==1.16
      - pbr==6.1.1
      - platformdirs==2.4.0
      - pluggy==1.0.0
      - pluginbase==0.7
      - py==1.11.0
      - pygments==2.14.0
      - pyjwt==1.7.1
      - pylint==2.13.9
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-cov==4.0.0
      - pyyaml==3.13
      - requests==2.27.1
      - six==1.17.0
      - soupsieve==2.3.2.post1
      - tomli==1.2.3
      - tqdm==4.64.1
      - typed-ast==1.5.5
      - typing-extensions==4.1.1
      - urllib3==1.26.20
      - waitress==2.0.0
      - webob==1.8.9
      - webtest==2.0.35
      - wrapt==1.16.0
      - zipp==3.6.0
prefix: /opt/conda/envs/conan

conans/test/unittests/client/conan_output_test.py::ConanOutputTest::test_blocked_output
conans/test/functional/configuration/profile_test.py::ProfileTest::test_package_test
conans/test/functional/configuration/profile_test.py::ProfileTest::test_profile_relative_cwd conans/test/functional/configuration/profile_test.py::ProfileAggregationTest::test_create conans/test/functional/configuration/profile_test.py::ProfileAggregationTest::test_export_pkg conans/test/functional/configuration/profile_test.py::ProfileAggregationTest::test_info conans/test/functional/configuration/profile_test.py::ProfileAggregationTest::test_install
conans/client/output.py conans/util/progress_bar.py conans/client/loader.py
conans/client/output.py conans/util/progress_bar.py conans/client/loader.py