简体   繁体   中英

Issue with virtual environment - python

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM