eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to convert the following installation commands using pip that downloads from another website, into a requirements.txt format, but just can't figure out how. Can anyone assist? pip install torch==1.5.0+cu101 torchvision==0.6.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/index.html 

Answer: The structure of the contents of a requirements.txt file is defined as follows:  [[--option]...] <requirement specifier> [; markers] [[--option]...] <archive url/path> [-e] <local project path> [-e] <vcs project url>   The <requirement specifier> defines the package and an optional version.  SomeProject SomeProject == 1.3 SomeProject >=1.2,<2.0 SomeProject[foo, bar] SomeProject~=1.4.2   The --option (such as the -f/--find-links) is the same as the pip install options you would use if you were doing pip install from the command line.  The following options are supported:  -i, --index-url --extra-index-url --no-index -c, --constraint -r, --requirement -e, --editable -f, --find-links --no-binary --only-binary --require-hashes --pre --trusted-host   So, for your install commands, the requirements.txt would look like this: # Torch --find-links https://download.pytorch.org/whl/torch_stable.html torch==1.5.0+cu101 torchvision==0.6.0+cu101 # Detectron --find-links https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/index.html detectron2  Make sure to verify that the links are correctly used: $ pip install -r requirements.txt Looking in links: https://download.pytorch.org/whl/torch_stable.html, https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/index.html Collecting torch==1.5.0+cu101 (from -r requirements.txt (line 3)) Using cached https://download.pytorch.org/whl/cu101/torch-1.5.0%2Bcu101-cp38-cp38-linux_x86_64.whl Collecting torchvision==0.6.0+cu101 (from -r requirements.txt (line 4)) Using cached https://download.pytorch.org/whl/cu101/torchvision-0.6.0%2Bcu101-cp38-cp38-linux_x86_64.whl Collecting detectron2 (from -r requirements.txt (line 8)) Using cached https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/detectron2-0.1.2%2Bcu101-cp38-cp38-linux_x86_64.whl ...   As a side note, you originally said "(not github)" in your title. The default source of packages installed using pip is hosted on PyPi: https://files.pythonhosted.org/. You can see the actual links when going to the Download Files section of a package in PyPi (example for Torch).
Question: In Python 2 I can install a set of packages via pip preferring binary packages over source packages (meaning: fallback to source if binary not found) with: (1) pip install --prefer-binary -r requirements.txt In Python 3 I can do this with: (2) pip3 install --only-binary=:all: -r requirements.txt But (1) is not exactly equal to (2) since the former says:   Prefer binaries when installing; but if I don't find a binary option, then I'll go with source.  The latter says:  I will fail if no binaries are found; don't even try from source.  So, from the docs it seems that one solution could be to just manually enter each package which should be considered for source installation - meaning: the "only-binary" flag can be provided multiple times on the command line and can thus handle special-cases like that (by emptying it out, or giving specific package names to the binary packages). This answer details, to some extent, that approach: Make pip download prefer to download source-distributions (not wheels). However, I have a large number of both types of packages so I need an automated way like the (1) approach. Question: How can I get a similar automated behavior as (1) but in Python/pip 3? Solution: Pip is not Python - upgrade pip to vs. 20.X and use --prefer-binary.

Answer: Solution: upgrade pip to vs. 20.X and use --prefer-binary
Question: In my settings, I have the TODO bound to highlight in yellow, yet in the actual code it does not highlight. Here is a screenshot of my settings: Editor -> TODO Does anyone know how to fix this? EDIT: I even tried re-installing Pycharm and I still have the issue. EDIT 2: In the TODO Window, it is saying "0 TODO items found in 0 files". I believe this means it is looking in the wrong files to check for TODO items. However, when I try to find TODO items in "this file" it still doesn't work. Does anyone know why this is?

Answer: Go to Preferences (or Settings), Project Structure, and make sure the folder with your files is not in the "Excluded" tab's list. Click the folder you want to include and click on the "Sources" tab. Click Apply, then OK! It should work.
Question: I have a torch tensor of size torch.Size([1, 128, 56, 128]) 1 is channel, 128 is the width, and height. 56 are the stacks of images. How can I resize it to torch.Size([1, 56, 128, 128]) ?

Answer: You could simply use permute or transpose.
Question: Eventually, every single time I install a new Linux distribution I do sudo apt-get install python3.  However, once installed I always get confused. python is Python 2.7 and python3 is Python 3.x. But also it appears that pip is for Python 2 and pip3 for Python 3. That said most tutorials I see on Internet always use the traditional pip install even though it is about Python 3.  How should I deal with this? Should I simply continue to put this annoying 3 every time I use Python (pip3, ipython3, python3...)? In most of my lectures I read that creating a symlink python->python3 is a bad practice. Is that correct?

Answer: Use python3 -m pip or python -m pip. That will use the correct pip for the python version you want. This method is mentioned in the pip documentation:  python -m pip executes pip using the Python interpreter you specified as python. So /usr/bin/python3.7 -m pip means you are executing pip for your interpreter located at /usr/bin/python3.7.  Symlinking python->python3 is a bad idea because some programs might rely on python being python 2. Though, I have seen some Dockerfiles symlink python->python3, like TensorFlow's CPU dockerfile (it's less of an issue in a Docker image). Coincidentally, that same Dockerfile uses the python3 -m pip install syntax that I recommend.
Question: I'm trying to sublclass Path from pathlib, but I failed with following error at instantiation from pathlib import Path class Pl(Path): def __init__(self, *pathsegments: str): super().__init__(*pathsegments)  Error at instantiation AttributeError: type object 'Pl' has no attribute '_flavour'  Update: I'm inheriting from WindowsPath still doesn't work. TypeError: object.__init__() takes exactly one argument (the instance to initialize)

Answer: I solved it. Mokey patching is the way to go. define functions just like this def method1(self, other): blah Path.method1 = method1  The fastest, easiest, most convenient solution, zero downsides. Autosuggest in Pycharm works well. UPDATE: I got THE solution (works with linter and auto suggestor): class MyPath(type(Path()), Path): pass
Question: How do I install Pip for Python 3.8 ? I made 3.8 my default Python version. sudo apt install python3.8-pip  gives  unable to locate package python3.8-pip  and running python3.8 -m pip install [package]  gives  no module named pip  I can't run sudo apt install python3-pip because it installs pip for Python 3.6

Answer: Install pip the official way: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3.8 get-pip.py   made 3.8 my default Python version  It depends on how you did that, but it might break something in your OS. For example some packages on Ubuntu 18.04 might depend on python being python2.7 or python3 being python3.6 with some pip packages preinstalled.
Question: I have a GitHub workflow for Django and when it gets to migrating the database it gives the error django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.  the secret key is stored in a .env file and loaded with  from dotenv import load_dotenv load_dotenv() from pathlib import Path env_path = Path('.') / '.env' load_dotenv(dotenv_path=env_path) SECRET_KEY = os.getenv("secret_key")  Here is the file tree C:. | db.sqlite3 | manage.py | \---djangosite | .env | asgi.py | settings.py | urls.py | wsgi.py | __init__.py | \---__pycache__ ...  This is the manage.py, it is the regular django one with the load .env code from settings.py #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" from dotenv import load_dotenv load_dotenv() from pathlib import Path env_path = Path('.') / '.env' load_dotenv(dotenv_path=env_path) import os import sys def main(): os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangosite.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc execute_from_command_line(sys.argv) if __name__ == '__main__': main()  when I run manage.py on my PC it loads the key and runs the server, but GitHub gives the error above. How do I stop this error from happening?

Answer: If you have stored the SECRET_KEY in your system's environment variable, then for GitHub workflow, you can add a dummy environment variable in the YAML file. The settings.py should look like this import os ... SECRET_KEY = os.environ.get('SECRET_KEY') # Or the name by which you stored environment variable ...  The steps are given below: Step 1: Generate a dummy SECRET_KEY. You can create it yourself by import secrets print(secrets.token_hex(25))  Or generate from a site like this. Step 2: In your .github/workflows YAML file (e.g., django.yml), add this steps: ... - name: Run Tests env: SECRET_KEY: your-genereated-secret_key run: | python manage.py test  Then everything will work fine with the same version of code in your local environment, production environment, and GitHub workflow.
Question: I am relatively new to python, and while attempting to train a chatbot I received the error: ‘UnboundLocalError: local variable 'logs' referenced before assignment‘. I used model.fit to train: model.fit(x_train, y_train, epochs=7)  And I received the error: UnboundLocalError Traceback (most recent call last) <ipython-input-10-847c83704a3f> in <module>() 2 x_train, 3 y_train, ----> 4 epochs=7 5 ) 1 frames /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs) 64 def _method_wrapper(self, *args, **kwargs): 65 if not self._in_multi_worker_mode(): # pylint: disable=protected-access ---> 66 return method(self, *args, **kwargs) 67 68 # Running inside `run_distribute_coordinator` already. /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing) 854 logs = tmp_logs # No error, now safe to assign to logs. 855 callbacks.on_train_batch_end(step, logs) --> 856 epoch_logs = copy.copy(logs) 857 858 # Run validation. UnboundLocalError: local variable 'logs' referenced before assignment  I ran this in google colab, with the link here: https://colab.research.google.com/drive/18uTvvKYDrd8CQi31kg6vX2Dbxg1gD20X?usp=sharing I used the chatterbot/english dataset on kaggle: https://www.kaggle.com/kausr25/chatterbotenglish 

