1

I have an issue with virtual environment, I'm learning python so really do not know what is wrong here.

I have a folder on my desktop called 'learning' in which I am trying to make a virtual environment called venv.

When I am in VSCode I am using the terminal and write python -m venv venv which spits out the following:

PS C:\Users\XXX\Desktop\learning> virtualenv venv
created virtual environment CPython3.9.5.final.0-64 in 1553ms
  creator CPython3Windows(dest=C:\Users\XXX\Desktop\learning\venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\XXX\AppData\Local\pypa\virtualenv)
    added seed packages: pip==21.1.2, setuptools==57.0.0, wheel==0.36.2
  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
PS C:\Users\XXX\Desktop\learning> python -m venv venv
[{'first': 'Csr', 'last': 'vR'}, {'first': 'Jessie', 'last': 'vdd'}, {'first': 'Bill', 'last': 'Gates'}]
Traceback (most recent call last):
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 15, in <module>
    import importlib.util
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\importlib\util.py", line 2, in <module>
    from . import abc
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\importlib\abc.py", line 17, in <module>
    from typing import Protocol, runtime_checkable
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\typing.py", line 22, in <module>
    import collections.abc
ModuleNotFoundError: No module named 'collections.abc'; 'collections' is not a package
PS C:\Users\XXX\Desktop\learning> python -m venv try
[{'first': 'Csr', 'last': 'vR'}, {'first': 'Jessie', 'last': 'vdd'}, {'first': 'Bill', 'last': 'Gates'}]
Could not import runpy module
Traceback (most recent call last):
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 15, in <module>
    import importlib.util
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\importlib\util.py", line 2, in <module>
    from . import abc
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\importlib\abc.py", line 17, in <module>
    from typing import Protocol, runtime_checkable
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python39\lib\typing.py", line 22, in <module>
    import collections.abc
ModuleNotFoundError: No module named 'collections.abc'; 'collections' is not a package

As you can see for some reason it is trowing back code from a file called collections.py in the folder 'learning'.. no idea why. Now when I use "virtualenv venv" it works fine, but I am trying to understand why the other command is not working.

10
  • please edit the question Commented Jun 16, 2021 at 20:27
  • 1
    Try python3 -m venv venv Commented Jun 16, 2021 at 20:28
  • 1
    Where is that list in your output coming from? Commented Jun 16, 2021 at 20:35
  • Also, python -m venv venv is supposed to create a virtual environment, but you've already created one using virtualenv. I suspect python -m venv is not executing the venv module in the standard library, but some other module you have locally with the same name. Commented Jun 16, 2021 at 20:36
  • 1
    suggested you to do what @It_is_Chris did, it makes the question more readable. Commented Jun 17, 2021 at 6:28

3 Answers 3

1

You have already created the virtual environment (venv). No need to type python -m venv venv again.

To activate virtual environment (venv), type

PS C:\Users\XXX\Desktop\learning> venv/Scripts/activate

This will activate the virtual environment named venv in the powershell.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, First of thank you for taking your time to think along :). I forgot to mention that between the 2 commands I removed the folder, so the virtual environment did not exist anymore when I fired the second command (also got the same output when I tried python -m venv venv the first time without thinking I could use virtualenv venv.
1

it's coming from a python file in the same folder called 'collections.py'

That module is somehow shadowing the collections module in the standard library. When typing.py tries to import collections.abc, the import mechanism is finding your module named collections first, but your module isn't a package that contains a module name abc, so you get the error.

The simplest fix would be to rename your own file to something else. However, Python introduced a difference between absolute and relative imports to address conflicts like this. There might be a way to structure your directory to avoid your local module from shadowing the standard library module instead.

virtualvenv is entirely different script from the venv module; it likely does not try to import the standard collections module, so the name collision doesn't affect it.

1 Comment

You are 100% right my good sir, renamed it to collectionss to test it out and everything works as intended now. Props to you.
0

In VSCode you may also activate the venv by selecting it in the bottom left hand corner. This is an easy reference to confirm which environment you are using.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.