Most voted questions in Python

0 votes
1 answer

Import a module from a relative path?

Assuming that both your directories are real ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
1,266 views
0 votes
1 answer

How can I safely create a nested directory?

On Python ≥ 3.5, use pathlib.Path.mkdir: from pathlib import ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
370 views
0 votes
1 answer

How to import a module given the full path?

For Python 3.5+ use: import importlib.util spec = importlib.util.spec_from_file_location("module.name", ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
2,102 views
0 votes
1 answer

Calling a function of a module by using its name (a string).

Assuming module foo with method bar: import foo method_to_call = getattr(foo, 'bar') result ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
412 views
0 votes
1 answer

How to import a module given its name as string?

With Python older than 2.7/3.1, that's pretty ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
855 views
0 votes
1 answer

How to find all occurrences of a substring?

There is no simple built-in string function ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
696 views
0 votes
1 answer

How to match all occurrences of a regex?

Using scan should do the trick: string.scan(/regex/) READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
2,740 views
0 votes
1 answer

How to find the occurrences of any word with more than two vowels in a file using regex?

for word in read: <--- iterating ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
2,444 views
0 votes
1 answer

How to merge multiple json objects into a single json object using python

You may do like this but it ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
18,748 views
0 votes
1 answer

Python and pip, list all versions of a package that's available?

The script at Pastebin does work. However, ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
5,043 views
0 votes
1 answer

Installing specific package versions with pip

TL;DR: pip install -Iv (i.e. pip install -Iv MySQL_python==1.2.2) First, I ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
764 views
0 votes
1 answer

How can I install pip on Windows?

Python 2.7.9+ and 3.4+ Python 3.4 (released March 2014) ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
444 views
0 votes
1 answer

Python Error: ModuleNotFoundError: No module named '<moduleName>'

Python3 and pip are most likely pointing ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
6,574 views
0 votes
1 answer

I am not able to install pywin32 into my Macos ..I tried all the ways and I could not fix the error...can you please help me

Hello @Harikrishna, For your query you can refer ...READ MORE

Nov 23, 2020 in Python by Niroj
• 82,880 points
5,869 views
0 votes
1 answer

invalid literal for int() with base 10 in python

Hello, You can try this simple code: def getSum(n): ...READ MORE

Nov 23, 2020 in Python by Niroj
• 82,880 points
1,899 views
0 votes
1 answer

I've been trying to run this code, but the error says "Expected an indented block" for the line, " word_as_list[index] = guess."

Hi, @Paradox, The error message IndentationError: expected an indented ...READ MORE

Nov 21, 2020 in Python by anonymous
• 65,910 points
872 views
0 votes
1 answer

How to Install pip for python 3.7 on Ubuntu 18?

The command you're looking for is: python3.7 -m ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
9,010 views
0 votes
1 answer

How are Python metaclasses different from regular class inheritance?

Python 3 class MyClass(object): = New-style class class MyClass: = New-style ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
313 views
0 votes
1 answer

How to get the current time in Python?

Use: >>> import datetime >>> datetime.datetime.now() datetime.datetime(2009, 1, 6, 15, ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
465 views
0 votes
1 answer

How do I parse XML in Python?

First build an Element instance root from the XML, ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
623 views
0 votes
1 answer

How do I install a Python package with a .whl file?

I just used the following which was ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
4,839 views
0 votes
1 answer

How to create a zip archive of a directory in Python?

Hii, The easiest way is to use shutil.make_archive. It ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
661 views
0 votes
1 answer

How do I update pip itself from inside my virtual environment?

Hello, pip is just a PyPI package like any other; you ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
4,743 views
0 votes
1 answer

ImportError: No module named requests

Hello, To install requests module for Python2: $ sudo apt-get install ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
679 views
0 votes
1 answer

How can I open multiple files using “with open” in Python?

Hello, Just replace and with , and you're done: try: ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
1,310 views
0 votes
1 answer

How to set environment variables in Python?

Hii, Environment variables must be strings, so use os.environ["DEBUSSY"] ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
659 views
0 votes
1 answer

Why not inherit from List<T>?

There are some good answers here. I ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
352 views
0 votes
4 answers

How to print instances of a class using print()?

You need to implement your own __repr__ ...READ MORE

Nov 25, 2020 in Python by Saksham Azad
978 views
0 votes
2 answers

How can I safely create a nested directory?

No need to put filename.txt For directory check path ...READ MORE

Nov 25, 2020 in Python by Deepak Chaurasia
1,391 views
0 votes
1 answer

How to run code when a class is subclassed?

Classes (by default) are instances of the ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
383 views
0 votes
0 answers

Which python projects are good for beginner If I already know the basics very well?

Which python projects are good for beginner ...READ MORE

Nov 19, 2020 in Python by samar
• 120 points
267 views
0 votes
1 answer

I have created python chatbot using flask framework using yml file now i want to use json file what should i do now ?

Hello @golu , Try something like this: with open(yaml_file, 'r') ...READ MORE

Nov 19, 2020 in Python by Niroj
• 82,880 points
1,418 views
0 votes
1 answer

How to get the home directory in Python?

Hello, You need to use os.path.expanduser. This will ensure it works ...READ MORE

Nov 19, 2020 in Python by Niroj
• 82,880 points
1,498 views
0 votes
1 answer

How do I parse XML in Python?

Hello @kartik, You can use BeautifulSoup: from bs4 import BeautifulSoup x="""<foo> ...READ MORE

Nov 19, 2020 in Python by Niroj
• 82,880 points
320 views
0 votes
1 answer

How do I create a constant in Python?

Hello @kartik, No there is not. You cannot ...READ MORE

Nov 19, 2020 in Python by Niroj
• 82,880 points
1,218 views
0 votes
1 answer

How can I safely create a nested directory?

Python 3.5+: import pathlib pathlib.Path('/my/directory').mkdir(parents=True, exist_ok=True) pathlib.Path.mkdir as used above ...READ MORE

Nov 19, 2020 in Python by Gitika
• 65,910 points
2,765 views
0 votes
1 answer

How to call an external command?

Look at the subprocess module in the standard library: import ...READ MORE

Nov 19, 2020 in Python by Gitika
• 65,910 points
382 views
0 votes
1 answer

Speed up millions of regex replacements in Python 3

One thing you can try is to ...READ MORE

Nov 19, 2020 in Python by Gitika
• 65,910 points
1,546 views
0 votes
1 answer

How to use multiprocessing pool.map with multiple arguments?

The answer to this is version- and ...READ MORE

Nov 19, 2020 in Python by Gitika
• 65,910 points
13,804 views
0 votes
1 answer

How do I get the number of elements in a list?

Hello, The len() function can be used with several different ...READ MORE

Nov 18, 2020 in Python by Niroj
• 82,880 points
456 views
0 votes
1 answer

How to access environment variable values?

Hello @kartik, To check if the key exists ...READ MORE

Nov 18, 2020 in Python by Niroj
• 82,880 points
305 views
0 votes
1 answer

How do I pass a variable by reference?

Hello, Just wrap it in a list: def Change(self, ...READ MORE

Nov 18, 2020 in Python by Niroj
• 82,880 points
323 views
0 votes
1 answer

How do I list all files of a directory?

Hii, Try this: import os os.listdir("somedirectory") Thank you!! ...READ MORE

Nov 18, 2020 in Python by Niroj
• 82,880 points
356 views
0 votes
1 answer

Error:AttributeError: 'NoneType' object has no attribute 'extend'

Hello @kartik, You can use itertools.chain(): import itertools list2d = [[1,2,3], ...READ MORE

Nov 18, 2020 in Python by Niroj
• 82,880 points
3,054 views
0 votes
1 answer

How can I create a nested directory?

Hello, Using try except and the right error ...READ MORE

Nov 18, 2020 in Python by Niroj
• 82,880 points
360 views
0 votes
1 answer

How to call an external command?

Hello @kartik, Look at the subprocess module in the standard ...READ MORE

Nov 18, 2020 in Python by Niroj
• 82,880 points
343 views
0 votes
1 answer

How to generate all permutations of a list?

Hello @kartik, Try this: def permutations(head, tail=''): ...READ MORE

Nov 17, 2020 in Python by Niroj
• 82,880 points
697 views
0 votes
1 answer

How to concatenate items in a list to a single string?

Hello @kartik, Use join: >>> sentence = ['this','is','a','sentence'] >>> '-'.join(sentence) 'this-is-a-sentence' Hope it ...READ MORE

Nov 17, 2020 in Python by Niroj
• 82,880 points
487 views
0 votes
1 answer

How to test multiple variables against a value?

Hello @kartik, Your problem is more easily addressed ...READ MORE

Nov 17, 2020 in Python by Niroj
• 82,880 points
379 views