eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to add more mypy type annotations to my existing codebase. I have a file that uses a lot of bs4. When I run the mypy checker on this file I get the error: error: Skipping analyzing "bs4": module is installed, but missing library stubs or py.typed marker  In the mypy docs on "Missing library stubs" it says:  Mypy will not try inferring the types of any 3rd party libraries you have installed unless they either have declared themselves to be PEP 561 compliant stub package (e.g. with a py.typed file) or have registered themselves on typeshed, the repository of types for the standard library and some 3rd party libraries.  But when I look on typeshed, there is a set of stubs. How do I fix that error and tell it to look at the stub definition from typeshed? BS4 and mypy are both running the latest version. This question asks a similar question about black, but when I test black in my code mypy doesn't complain but it seems that they've solved it in a different way as there's no black stub in typeshed.

Answer: Here is the content of mypy typeshed. It doesn't include third-party libraries, limited only to standard library. So types for beautifulsoup are not shipped with mypy (and it would be really odd to do so, because these stubs are updated sometimes more often than mypy itself, plus other typecheckers rely on this typeshed). So you need to install required stubs separately (package home page): pip install types-beautifulsoup4
Question: I am a beginner and was "playing around" with environments a bit. I came across a situation where it seemed that I had two environments activated: I create a directory, create an environment with venv, activate it and then also conda activate a conda environment which I created before. These are the commands: mkdir dummie_directory cd dummie_directory python -m venv . Scripts\activate conda activate old_env  After this the beginning of my command line looks like this: (old_env)(dummie_directory) C:\Users\....  Does this mean that both environments are active? Is there any intended use for this or will it most likely lead to some kind of clash/conflict between the installed packages? Thanks

Answer: No, it does not mean they are both activated. Only one can have priority in the PATH, which is what I’d consider the simplest definition of what “activated” means, functionally. The indicators in the PS1 string (i.e., the shell’s prompt string) are not robustly managed. The two environment managers are simply unaware of each other, and the string is only manipulated when an activate or deactivate procedure is called. There isn’t any dynamic monitoring that a particular environment is remaining active. I wouldn’t rely on any behavior you observe in this state. It does not have a defined specification and is not intended to be used like this.
Question: I'm using python and I need to plot a route in a map. I have a dataframe that looks like this:  latitude longitude 41.393095 -8.703483 41.393095 -8.703483 41.393095 -8.703483 41.392483 -8.703088 40.942170 -8.540572 40.942188 -8.540567 41.187624 -8.568143 41.321009 -8.711874 41.345618 -8.547567  The order of the dataframe represents the order of the route and I would like to plot it based on latitude and longitude. But i only find ways to plot it based on osm node IDs. Does anyone know a way of plotting this route with the exact geographic coordinates? Thanks!

Answer: Using This tutorial, I managed to plot the points on a map:  # Create a bounding box to determine the size of the required map BBox = (df.longitude.min()-0.1, df.longitude.max()+0.1, df.latitude.min()-0.1, df.latitude.max()+0.1) # Downloaded using this tutorial: https://medium.com/@abuqassim115/thanks-for-your-response-frank-fb869824ede2 map_img = plt.imread('map.png') # Plotting the points on the graph fig, ax = plt.subplots(figsize=(8, 7)) ax.plot(df.longitude, df.latitude, 'xb-') # Setting limits for the plot ax.set_xlim(BBox[0], BBox[1]) ax.set_ylim(BBox[2], BBox[3]) # Showing the image behind the points ax.imshow(map_img, zorder=0, extent=BBox, aspect='equal') plt.show()
Question: Let's say I have an image I want to downsample to half its resolution via either grid_sample or interpolate from the torch.nn.functional library. I select mode ='bilinear' for both cases. For grid_sample, I'd do the following: dh = torch.linspace(-1,1, h/2) dw = torch.linspace(-1,1, w/2) mesh, meshy = torch.meshgrid((dh,dw)) grid = torch.stack.((meshy,meshx),2) grid = grid.unsqueeze(0) #add batch dim x_downsampled = torch.nn.functional.grid_sample(img, grid, mode='bilinear')  For interpolate, I'd do: x_downsampled = torch.nn.functional.interpolate(img, size=(h/2,w/2), mode='bilinear')  What do both methods differently? Which one is better for my example?

Answer: Interploate is limited to scaling or resizing the input. Grid_sample is more flexible and can perform any form of warping of the input grid. The warping grid can specify the position where each element in the input will end up in the output. In simple terms, interpolate does not provide the ability to change the ordering of elements in the input grid (an element to the right of another element will still be to the right after interpolation). Grid_sample is capable of changing the order of the elements and any arbitrary order can be achieved based on warping grid that is passed to the function. A simple 2D illustration to show grid_sample in action: input = [[10, 20], [30, 40]] grid (contains coordinates) = [[(1,0),(0,0)], [(1,0),(0,0)]] output will be: [[20, 10], [30, 40]]  For your example, while both can work, interpolate would be the preferred way to go.
Question: I'm trying to install a Jupyter kernel for my poetry environment, but it seems like the kernel gets my base conda environment. Here's what I'm trying: poetry env list >ENV_NAME-HASH-py3.9 (Activated) poetry run which python >/Users/myusername/Library/Caches/pypoetry/virtualenvs/ENV_NAME-HASH-py3.9/bin/python poetry run ipython kernel install --name=ENV_NAME >Installed kernelspec ENV_NAME in /Users/myusername/Library/Jupyter/kernels/ENV_NAME  Then if I open a Jupyter with this kernel I don't get the libraries that should be installed. Checking the Python version I get: !which python /Users/myusername/opt/anaconda3/bin/python  Any help appreaciated!

Answer: Bonjour, I come tardily but I think I have your solution. If you just want to use your currently configured Poetry environment in VSCode with your .ipynb notebooks, you can do this for example:  Create a new project with Poetry: poetry new test_ipynb cd test_ipynb   Use a specific Python version with your environment: poetry env use 3.12   Add ipykernel to your environment: poetry add ipykernel   Check your environment's executable path: poetry env info  Example output: /home/guillaume/.cache/pypoetry/virtualenvs/test-ipynb-pH8Jwd_U-py3.12/bin/python   Open your project in VSCode: code .    Then, in VSCode:  Install Jupyter extension   Need to reload window after Ctrl + Shift + p Reload Window   In the top right, click on the Select Kernel.   Choose "Python Environments".   And select the environment you got from the env info command.    This should allow you to use your Poetry-managed virtual environment directly within VSCode for Jupyter notebooks. Additionally, if you want to use Jupyter or JupyterLab, just follow the steps above and add: poetry add jupyter poetry run jupyter notebook # For JupyterLab poetry add jupyterlab poetry run jupyter lab  This ensures that your development environment is consistent across different tools and platforms, leveraging Poetry's dependency management and virtual environment capabilities directly within VSCode for Jupyter notebooks.
Question: I want to define a UserDict that reads values from JSON and stores a position for a given key. The JSON file looks like this: { "pages": [ { "areas": [ { "name": "My_Name", "x": 179.95495495495493, "y": 117.92792792792793, "height": 15.315315315315303, "width": 125.58558558558553 }, ... ] } ] }  I would like to indicate to type linters (e.g. MyPy) that this dictionary as a key being a string and the values being a Position. My current code is the following: import json from collections import UserDict from dataclasses import dataclass, field from pathlib import Path from typing import Dict, List, Optional, Union from typing_extensions import Literal JsonPosition = Dict[str, Union[str, float]] JsonPage = Optional[Dict[Literal["areas"], List[JsonPosition]]] @dataclass class Position: """Information for a position""" name: str x: float y: float width: float height: float @classmethod def from_json(cls, dict_values: JsonPosition): return cls(**dict_values) # type: ignore # dynamic typing class Page(UserDict): """Information about positions on a page""" @classmethod def from_json(cls, page: JsonPage): """Get positions from JSON Dictionary""" if page is None: return cls() return cls({cast(str, p["name"]): Position.from_json(p) for p in page["areas"]}) JSON = Path("my_positions.json").read_text() positions = json.loads(JSON) page_1 = Page.from_json(positions["pages"][0])  I would like MyPy (or Pylance or whatever type hinter I use), to automatically recognize page_1["My_Name"] as being a Position. What could I change?