Answer: This issue looks similar to the problem I had while working with small datasets and it is covered in this thread: #38064. I solved my particular issue setting a smaller batch_size, in my case: batch_size = 2
Question: I have this dataclass: from dataclasses import dataclass, field from typing import List @dataclass class Person: name: str dob: str friends: List['Person'] = field(default_factory=list, init=False)  name and dob are immutable and friends is mutable. I want to generate a hash of each person object. Can I somehow specify which field to be included and excluded for generating the __hash__ method? In this case, name and dob should be included in generating the hash and friends shouldn't. This is my attempt but it doesn't work @dataclass class Person: name: str = field(hash=True) dob: str = field(hash=True) friends: List['Person'] = field(default_factory=list, init=False, hash=False)  >>> hash(Person("Mike", "01/01/1900")) Traceback (most recent call last): File "<pyshell#43>", line 1, in <module> hash(Person("Mike", "01/01/1900")) TypeError: unhashable type: 'Person'  I also can't find a way to set name and dob to be frozen. And I'd refrain from setting unsafe_hash to True, just by the sound of it. Any suggestions? Also, is what I'm doing considered good practice? If not, can you suggest some alternatives? Thank you Edit: This is just a toy example and we can assume that the name and dob fields are unique. Edit: I gave an example to demonstrate the error.

Answer: Just indicate that the friends field should not be taken in account when comparing instances with __eq__, and pass hash=True to field instances on the desired fields. Then, pass the unsafe_hash=True argument to the dataclass decorator itself - it will work as you intend (mostly): In case of hash, the language restriction is that if one instance compares equal with another (__eq__), the hash of of both must be equal as well. The implication in this case is that if you have two instances of the "same" person with the same "name" and "dob" fields, they will be considered equal, even if they feature different friends lists. Other than that, this should work:  from dataclasses import dataclass, field from typing import List @dataclass(unsafe_hash=True) class Person: name: str = field(hash=True) dob: str = field(hash=True) friends: List['Person'] = field(default_factory=list, init=False, compare=False, hash=False)  Then, remember to behave like a "consenting adult" and not change the "name" and "dob" fields of Person instances in any place, and you are set.
Question: I have an Excel (.xlsx) file that has two columns of phrases. For example: John I have a dog Mike I need a cat Nick I go to school  I want to import it in Python and to get a list of tuples like: [('John', 'I have a dog'), ('Mike', 'I need a cat'), ('Nick', 'I go to school'), ...]  What could I do?

Answer: You can read the excel file using pd.read_excel. You need to care about the header is there are some or not. As you said, it returns a dataframe. In my case, I have the following. df = pd.read_excel("data.xlsx") print(df) # name message # 0 John I have a dog # 1 Mike I need a cat # 2 Nick I go to school  Then, it's possible to have the values of the dataframe using to_numpy. It return a numpy array.  If you want a list, we use the numpy method tolist to convert it as list: out = df.to_numpy().tolist() print(out) # [['John', 'I have a dog'], # ['Mike', 'I need a cat'], # ['Nick', 'I go to school']]  As you can see, the output is a list of list. If you want a list of tuples, just cast them: # for getting list of tuples out = [tuple(elt) for elt in out] print(out) # [('John', 'I have a dog'), # ('Mike', 'I need a cat'), # ('Nick', 'I go to school')]   Note: An older solution was to call values instead of to_numpy(). However, the documentation clearly recommends using to_numpy and forgive values. Hope that helps !
Question: Why the first code outputs 51 and the second code outputs 21. I understand the second code should output 21, but the way I understood, the first code should also output 21 (The value of b changed to 20 and then is calling the function f). What am I missing?  b = 50 def f(a, b=b): return a + b b = 20 print(f(1))  Output: 51 b = 50 b = 20 def f(a, b=b): return a + b print(f(1))  Output: 21 Edit: This is different from How to change default value of optional function parameter in Python 2.7? because here the unintentional change happening to the default parameter is being discussed, not how to intentionally change the value of default parameter, ie here the question focuses on how the python interpreter treats the position of function definition for functions having default parameters. 

