Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

Python

A collection of Python scripts and projects showcasing everything from basic programming to advanced machine learning and web development. Explore, learn, and contribute to a variety of Python code examples. Here’s a description for a Python codes repository on GitHub:


Python Code Repository

Welcome to this collection of Python scripts and projects! This repository is designed to showcase a variety of Python programs ranging from simple exercises to more complex applications. Whether you're a beginner learning Python or an experienced developer seeking examples, you'll find useful code here to learn from, modify, and integrate into your own projects.

Features:

  • Basic Python Programs: Learn the fundamentals of Python through basic scripts like calculators, string manipulation, and loops.
  • Data Science & Machine Learning: Explore Jupyter notebooks with data analysis, visualization, and machine learning models using libraries such as NumPy, Pandas, Matplotlib, and Scikit-learn.
  • Web Development: Example code for building simple web applications using frameworks like Flask or Django.
  • Automation: Scripts for automating repetitive tasks like file management, web scraping, and more.
  • Algorithms & Data Structures: Solutions to common problems with algorithms and data structures to improve your problem-solving skills.

Technologies Used:

  • Python 3.x
  • Libraries: NumPy, Pandas, Matplotlib, Scikit-learn, Flask, Django, etc.
  • Tools: Jupyter, Git, Virtualenv

How to Use:

  1. Clone this repository:
    git clone https://github.com/yourusername/python-codes.git
  2. Navigate to the project folder and install dependencies:
    pip install -r requirements.txt
  3. Run the scripts or explore the notebooks. Using Python on Linux is straightforward because Python is often pre-installed in most Linux distributions. Here's an introduction to get you started with Python on Linux:

1. Check if Python is Installed

Most modern Linux distributions come with Python pre-installed. To check if Python is installed, open your terminal and type:

python3 --version

or

python --version

If Python is installed, this command will display the version of Python installed (e.g., Python 3.x.x). If it's not installed, you can easily install it using the package manager of your distribution.

2. Installing Python on Linux

If Python is not installed, or you want to install a different version, you can use your package manager. Here are the commands for common Linux distributions:

  • Ubuntu/Debian-based:

    sudo apt update
    sudo apt install python3
  • Fedora:

    sudo dnf install python3
  • CentOS/RHEL:

    sudo yum install python3

Once installed, verify the installation again by running python3 --version.

3. Setting Up a Virtual Environment (Optional but Recommended)

Installing Python Packages on Ubuntu/Debian

There are several methods you can use to install Python packages on Ubuntu/Debian systems. Below are some of the most common approaches:

1. Install Python Package Using APT

You can install certain Python packages directly from the system's package manager (APT). For example, to install the requests library for Python 3:

sudo apt install python3-requests

This method installs the library system-wide, and the package will be available to all Python scripts running on the system.

Note: Not all packages on PyPI (Python Package Index) are available via APT. Some packages may not be packaged or included in the Debian/Ubuntu repositories.

2. Create a Virtual Environment Using venv or virtualenv
Install venv

First, ensure that the venv package is installed:

sudo apt install python3-venv
Create a Virtual Environment

To create a virtual environment in a directory named .venv, use the following command:

python3 -m venv .venv
Activate the Virtual Environment

To activate the virtual environment and modify your PATH environment variable, run:

source .venv/bin/activate

Once activated, you can install packages in this isolated environment. For example, to install the requests package:

pip install requests

Packages will be installed under the .venv/ directory and will only be available when the virtual environment is activated.

Deactivate the Virtual Environment

To leave the virtual environment, simply run:

deactivate
Alternative: Run Python Executables Directly

If you prefer not to activate or deactivate the virtual environment every time, you can run executables by specifying their path:

.venv/bin/pip install requests
.venv/bin/python3
>>> import requests
>>> help(requests)

As you can see, pipx installed a symlink in ~/.local/share/pipx/venvs/pycowsay/bin to the executable in a virtual environment:

Here's the output you've provided, formatted in markdown:

### Checking the Executable Files in `pipx` Virtual Environment

You can also inspect the files within the virtual environment created by `pipx` by looking at the `bin/` directory. Here's an example of what you might find in the `~/.local/share/pipx/venvs/pycowsay/bin/` directory:

```bash
ls -l ~/.local/share/pipx/venvs/pycowsay/bin

Output:

total 28
-rw-r--r-- 1 samvat samvat 2086 Jan  6 18:03 activate
-rw-r--r-- 1 samvat samvat  949 Jan  6 18:03 activate.csh
-rw-r--r-- 1 samvat samvat 2224 Jan  6 18:03 activate.fish
-rw-r--r-- 1 samvat samvat 9033 Jan  6 18:03 Activate.ps1
-rwxrwxr-x 1 samvat samvat  252 Jan  6 18:04 pycowsay
lrwxrwxrwx 1 samvat samvat    7 Jan  6 18:03 python -> python3
lrwxrwxrwx 1 samvat samvat   16 Jan  6 18:03 python3 -> /usr/bin/python3
lrwxrwxrwx 1 samvat samvat    7 Jan  6 18:03 python3.12 -> python3
  • The pycowsay executable is listed with execute permissions (-rwxrwxr-x), and it points to the Python environment inside the virtual environment.
  • Other files such as activate, activate.csh, and Activate.ps1 are used to activate the virtual environment for different shell types (e.g., Bash, CSH, PowerShell).
  • The symlinks for python and python3 point to the Python version installed on your system.

This confirms that pipx has properly set up the isolated environment and linked the executable to ~/.local/bin/.

## 3. Use `pipx` to Install Python Applications

`pipx` is a tool that allows you to install and run Python applications in isolated environments. This is the recommended way to install Python packages that are command-line tools.

### Install `pipx`

To install `pipx`, run the following command:

```bash
sudo apt install pipx

Ensure pipx is in Your PATH

Make sure that the directory ~/.local/bin/ is in your PATH. You can add it by running:

pipx ensurepath

You might need to restart your terminal for the changes to take effect.

Install a Package Using pipx

Once pipx is installed, you can easily install Python packages, such as pycowsay, from PyPI:

pipx install pycowsay

Run the Installed Application

You can run the installed command directly from the terminal:

pycowsay Mooo!

Output:

 -----
< Mooo! >
 -----
   \   ^__^
    \  (oo)\_______
       (__)\       )\/\
           ||----w |
           ||     ||

As you can see, pipx creates a symlink to the executable in ~/.local/bin/ and runs it from within an isolated virtual environment:

ls -l ~/.local/bin/pycowsay

Output:

lrwxrwxrwx 1 flimm flimm 50 May 24 11:19 /home/flimm/.local/bin/pycowsay -> /home/flimm/.local/pipx/venvs/pycowsay/bin/pycowsay*

These are three common ways to install and manage Python packages on Ubuntu/Debian systems. Choose the method that best suits your needs!


This guide breaks down the installation methods clearly and is ready for a GitHub README.

### 4. **Installing Python Packages**

To install additional Python packages, you can use `pip`, Python's package manager. For example, to install `numpy`:

```bash
pip install numpy

You can also install a list of packages from a requirements.txt file (often used in Python projects):

pip install -r requirements.txt

5. Running Python Scripts

To run a Python script, navigate to the directory where your .py script is located and run:

python3 script_name.py

Alternatively, you can run Python interactively by just typing:

python3

This opens the Python interpreter, where you can type and execute Python code line by line.

6. Using Python's Integrated Development Environment (IDE)

While you can write Python code using any text editor (e.g., Vim, Nano), you can also use Python-specific IDEs or text editors with Python support, such as:

  • VS Code (requires installation):

    sudo apt install code
  • PyCharm (IDE for Python development, available as free and paid versions)

  • Jupyter Notebooks (especially for data science tasks):

    pip install notebook

You can then start a Jupyter Notebook with:

jupyter notebook

7. Python Documentation and Help

Python has excellent documentation, which you can access online: Python Docs.

Additionally, you can use the built-in help() function in the Python shell for quick reference:

help('modules')  # List available modules
help('os')       # Get help on the os module

8. Common Linux Commands for Python Development

  • Viewing Python file contents:

    cat filename.py
  • Opening Python files in an editor:

    nano filename.py  # or use your preferred text editor like vim or emacs

Conclusion

Using Python on Linux is a seamless experience with powerful tools available for package management, environment isolation, and script execution. By following the steps above, you should be ready to start your Python development journey on Linux! Feel free to fork, contribute, or suggest improvements!


About

A collection of Python scripts and projects showcasing everything from basic programming to advanced machine learning and web development. Explore, learn, and contribute.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors