Home Backend Development Python Tutorial How to install python egg

How to install python egg

Jul 05, 2019 am 09:20 AM
python

How to install python egg

Students who are often exposed to Python may notice that when they need to install a third-party python package, the easy_install command may be used. easy_install is a command included in the setuptools package developed by PEAK (Python Enterprise Application Kit). It is used to install the egg package. The egg package is currently the most popular way to package and deploy Python applications.

How to make and install egg package? I will briefly analyze it below.

1.1 Install setuptools

First, install the setuptools tool. You can directly use apt installation under Debian/Ubuntu: :

$ sudo apt-get install python-setuptools
Copy after login

If you install it manually, there are two ways:

Install through the boot program ez_setup.py. This boot program will download the latest version of setuptools from the Internet for installation, and can also update the local setuptools.

$ wget http://peak.telecommunity.com/dist/ez_setup.py
$ sudo python ez_setup.py
Copy after login

Update setuptools::

$ sudo python ez_setup.py -U setuptools
Copy after login

Or download the egg package of setuptools to install. The latest version download can be viewed here. After downloading, install it through sh.

$ wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg
$ sudo sh setuptools-0.6c11-py2.6.egg
Copy after login

Now you can use the easy_install command to install other egg packages.

1.2 Make your own egg package

I always install other people’s egg packages, do you also want to make your own egg package? Okay, next we will make a simple egg bag ourselves. First, create the project directory egg-demo and initialize a setup.py file:

$ mkdir egg-demo
$ cd egg-demo
$ touch setup.py
$ ls
setup.py
Copy after login

The following is mainly to fill in setup.py. setup.py is actually the configuration file of the python tool package distutils, and setuptools is based on distutils. Configure packaging information through the setup function in setup.py. First, we need to introduce the function setup of setuptools. The setup of setuptools is actually the setup function of distutils. Fill in setup.py with the following content:

$ cat setup.py
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from setuptools import setup
setup()
Copy after login

At this point, an empty egg configuration file is written. We can use the following command to generate the egg package:

$ python setup.py bdist_egg
Copy after login

Let’s take a look at what is generated:

$ ls -F
build/ dist/ setup.py UNKNOWN.egg-info/
Copy after login

You can see three more folders. Under the dist folder, there is an egg file: UNKNOWN-0.0.0-py2.6.egg. Egg laying successful! Let’s first take a look at the format of this egg file:

$ file dist/UNKNOWN-0.0.0-py2.6.egg
dist/UNKNOWN-0.0.0-py2.6.egg: Zip archive data, at least v2.0 to extract
Copy after login

Oh, it turns out to be a zip compressed package! Okay, let’s take a look at the internal structure:

$ unzip -l dist/UNKNOWN-0.0.0-py2.6.egg
Archive:  dist/KNOWN-0.0.0-py2.6.egg
  Length      Date    Time    Name
---------  ---------- -----   ----
      120  2010-12-06 17:04   EGG-INFO/SOURCES.txt
        1  2010-12-06 17:04   EGG-INFO/top_level.txt
        1  2010-12-06 17:04   EGG-INFO/zip-safe
        1  2010-12-06 17:04   EGG-INFO/dependency_links.txt
      227  2010-12-06 17:04   EGG-INFO/PKG-INFO
---------                     -------
      350                     5 files
Copy after login

There is only one EGG-INFO folder, containing five egg information files, which is gone. The name of this egg is unknown, version 0.0.0. This is because we haven't set anything in the setup. Obviously, this egg can't do anything. Now add some ingredients to it. In setup.py, the setup function receives a series of properties as configuration parameters.

name name is the name of the egg package and the name of the folder to be packaged. The default is UNKNOWN.

version version number, default 0.0.0

packages Another function of setuptools, find_packages, is used here. As the name suggests, find_packages is used to package files in the specified directory.

zip_safe The default is False, so that the contents of the project file will be checked every time the egg package is generated to ensure that it is correct.

There are also some descriptive attributes, such as description, long_description, author, author_email, license, keywords, platform, url, etc. Fill in the setup.py file as follows: :

$ cat setup.py
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from setuptools import setup, find_packages
setup(
        name = "demo",
        version="0.1.0",
        packages = find_packages(),
        zip_safe = False,
        description = "egg test demo.",
        long_description = "egg test demo, haha.",
        author = "amoblin",
        author_email = "amoblin@ossxp.com",
        license = "GPL",
        keywords = ("test", "egg"),
        platforms = "Independant",
        url = "",
        )
Copy after login

Create a directory demo with the same name as the above name in the egg-demo directory, and write the __init__.py file in the demo directory:

$ mkdir demo
$ cat demo/__init__.py
#!/usr/bin/env python
#-*- coding:utf-8 -*-
def test():
    print "Hello, I'm amoblin."
if __name__ == '__main__':
    test()
Copy after login

Generate again After the egg package, check the egg package information:

$ python setup.py bdist_egg
$ unzip -l dist/demo-0.1.0-py2.6.egg
Archive:  dist/demo-0.1.0-py2.6.egg
  Length      Date    Time    Name
---------  ---------- -----   ----
      121  2010-12-06 17:30   demo/__init__.py
      344  2010-12-06 17:46   demo/__init__.pyc
      137  2010-12-06 17:46   EGG-INFO/SOURCES.txt
        5  2010-12-06 17:46   EGG-INFO/top_level.txt
        1  2010-12-06 17:46   EGG-INFO/zip-safe
        1  2010-12-06 17:46   EGG-INFO/dependency_links.txt
      227  2010-12-06 17:46   EGG-INFO/PKG-INFO
---------                     -------
      836                     7 files
Copy after login

You can see that there is an additional folder demo, which contains the __init__.py we wrote. Adhering to the agile principle, install it first and experience it before saying:

$ sudo python setup.py install
running install
install_dir /usr/local/lib/python2.6/dist-packages/
...
creating /usr/local/lib/python2.6/dist-packages/demo-0.1.0-py2.6.egg
Extracting demo-0.1.0-py2.6.egg to /usr/local/lib/python2.6/dist-packages
demo 0.1.0 is already the active version in easy-install.pth
Installed /usr/local/lib/python2.6/dist-packages/demo-0.1.0-py2.6.egg
Processing dependencies for demo==0.1.0
Finished processing dependencies for demo==0.1.0
Copy after login

OK! Installation completed! Next we can use it directly through import!

$ python -c "from demo import test;test()"
Hello, I'm amoblin.
Copy after login

Successful output! This means the installation is correct. One of our egg bags was born. Normally, our source programs are placed in the src directory, so next we move the demo folder to src. But this also requires modifying the setup.py file, modifying the parameter in the find_packages function to 'src', and adding the package_dir parameter:

packages=find_packages('src'),
package_dir = {'':'src'}
Copy after login

This tells setuptools to look for packages in the src directory instead of the original default project root directory. .

1.3 Egg file uninstallation

Taking the python2.6 version as an example, the egg file is generally installed in the /usr/local/lib/python2.6/dist-packages/ directory. There is also an easy-install.pth file used to store the installed egg information. :

$ cd /usr/local/lib/python2.6/dist-packages
$ cat easy-install.pth|grep demo
./demo-0.1.0-py2.6.egg
$ ls -F|grep demo
demo-0.1.0-py2.6.egg/
Copy after login

Uninstalling the egg file is very simple, first delete the line containing this egg from easy-install.pth, and then delete the egg folder.

Related recommendations: "Python Tutorial"

The above is the detailed content of How to install python egg. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles