I can’t run correctly a uvicorn server on vscode, versions3.9.2 64 bit among newest ones

I’m learning Python on VSCode, initially I had installed Python 3.9.2 64 bit on my Debian system, I arrived a part of the course where needed to use.

The following line of code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>id: str | None
</code>
<code>id: str | None </code>
id: str | None

Then Python started to say that “the alternative sintax for unions requires Python 3.10 o posterior.”, then I installed 3.10.6 64 bits and 3.12.5 64 bits.

If I use one of the versions that that are equals to 3.10 or superior then the error of “syntax for unions” disappears, but then appears errors from som apis y/or modules that are imported previously are shown as errors, because they can’t be imported, for example Pydantic.

I already checked that all the needed API’s are installed.

UPDATE 07/09/2024:

The following functions are defined for a router in the api but Pylance reports that the “user” function remains hidden by another declaration of the same name

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>router.get("/{id}")
async def user(id: int):
return search_user(id)
# Query
@router.get("/")
async def user(id: int, name: str):
return search_user(id)
def search_user(id: int):
users = filter(lambda user: user.id == id, users_list)
try:
return list(users)[0]
except:
return {"error": "No se ha encontrado el usuario"}
</code>
<code>router.get("/{id}") async def user(id: int): return search_user(id) # Query @router.get("/") async def user(id: int, name: str): return search_user(id) def search_user(id: int): users = filter(lambda user: user.id == id, users_list) try: return list(users)[0] except: return {"error": "No se ha encontrado el usuario"} </code>
router.get("/{id}")
async def user(id: int):
    return search_user(id)

    
# Query

@router.get("/")
async def user(id: int, name: str):
    return search_user(id)
    
def search_user(id: int):
    users = filter(lambda user: user.id == id, users_list)
    try:
        return list(users)[0]
    except:
        return {"error": "No se ha encontrado el usuario"}

How can solve this?

2

Syntax like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>id: str | None
</code>
<code>id: str | None </code>
id: str | None

is only compatible with python3.10+. If you want to use it, you must use new python versions.

Using python3.9

However, you can still use python3.9, but you will have to rewrite this line like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from typing import Optional
id: Optional[str]
</code>
<code>from typing import Optional id: Optional[str] </code>
from typing import Optional

id: Optional[str]

or, another option

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from typing import Union
id: Union[str, None]
</code>
<code>from typing import Union id: Union[str, None] </code>
from typing import Union

id: Union[str, None]

Upgrading to python3.10+

If you want to upgrade to newer python versions (and I personally recommend it to use latest features), you should install newer versions of packages (pydantic; etc.), because old packages versions can be not compatible with new python versions.

Try creating new virtual environment (if you use them) and install latest packages versions (recommended way), or just use commands like pip install pydantic --upgrade for all of your failing packages.

Upgrading note

Probably, you will have to refactor your code. Some packages modules or functions were removed in latest versions (for example, if you have used pydantic 1.* and upgraded to pydantic 2.*, this package had a lot of changes). Check out failing messages and try to use new packages modules, that replaced deprecated ones. (If you don’t want to deal with those refactors, you can always downgrade python back to 3.9 and use type annotations as I mentioned before).

If you will still have errors, provide concrete packages runtime/import failure messages on python 3.10/3.12, and we will try to fix them.

3

if you upgraded the python version to the latest version. You may need to update other dependencies. You can use the pip list to see the installed libraries, and then use the pip install upgrade <package> to update the libraries. Alternatively, you can use ./yourvenv/scripts/activate to enter the environment and then try to update the library. This can also avoid some dependency conflicts and then run the project in the environment.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật