How to use pip install
How to use pip install
Installing Packages With Pip
AugustoVal on Sept. 18, 2019
I hope you are doing well,
Quick question. Are these requests directories?
Thanking you in advances
Dan Bader RP Team on Sept. 18, 2019
The built-in dir function returns a list of valid attributes for an object. So these are not directory names or anything related to the filesystem, but methods and variables on the requests module.
brunofl on Jan. 2, 2020
Nice! I had no idea about this feature of using pip to install directly from git repo.
theartofdonaldlsmith on Nov. 27, 2020
Dan, Sometimes you illustrate with just pip, other times with pip3. How do we know when to use which? or does it matter?
Dan Bader RP Team on Nov. 28, 2020
Once you’ve activated the virtual environment pip will refer to the correct version of the pip package manager automatically, so you no longer need to use the pip3 command at that point.
theartofdonaldlsmith on Nov. 28, 2020
I guess this will make more since to me after I learn more about virtual environments. I know what virtual environments are and I’ve used them for running Windows 7 in on a windows 10 computer. I use PyCharm and it setups up a VE automatically for each project. After finishing all of lesson 2, I now understand the value of using VEs.
At first I was skeptical of buying this course. I figured I could watch the first lesson and if I wasn’t learning anything useful I could get a refund. While I’m retired and learning Python for fun, I’m glad I bought your course. I’ve taken a lot of python classes online, but none of them have even touched PyPI and the package library. I’ve seen it, but just thought it was another package like NumPy, and I didn’t fully understand it’s purpose.
Dan Bader RP Team on Nov. 30, 2020
Thanks for your comment there Don, I really appreciate the feedback. Happy Pythoning 🙂
Installing packages using pip and virtual environments¶
This guide discusses how to install packages using pip and a virtual environment manager: either venv for Python 3 or virtualenv for Python 2. These are the lowest-level tools for managing Python packages and are recommended if higher-level tools do not suit your needs.
This doc uses the term package to refer to a Distribution Package which is different from an Import Package that which is used to import modules in your Python source code.
Installing pip¶
pip is the reference Python package manager. It’s used to install and update packages. You’ll need to make sure you have the latest version of pip installed.
You can also install pip yourself to ensure you have the latest version. It’s recommended to use the system pip to bootstrap a user installation of pip:
Afterwards, you should have the latest version of pip installed in your user site:
The Python installers for Windows include pip. You can make sure that pip is up-to-date by running:
Afterwards, you should have the latest version of pip:
Installing virtualenv¶
If you are using Python 3.3 or newer, the venv module is the preferred way to create and manage virtual environments. venv is included in the Python standard library and requires no additional installation. If you are using venv, you may skip this section.
virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.
Creating a virtual environment¶
venv (for Python 3) and virtualenv (for Python 2) allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments. It is always recommended to use a virtual environment while developing Python applications.
To create a virtual environment, go to your project’s directory and run venv. If you are using Python 2, replace venv with virtualenv in the below commands.
venv will create a virtual Python installation in the env folder.
Activating a virtual environment¶
You can confirm you’re in the virtual environment by checking the location of your Python interpreter:
Installing specific package version with pip
However, when installed, it still shows MySQL_python-1.2.3-py2.6.egg-info in the site packages. Is this a problem specific to this package, or am I doing something wrong?
11 Answers 11
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
TL;DR:
What these options mean:
The download link 404s and the fallback URL links are re-directing infinitely due to sourceforge.net’s recent upgrade and PyPI’s stale URL.
So to properly install the driver, you can follow these steps:
You can even use a version range with pip install command. Something like this:
One way, as suggested in this post, is to mention version in pip as:
To install a specific python package version whether it is the first time, an upgrade or a downgrade use:
MySQL_python version 1.2.2 is not available so I used a different version. To view all available package versions from an index exclude the version:
Sometimes, the previously installed version is cached.
It returns the followings:
Requirement already satisfied: pillow==5.2.0 in /home/ubuntu/anaconda3/lib/python3.6/site-packages (5.2.0)
Since this appeared to be a breaking change introduced in version 10 of pip, I downgraded to a compatible version:
This command tells pip to install a version of the module lower than version 10. Do this in a virutalenv so you don’t screw up your site installation of Python.
This below command worked for me
-I will not uninstall the existing package before proceeding; it will just install it on top of the old one. This means that any files that should be deleted between versions will instead be left in place. This can cause weird behavior if those files share names with other installed modules.
In my case, this manifested with strange syntax errors because the newer version of the package added a file that was only compatible with Python 3, and when I downgraded package versions to support Python 2, I continued importing the Python-3-only module.
MichaelKim0407/tutorial-pip-package
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
TUTORIAL: How to create your own pip library
The idea of pip roots back to the import keyword in Python, and that the keyword works for both standard library and user-defined modules.
While user-defined modules are often single-use and not very complicated, it can be helpful that they can be reused across different projects without copy-pasting, or even shared with other developers.
Add modules to the standard Python library.
This is not a good approach because every developer needs different libraries, so increasing the size of the Python distribution is not beneficial. Also, code in the standard library should have a higher standard and have less flexibility when changes are needed.
Modify PYTHONPATH environment variable.
While this can work locally on one machine, modifying the system setup can be problematic when it comes to distribution/deployment, and it has a high chance of messing things up on other parts of the system.
pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes.
Recommendations for this tutorial
It is recommended to create a virtual environment and do everything in it for the purpose of this tutorial, so that you won’t mess up your python installation.
For Python 3.6+, you may use the venv module in the standard library. HOWTO
After creating the virtual environment, it might be a good idea to update the base packages we are going to use:
Step 1: Create an importable module!
Confirm that it can be imported properly:
Checkout the repo at this stage using the 01-create-module tag.
Step 2: Create setup.py
setup.py is used to tell pip how to install the package. You can find the full documentation here.
For this tutorial we will have the most basic setup ready, and expand upon it.
Change url and author info for yourself.
Add this to my_pip_package.py :
To confirm that setup.py works properly:
Now, you should be able to import the package outside of the folder:
If you have pushed your code to a git hosting service, you should be able to install it anywhere right now:
(replace with your own repo url)
Checkout the repo at this stage using the 02-setup-py tag.
Step 3: Convert to multi-file package
This step is optional, if you want to keep everything in one file. However, the setup is slightly different so we’ll keep this as a separate step.
First, turn the Python module into a package:
Add another Python file in the package, e.g. math.py :
Change the following lines in setup.py :
Test that everything works:
Checkout the repo at this stage using the 03-convert-package tag.
Step 4: Adding dependencies
First, let’s add the following code to math.py :
To specify returns-decorator as a dependency, add the following entry to setup(. ) in setup.py :
Now verify that it works:
Checkout the repo at this stage using the 04-dependency tag.
Step 5: Adding optional (extra) dependencies
Sometimes certain parts of your code require a specific dependency, but it’s not necessarily useful for all use cases.
One example would be the sqlalchemy library, which supports a variety of SQL dialects, but in most cases anyone using it would only be interested in one dialect.
Installing all dependencies is both inefficient and messy, so it’s better to let the user decide what exactly is needed. However, it would be cumbersome for the user to install the specific dependencies. This is where extra dependencies some in.
For this tutorial, after the last step, let’s pretend that we don’t want to always install returns-decorator unless math is used. We can replace the install_requires with the following:
Note the s : install_requires is singular but extras_require is plural.
Now, we can install the extra dependency by appending [math] in the installation:
One good habit is to make a [dev] extra dependency, which includes all dependencies needed for local development. In setup.py :
Checkout the repo at this stage using the 05-extra-dependency tag.
Step 6: Command line entries
pip allows packages to create command line entries in the bin/ folder.
Now, add the following entry to setup(. ) :
The __name__ == ‘__main__’ part is not really needed, so let’s remove it.
Also, since the add command requires the [math] dependency, let’s make it explicit for anyone wishing to use the command:
Checkout the repo at this stage using the 06-command tag.
Step 7: Adding tests!
If you are developing a package you should probably include tests from the beginning, but since it’s a different step in the setup we’ll do it now.
For this tutorial, we’ll be using pytest for testing and pytest-cov for coverage.
Lets include the packages in the extras:
and update the [dev] extra dependency to include testing:
For the sake of length, we’ll add to the repo without writing them down here. Run pytest to test the package.
Once everything’s passed, we can move on for coverage test.
Checkout the repo at this stage using the 07-tests tag.
Step 8: Adding tests to CI
While testing locally can catch a lot of problems already, running tests automatically is a further step on quality control, especially multiple developers are involved, and it also shows the world that your library is indeed working as intended.
For GitHub repos, we’ll be using Travis CI to run the CI tests.
We’ll be using Coveralls for coverage reporting. (There is an alternative called Codecov, however it has a pretty significant issue for Python.)
First, coveralls requires an extra dependency, so let’s create an extra called ci :
Let’s also add the badges to the top of our README file so everyone can see them immediately. The code to embed badges can be found on travis and coveralls. After the CI runs successfully, the badges will be updated.
Checkout the repo at this stage using the 08-ci tag.
Step 9: Releasing on pypi!
At this point, your library can already be shared with the world, however it is not on pypi yet.
To release on pypi, there are a few things we need to take care of.
Next, change the name of your package, if you followed the tutorial thus far, since my_pip_package would be taken by me. Be creative! The name argument in setup() does not need to match the name of the python package, but it’s better to keep them the same so that anyone that installs your library won’t be confused.
Once everything is good, we can package the library:
Finally, to upload the file:
It would also be a good idea to create a release on GitHub, and drop the packaged file as an attachment.
Checkout the repo at this stage using the 09-release tag.
About
Tutorial on how to create your own pip package
Get started using Python for web development on Windows
The following is a step-by-step guide to get you started using Python for web development on Windows, using the Windows Subsystem for Linux (WSL).
Set up your development environment
We recommend installing Python on WSL when building web applications. Many of the tutorials and instructions for Python web development are written for Linux users and use Linux-based packaging and installation tools. Most web apps are also deployed on Linux, so this will ensure you have consistency between your development and production environments.
If you are using Python for something other than web development, we recommend you install Python directly on Windows using the Microsoft Store. WSL does not support GUI desktops or applications (like PyGame, Gnome, KDE, etc). Install and use Python directly on Windows for these cases. If you’re new to Python, see our guide: Get started using Python on Windows for beginners. If you’re interested in automating common tasks on your operating system, see our guide: Get started using Python on Windows for scripting and automation. For some advanced scenarios, you may want to consider downloading a specific Python release directly from python.org or consider installing an alternative, such as Anaconda, Jython, PyPy, WinPython, IronPython, etc. We only recommend this if you are a more advanced Python programmer with a specific reason for choosing an alternative implementation.
Install Windows Subsystem for Linux
WSL lets you run a GNU/Linux command line environment integrated directly with Windows and your favorite tools, like Visual Studio Code, Outlook, etc. We generally recommend using WSL 2 for Python web development work.
To enable and install WSL 2, see the WSL install documentation. These steps will include choosing a Linux distribution (for example, Ubuntu).
Consider installing the new Windows Terminal from the Microsoft Store to enable multiple tabs (quickly switch between multiple Linux command lines, Windows Command Prompt, PowerShell, Azure CLI, etc), create custom key bindings (shortcut keys for opening or closing tabs, copy+paste, etc.), use the search feature, and set up custom themes (color schemes, font styles and sizes, background image/blur/transparency). Learn more.
Set up Visual Studio Code
Take advantage of IntelliSense, Linting, Debug support, Code snippets, and Unit testing by using VS Code. VS Code integrates nicely with the Windows Subsystem for Linux, providing a built-in terminal to establish a seamless workflow between your code editor and your command line, in addition to supporting Git for version control with common Git commands (add, commit, push, pull) built right into the UI.
Create a new project
Let’s create a new project directory on our Linux (Ubuntu) file system that we will then work on with Linux apps and tools using VS Code.
Close VS Code and open Ubuntu 18.04 (your WSL command line) by going to your Start menu (lower left Windows icon) and typing: «Ubuntu 18.04».
Install Python, pip, and venv
Create a virtual environment
Using virtual environments is a recommended best practice for Python development projects. By creating a virtual environment, you can isolate your project tools and avoid versioning conflicts with tools for your other projects. For example, you may be maintaining an an older web project that requires the Django 1.2 web framework, but then an exciting new project comes along using Django 2.2. If you update Django globally, outside of a virtual environment, you could run into some versioning issues later on. In addition to preventing accidental versioning conflicts, virtual environments let you install and manage packages without administrative privileges.
A Security Alert will pop-up from Windows Defender, select «Allow access». Once VS Code opens, you should see the Remote Connection Host indicator, in the bottom-left corner, letting you know that you are editing on WSL: Ubuntu-18.04.
Close your Ubuntu terminal. Moving forward we will use the WSL terminal integrated into VS Code.
Open the WSL terminal in VS Code by pressing Ctrl+` (using the backtick character) or selecting View > Terminal. This will open a bash (WSL) command-line opened to the project folder path that you created in your Ubuntu terminal.
Install the Microsoft Python extension
Open the VS Code Extensions window by entering Ctrl+Shift+X (or use the menu to navigate to View > Extensions).
In the top Search Extensions in Marketplace box, enter: Python.
Find the Python (ms-python.python) by Microsoft extension and select the green Install button.
Run a simple Python program
Python is an interpreted language and supports different types of interpretors (Python2, Anaconda, PyPy, etc). VS Code should default to the interpreter associated with your project. If you have a reason to change it, select the interpreter currently displayed in blue bar on the bottom of your VS Code window or open the Command Palette (Ctrl+Shift+P) and enter the command Python: Select Interpreter. This will display a list of the Python interpreters that you currently have installed. Learn more about configuring Python environments.
Let’s create and run a simple Python program as a test and ensure that we have the correct Python interpreter selected.
Open the VS Code File Explorer window by entering Ctrl+Shift+E (or use the menu to navigate to View > Explorer).
If it’s not already open, open your integrated WSL terminal by entering Ctrl+Shift+` and ensure that your HelloWorld python project folder is selected.
Paste this Python code into your test.py file and then save the file (Ctrl+S):
To run the Python «Hello World» program that we just created, select the test.py file in the VS Code Explorer window, then right-click the file to display a menu of options. Select Run Python File in Terminal. Alternatively, in your integrated WSL terminal window, enter: python test.py to run your «Hello World» program. The Python interpreter will print «Hello World» in your terminal window.
Congratulations. You’re all set up to create and run Python programs! Now let’s try creating a Hello World app with two of the most popular Python web frameworks: Flask and Django.
Hello World tutorial for Flask
Flask is a web application framework for Python. In this brief tutorial, you’ll create a small «Hello World» Flask app using VS Code and WSL.
Open Ubuntu 18.04 (your WSL command line) by going to your Start menu (lower left Windows icon) and typing: «Ubuntu 18.04».
Inside VS Code, open your integrated WSL terminal (aka Bash) by entering Ctrl+Shift+` (your HelloWorld-Flask project folder should already be selected). Close your Ubuntu command line as we will be working in the WSL terminal integrated with VS Code moving forward.
Create a new file for your Python code: touch app.py
In app.py, add code to import Flask and create an instance of the Flask object:
Also in app.py, add a function that returns content, in this case a simple string. Use Flask’s app.route decorator to map the URL route «/» to that function:
You can use multiple decorators on the same function, one per line, depending on how many different routes you want to map to the same function.
Save the app.py file (Ctrl+S).
In the terminal, run the app by entering the following command:
This runs the Flask development server. The development server looks for app.py by default. When you run Flask, you should see output similar to the following:
Open your default web browser to the rendered page, Ctrl+Click the http://127.0.0.1:5000/ URL in the terminal. You should see the following message in your browser:
Observe that when you visit a URL like «/», a message appears in the debug terminal showing the HTTP request:
Stop the app by using Ctrl+C in the terminal.
If you want to use a different filename than app.py, such as program.py, define an environment variable named FLASK_APP and set its value to your chosen file. Flask’s development server then uses the value of FLASK_APP instead of the default file app.py. For more information, see Flask’s Command Line Interface documentation.
Congratulations, you’ve created a Flask web application using Visual Studio Code and Windows Subsystem for Linux! For a more in-depth tutorial using VS Code and Flask, see Flask Tutorial in Visual Studio Code.
Hello World tutorial for Django
Django is a web application framework for Python. In this brief tutorial, you’ll create a small «Hello World» Django app using VS Code and WSL.
Open Ubuntu 18.04 (your WSL command line) by going to your Start menu (lower left Windows icon) and typing: «Ubuntu 18.04».
Inside VS Code, open your integrated WSL terminal (aka Bash) by entering Ctrl+Shift+` (your HelloWorld-Django project folder should already be selected). Close your Ubuntu command line as we will be working in the WSL terminal integrated with VS Code moving forward.
Next, run the following command to create the Django project:
Ctrl+click the http://127.0.0.1:8000/ URL in the terminal output window to open your default browser to that address. If Django is installed correctly and the project is valid, you’ll see a default page. The VS Code terminal output window also shows the server log.
When you’re done, close the browser window and stop the server in VS Code using Ctrl+C as indicated in the terminal output window.
Now, to create a Django app, run the administrative utility’s startapp command in your project folder (where manage.py resides):
The command creates a folder called hello that contains a number of code files and one subfolder. Of these, you frequently work with views.py (that contains the functions that define pages in your web app) and models.py (that contains classes defining your data objects). The migrations folder is used by Django’s administrative utility to manage database versions as discussed later in this tutorial. There are also the files apps.py (app configuration), admin.py (for creating an administrative interface), and tests.py (for tests), which are not covered here.
Modify hello/views.py to match the following code, which creates a single view for the app’s home page:
Save all modified files.
In the VS Code Terminal, run the development server with python3 manage.py runserver and open a browser to http://127.0.0.1:8000/ to see a page that renders «Hello, Django».
Congratulations, you’ve created a Django web application using VS Code and Windows Subsystem for Linux! For a more in-depth tutorial using VS Code and Django, see Django Tutorial in Visual Studio Code.
Источники информации:
- http://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/?highlight=scripts
- http://stackoverflow.com/questions/5226311/installing-specific-package-version-with-pip
- http://github.com/MichaelKim0407/tutorial-pip-package
- http://docs.microsoft.com/en-us/windows/python/web-frameworks