Answer: Tip for python beginners : If you use IDEs like pycharm - you can put a debugger and see what is happening with the variables. We can get a better understanding of what is going on using the id(b) which gets us the address of the particular object in memory:  Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. CPython implementation detail: This is the address of the object in memory.  Let me modify your code to the following : b = 50 print("b=50 :", id(b)) def f(a, b=b): print("b in the function f :", id(b)) print(id(b)) return a + b b = 20 print("b=20 :", id(b)) print(f(1))  The output is as following: b=50 : 4528710960 b=20 : 4528710000 b in the function f : 4528710960 4528710960 51  As you can see the b inside the function and the b=50 have the same address. When you do b=20 a new object was created. In Python, (almost) everything is an object. What we commonly refer to as "variables" in Python are more properly called names. Likewise, "assignment" is really the binding of a name to an object. Each binding has a scope that defines its visibility, usually the block in which the name originates. In python  When you do  b=50 a binding of b to an int object is created in the scope of the block When we later say b=20 the int object b=50 is unaffected. These both are essentially two different objects. You can read more about it in these links.  Is Python call-by-value or call-by-reference? Neither. Parameter Passing Python id()
Question: When I execute a command python setup.py install or python setup.py develop it would execute build_ext command as one of the steps. How can I pass --debug option to it as if it was invoked as python setup.py build_ext --debug? UPDATE Here is a setup.py very similar to mine: https://github.com/pybind/cmake_example/blob/11a644072b12ad78352b6e6649db9dfe7f406676/setup.py#L43 I'd like to invoke python setup.py install but turn debug property in build_ext class instance to 1.

Answer: A. If I am not mistaken, one could achieve that by adding the following to a setup.cfg file alongside the setup.py file: [build_ext] debug = 1  B.1. For more flexibility, I believe it should be possible to be explicit on the command line: $ path/to/pythonX.Y setup.py build_ext --debug install  B.2. Also if I understood right it should be possible to define so-called aliases # setup.cfg [aliases] release_install = build_ext install debug_install = build_ext --debug install  $ path/to/pythonX.Y setup.py release_install $ path/to/pythonX.Y setup.py debug_install  References  https://docs.python.org/3/distutils/configfile.html https://setuptools.readthedocs.io/en/latest/setuptools.html#alias-define-shortcuts-for-commonly-used-commands
Question: I'm making a dataclass with a field for which I'd like there to only be a few possible values. I was thinking something like this: @dataclass class Person: name: str = field(default='Eric', choices=['Eric', 'John', 'Graham', 'Terry'])  I know that one solution is to validate arguments in the __post_init__ method but is there a cleaner way using something like the syntax above?

Answer: python 3.8 introduced a new type called Literal that can be used here: from dataclasses import dataclass from typing import Literal @dataclass class Person: name: Literal['Eric', 'John', 'Graham', 'Terry'] = 'Eric'  Type checkers like mypy have no problems interpreting it correctly, Person('John') gets a pass, and Person('Marc') is marked as incompatible. Note that this kind of hint requires a type checker in order to be useful, it won't do anything on its own when you're just running the code. If you're on an older python version and can't upgrade to 3.8, you can also get access to the Literal type through the official pip-installable backport package typing-extensions, and import it with from typing_extensions import Literal instead.  If you need to do actual checks of the passed values during runtime, you should consider using pydantic to define your dataclasses instead. Its main goal is to extend on dataclass-like structures with a powerful validation engine which will inspect the type hints in order to enforce them, i.e. what you considered to write by hand in the __post_init__.
Question: I installed the latest version of Python 3.8.2 on a Windows 10 machine. I previously had Python 3.7, which I uninstalled and confirmed in the System PATH it was no longer referenced.  After installing the latest version, I run through CMD as Admin: py -m venv env  and I get this error: Error: [WinError 2] The system cannot find the file specified: 'C:\Users\test_user\Documents\app_test\env' I know the Python Path is in the System Path environmental settings, but not specifically for the user (don't know if that makes a difference?). I have also tried to uninstall virtualenv using powershell and reinstaling, but have the same result. Any ideas on where else to look to solve this?

Answer: I found out that Windows Defender now has a feature that blocks access/changes to system files. It added my documents folder by default, somehow preventing me from creating any folders in CMD despite having Admin Access. I hope this helps someone else!! In short -- you may have to revise or disable your Windows 10 "Ranswomare Security Protection" setting to allow you to write files to your directories.
Question: My setup.py has the following console_scripts as entry points: entry_points={ 'console_scripts': ['script=myapp.app:do_something', 'script2=myapp.app:do_something2'], },  with the following structure . ├── myapp │ ├── __init__.py │ ├── app.py │ ├── mod.py │ ├── mod2.py │ └── submodules │ ├── __init__.py │ └── mod3.py ├── requirements.txt └── setup.py  and app looking like ##my_app.app def do_something(): #do stuff def do_something2(): #do other stuff  How can I get VS code debug configuration to enter at these module attributes. I have this that can run the module if I use if __name__ == "__main__": do_something() but want seperate launch.json files depending on the console_scripts ##launch.json { "configurations": [ { "name": "Python: Module", "type": "python", "request": "launch", "cwd": "${workspaceFolder}", "module": "myapp.app", "args": ["--hello world"] } ] }  Thought you might be able to do simlar with:  "module": "myapp.app:do_something",  but alas: No module named myapp.app:do_something 

Answer: There currently isn't a way to make this work. At minimum you need a separate module per entry point or have a single module that took a command-line argument that then chose which function to call.