Answer: Actually, you can directly provide the type to UserDict with square brackets ([...]) like you would with a Dict: class Page(UserDict[str, Position]): ...  For Python 3.6 or earlier, this will not work. For Python >=3.7 and <3.9, you need the following to subscript collections.UserDict and put it in a separate block specific to type checking (with constant TYPE_CHECKING): from __future__ import annotations from collections import UserDict from typing import TYPE_CHECKING if TYPE_CHECKING: TypedUserDict = UserDict[str, Position] else: TypedUserDict = UserDict class Page(TypedUserDict): ...  For Python 3.9+, no additional import or trick is necessary.
Question: I had always thought that f-strings invoked the __str__ method. That is, f'{x}' was always the same as str(x). However, with this class class Thing(enum.IntEnum): A = 0  f'{Thing.A}' is '0' while str(Thing.A) is 'Thing.A'. This example doesn't work if I use enum.Enum as the base class. What functionality do f-strings invoke?

Answer: From "Formatted string literals" in the Python reference: f-strings invoke the "format() protocol", meaning that the __format__ magic method is called instead of __str__. class Foo: def __repr__(self): return "Foo()" def __str__(self): return "A wild Foo" def __format__(self, format_spec): if not format_spec: return "A formatted Foo" return f"A formatted Foo, but also {format_spec}!" >>> foo = Foo() >>> repr(foo) 'Foo()' >>> str(foo) 'A wild Foo' >>> format(foo) 'A formatted Foo' >>> f"{foo}" 'A formatted Foo' >>> format(foo, "Bar") 'A formatted Foo, but also Bar!' >>> f"{foo:Bar}" 'A formatted Foo, but also Bar!'  If you don't want __format__ to be called, you can specify !s (for str), !r (for repr) or !a (for ascii) after the expression: >>> foo = Foo() >>> f"{foo}" 'A formatted Foo' >>> f"{foo!s}" 'A wild Foo' >>> f"{foo!r}" 'Foo()'  This is occasionally useful with strings: >>> key = 'something\n nasty!' >>> error_message = f"Key not found: {key!r}" >>> error_message "Key not found: 'something\\n nasty!'"
Question: I am trying to make the Django tutorial codes polls into async with uvicorn async view. ORM query works with async view by wrapping in sync_to_async() as such. question = await sync_to_async(Question.objects.get, thread_sensitive=True)(pk=question_id)  But I have no idea how to apply sync_to_async or thread inside Django templates. This code fails saying 'You cannot call this from an async context - use a thread or sync_to_async.' Or any other way to work around this? {% for choice in question.choice_set.all %}  I use Python 3.10, Django 4.0.4 and uvicorn 0.17.6

Answer: I have found several solutions for that: It's not safe. In your settings.py add: import os os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"  Or get your Question object in separate function: @sync_to_async def get_question(question_id): return get_object_or_404(Question, pk=question_id) question = await get_question(pk=question_id)  Or without splitting (works with Django 4.1+): question = await Question.objects.aget(id=question_id)
Question: I'm trying to install pandas via pip install pandas on my laptop. Environment:  Window 11 Pro Python 3.10.4 Pip version 22.0.4  Compatibility:  Officially Python 3.8, 3.9 and 3.10. You must have pip>=19.3 to install from PyPI.   C:\Users\PC>pip install pandas WARNING: Ignoring invalid distribution -ywin32 (c:\users\pc\appdata\local\programs\python\python310-32\lib\site-packages) WARNING: Ignoring invalid distribution -ywin32 (c:\users\pc\appdata\local\programs\python\python310-32\lib\site-packages) Collecting pandas Using cached pandas-1.4.2.tar.gz (4.9 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Requirement already satisfied: numpy>=1.21.0 in c:\users\pc\appdata\local\programs\python\python310-32\lib\site-packages (from pandas) (1.22.4) Requirement already satisfied: python-dateutil>=2.8.1 in c:\users\pc\appdata\local\programs\python\python310-32\lib\site-packages (from pandas) (2.8.2) Collecting pytz>=2020.1 Using cached pytz-2022.1-py2.py3-none-any.whl (503 kB) Requirement already satisfied: six>=1.5 in c:\users\pc\appdata\local\programs\python\python310-32\lib\site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0) Building wheels for collected packages: pandas Building wheel for pandas (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for pandas (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [2010 lines of output] C:\Users\PC\AppData\Local\Temp\pip-build-env-q3kdt5nb\overlay\Lib\site-packages\setuptools\config\setupcfg.py:459: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead. warnings.warn(msg, warning_class)   ...    note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for pandas Failed to build pandas ERROR: Could not build wheels for pandas, which is required to install pyproject.toml-based projects  What I have tried:  updated pip to 22.1.1 installed wheel 0.37.1 uninstalled and installed pip uninstalled and installed python 3.10.4   Error still reproducible with pandas 1.5.1  Thanks to @AKX which has pointed up that there is no and may will no 32-bit version of pandas in the future. See the discussion on GitHub.

Answer: Pandas doesn't require Anaconda to work, but based on python310-32 in your output, you're using a 32-bit build of Python. Pandas evidently does not ship 32-bit wheels for Python 3.10 (they do have win32 wheels for Python 3.8 and Python 3.9 though). (There could be alternate sources for pre-built 32-bit wheels, such as Gohlke's site.) In other words, on that platform you would need to install Pandas from source, which will likely be a rather difficult undertaking, and can't be done directly within pip anyway (as you noticed via error: metadata-generation-failed). If your system is capable of running 64-bit Python, you should switch to it.
Question: I'm using WSL2 with Ubuntu on Windows 11 v2004.2022.10 and I have both Python 3.8 and 3.9 installed. I want to make the 3.9 version the default, and I'm happy to remove Python 3.8 altogether if necessary. If I type python --version in Ubuntu, I get Python 3.8.10. I tried the following: sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 0  and if I type sudo update-alternatives --config python I see There are 2 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3.9 1 auto mode 1 /usr/bin/python3.8 0 manual mode 2 /usr/bin/python3.9 1 manual mode  However, if I type again python3 --version, it still says Python 3.8.10 I then tried sudo update-alternatives --remove python /usr/bin/python3.8 and now sudo update-alternatives --config python tells me that There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python3.9 Nothing to configure. And yet, python3 --version still says Python 3.8.10 I also tried sudo update-alternatives --set python /usr/bin/python3.9 and that didn't work either. This works: alias python='/usr/bin/python3.9': now python3 --version is Python 3.9.5 - but only temporarily, as upon closing and reopening Ubuntu it reverts to Python 3.8.10. I then tried creating a permanent alias by adding that same line to my .bashrc script (I followed these steps), and the same thing happened. I'm new to all of this, so please be patient. How can I change the default Python 3.8 to the 3.9 version, and/or remove Python 3.8 altogether? I tried deleting the python3.8 directory but that didn't work. Perhaps it's because I still have python3.8-config, which I didn't manage to delete? Thanks!

Answer: You can always use sudo update-alternatives --config python3 and then select your python3 version. That should solve your issue without needing to do weird configs.
Question: I am trying to install Cartopy on Ubuntu and need to install proj v8.0.0 binaries for Cartopy. However when I try to apt-get install proj-bin I can only get proj v6.3.1. How do I install the latest (or at least v8.0.0) proj for cartopy?

Answer: I'm answering my own question here partly to help others with this problem, and partly as an archive for myself so I know how to fix this issue if I come across it again. I spent quite a while trying to figure it out, and wrote detailed instructions, so see below: Installing cartopy is a huge pain, and I've found using conda to be a very bad idea (it has bricked itself and python along with it multiple times for me) THIS INSTALLATION IS FOR LINUX. Step 0. Update apt: apt update  Step 1. Install GEOS: Run the following command to install GEOS: apt-get install libgeos-dev  In case that doesn't do it, install all files with this: apt-get install libgeos-dev libgeos++-dev libgeos-3.8.0 libgeos-c1v5 libgeos-doc  Step 2. Install proj dependencies:  Install cmake:  apt install cmake   Install sqlite3:  apt install sqlite3   Install curl devlopment package:  apt install curl && apt-get install libcurl4-openssl-dev  Step 3. Install Proj Trying apt-get just in case it works: Unfortunately, cartopy requires proj v8.0.0 as a minimum, but if you install proj using apt you can only install proj v6.3.1 Just for reference in case anything changes, this is the command to install proj from apt: apt-get install proj-bin  I'm fairly sure this is all you need, but in case it's not, this command will install the remaining proj files: apt-get install proj-bin libproj-dev proj-data  To remove the above installation, run: apt-get remove proj-bin  or: apt-get remove proj-bin libproj-dev proj-data  Building Proj from source So if the above commands don't work (it's not working as of 2022/4/8), then follow the below instructions to install proj from source:  Go to your install folder and download proj-9.0.0 (or any version with proj-x.x.x.tar.gz):  wget https://download.osgeo.org/proj/proj-9.0.0.tar.gz   Extract the tar.gz file:  tar -xf proj-9.0.0.tar.gz   cd into the folder:  cd proj-9.0.0   Make a build folder and cd into it:  mkdir build && cd build   Run (this may take a while):  cmake .. cmake --build . cmake --build . --target install   Run to make sure everything installed correctly:  ctest  The test command failed on one test for me (19 - nkg), but otherwise was fine. You should find the required files in the ./bin directory Finally:  Move binaries to the /bin directory:  cp ./bin/* /bin   As per Justino, you may also need to move the libraries:  cp ./lib/* /lib  Now after all this, you can finally install cartopy with pip: pip install cartopy  After doing this, my cartopy still wasn't working. I went home to work on this next week, came back, and all of a sudden it was working so maybe try restarting
Question: I am trying to run this code: import cv2 import numpy as np src = np.array([[10, 20, 40, 50], [50, 20, 50, 20], [10, 10, 30, 60], [20, 40, 60, 70]]) dst1 = cv2.blur(src, ksize=(3, 3), borderType = cv2.BORDER_CONSTANT) print(dst1) dst2 = cv2.GaussianBlur(src, ksize=(3, 3), sigmaX=0, borderType = cv2.BORDER_CONSTANT)  And I got an error:  dst2 = cv2.GaussianBlur(src, ksize=(3, 3), sigmaX=0, borderType = cv2.BORDER_CONSTANT) cv2.error: OpenCV(4.5.5) /Users/runner/work/opencv-python/opencv-python/opencv/modules/imgproc/src/filter.simd.hpp:3045: error: (-213:The function/feature is not implemented) Unsupported combination of source format (=4), and buffer format (=5) in function 'getLinearRowFilter' 

Answer: if you construct an np.array like that, it's (default) format is np.int32, which is not supported. rather make it: src = np.array([[10, 20, 40, 50], [50, 20, 50, 20], [10, 10, 30, 60], [20, 40, 60, 70]], np.uint8) # <-- correct type !!!
Question: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives from distutils.dir_util import copy_tree  Is it something that I should worry about? Which import should I use to replace the from distutils.dir_util import copy_tree line?

Answer: Change from: from distutils.dir_util import copy_tree copy_tree(loc_source, loc_destination)  To: import shutil shutil.copytree(loc_source, loc_destination)  Or, to replace existing files and folders: import shutil shutil.copytree(loc_source, loc_destination, dirs_exist_ok=True)  Documentation: https://docs.python.org/3/library/shutil.html#shutil.copytree
Question: I built a package using poetry package manager but I regret naming it because it sounds a bit childish. Besides, because poetry's default behavior is to force change the project's name to lower case (SuperPackage --> superpackage), it is difficult to import the package inside/outside the package's main directory. Here's an example directory structure: SuperPackage/ - superpackage/ - mysubpackage/ - __init__.py - utils.py - foo.py - tests/ - __init__.py - test_superpackage.py - poetry.lock - pyproject.toml - README.md - README.rst - .gitignore  Because of this structure, from SuperPackage.mysubpackage import utils # Works outside SuperPackage/ directory. Doesn't work inside SuperPackage/. from superpackage.mysubpackage import utils # Works inside SuperPackage/. Doesn't work outside SuperPackage/ directory.  Now, I want to change SuperPackage to nicepackage. How do I achieve this? I can't google it maybe because it's very uncommon or it's too obvious. Should I just change "name" field in pyproject.toml file? However, I'm not sure if it's okay (and recommended) to change "name" field directly. [tool.poetry] name = "SuperPackage" version = "0.1.0" description = "" authors = ["John-Doe <johndoe@gmail.com>"] [tool.poetry.dependencies] python = "^3.8" pandas = "^1.3.4" matplotlib = "^3.4.3" beautifulsoup4 = "^4.10.0" 

Answer: Change the name field in pyproject.toml, run poetry update (not sure that's needed but do it just in case?) and change the directories names. Note that the name of the virtual environment on your computer will stay the same, but that probably isn't a problem as it only is a local ting.