Featured

TechBytes on Linux

This is a growing list of Linux commands which might come handy for the of Linux users. 1. Found out i had to set the date like this: ...

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, April 28, 2023

How to debug Python code in VS Code with arguments passed from command line

 Debugging Python code in VS Code with arguments passed from the command line is a straightforward process. Here are the steps you can follow:


1. Open the Python file that you want to debug in VS Code.

2. Set breakpoints in the code where you want to pause and inspect variables.

3. Open the Debug panel in VS Code by clicking on the Debug icon in the Activity Bar or by pressing `Ctrl+Shift+D` on Windows or `Cmd+Shift+D` on Mac.

4. Click on the "create a launch.json file" button and select "Python" as the environment.

5. In the launch.json file that opens, modify the "args" attribute to include the command-line arguments you want to pass to the Python script. For example:


   ```

   "args": ["arg1", "arg2"]

   ```


   Replace "arg1" and "arg2" with the actual arguments you want to pass.

NOTE: Add the below property in the launch.json file.   

"purpose": ["debug-in-terminal"]

6. Save the launch.json file.

7. Start the debugging process by clicking on the "Start Debugging" button in the Debug panel or by pressing `F5`.

8. The code execution will pause at the first breakpoint. Use the Debug Console to inspect variables, and use the Debug toolbar to step through the code and continue the execution.


That's it! You can now debug your Python code with arguments passed from the command line in VS Code.

Thursday, March 30, 2023

Regular Expressions



Regular Expressions
List of meta characters:

. ---> Any one character
? ---> Zero or one
+ ---> One or more
* ---> zero or more
^ ---> at the beginning of the string
$ ---> at the end of the string
[abc] ---> any one of a b c
{m} ---> 'm' times
{m,n} ---> at least m times, at most n times
| ---> or
\ ---> escape sequence character
\s ---> a space
\d ---> a digit
\w ---> a word
\b ---> a word boundary

examples:
\d ---> a single digit number (0 to 9)
\d\d ---> a two digit number (0 to 99)
\d\d\d ---> a three digit number (000 to 999)

NOTE: ?, +, *, {} are used as Quantifiers (to represent quantity)

\d{3} ---> same as above
\d{3,5} ---> either 3 digit or 5 digit number

hell?o ---> helo | hello
hell+o ---> hello | helllo | helllllo | ...
hrll*o ---> helo | hello | helllo | helllllo | ...
he(ll)+o ---> hello | hellllo | hellllllo | ...

S = "hi hello how are hello"

hello ---> Yes
^hello ---> No
hello$ ---> Yes
\d+ ---> a number
[0123456789]+ ---> a number
[0-9]+ ---> a number
\d{3} ---> a 4 digit ODD number

\d+\s\w+ ---> a number then a space then a word
\d+\s[a-z]+ ---> a number then a space then lowercase word
\d+\s[A-K764]{5,10} ---> a number then a space then min 5 max 10 char uppercase word between A - K 764
\d{3}:\d{2}:\d{4} --->
(ABC)?\d{3}:\d{4}[XYZ]? --->

Monday, July 20, 2020

Installing pip with get-pip.py in a Python virtual environment on Windows MacOS & Linux

  • To create a Python virtual environment type below commands on the required console prompt:

On Windows
    python -m venv ./venv
    .\venv\Scripts\activate
On Ubuntu
    python3 -m venv ./venv
     source ./venv/bin/activate    
There are some exceptional cases where vent creation might fail. Here are some workarounds -
https://askubuntu.com/questions/1268833/error-command-path-to-env-bin-python3-7-im-ensurepip-upgrade
123

To exit a Python virtual environment, you can use the deactivate command. This command is available in most Unix-like systems and Windows.

Example

$ deactivate

Steps to Exit Virtual Environment

1. Using deactivate Command

The most common way to exit a virtual environment is by using the deactivate command1.

Example:

$ deactivate

2. Using source deactivate

In some cases, especially with certain virtual environment management tools like virtualenvwrapper, you might need to use the source deactivate command2.

Example:

source deactivate

  • To install pip, securely download get-pip.py by following this link: get-pip.py. Alternatively, use curl:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Then run the following command in the folder where you have downloaded get-pip.py:

python get-pip.py

Warning

 

Be cautious if you are using a Python install that is managed by your operating system or another p

ackage manager. get-pip.py does not coordinate with those tools, and may leave your system in an inconsistent state. Hence it is recommended to use venv (Python Virtual Environment) to isolate the working environment with the Python environment at the system level.
Found this article to be a gem to troubleshoot pip errors on Windows -- https://jhooq.com/pip-install-connection-error/

To install python3 & virtualenv on MacOS

Python3 Virtualenv Setup

Requirements:

  • Python3
  • Pip3
$ brew install python3 #upgrade

Pip3 is installed with Python3

Installation

To install virtualenv via pip run:

$ pip3 install virtualenv

Usage

Creation of virtualenv:

$ virtualenv -p python3 <desired-path>

Activate the virtualenv:

$ source <desired-path>/bin/activate

Deactivate the virtualenv:

$ deactivate

You can see more about the Homebrew on the official page.


Popular Posts