Our Python tutorial code now runs online without installation. However, if you want to clone the runtime environment locally, refer to the instructions in this article.

confusion

These days, IN my spare time, I am busy transferring my knowledge planet sharing articles to The Language Finch platform, so that subscribers can read and get instant push. At present, the sharing space has begun to take shape.

Looking back, the column and wechat public account background, accumulated a lot of users’ questions.

For example, this user asks:

Why is my new file with Binder lost when I open it again?

The question comes from my how to Run Python Code on an iPad? The article.

Mybinder is used to provide readers with a consistent code environment.

You can run the sample code without installing it. You can modify the code to run it again, and you can even upload data files to do your own analysis.

I need to add an important caveat — MyBinder provides Python runtime resources that are shared, not permanently exclusive.

Each student opened the same link, MyBinder opened an independent environment, no interference.

However, the Python environment needs resources in the background to run.

Every time you open a link with MyBinder, the background provides you with the corresponding CPU, memory, hard disk and other resources.

If these resources are occupied by a large number of users for a long time, the platform cannot afford them. New users will no longer be able to join.

Mybinder’s convention with users is that if you are “inactivity” for more than 10 minutes, the system shuts down your session in order to reclaim resources and serve more users in need.

When you close the browser after running it, use the link again for more than 10 minutes and the changes will be gone.

That’s why I gave you the Github repo address for the source code at the end of the tutorial.

You can choose to clone the tutorial Python runtime environment on your own machine.

In what cases would you want to clone the Python runtime environment locally?

If your data is large or requires high security, uploading it to the cloud is inconvenient.

If you are afraid of network problems in the middle of code running because of network stability, all your previous efforts will be wasted;

If you’re running a deep learning model that requires a GPU or a large amount of memory…

Encounter the above situation, it does not matter.

Let me show you how to use Pipenv to easily clone the Python runtime environment specified by the tutorial and run the Jupyter Notebook locally.

process

How to Collect and analyze Network Data using Python and APIS Take this article for example.

At the end of this article, I gave you the code address of the corresponding Github repo (t.cn/R3usEti).

You can also download a zip package containing the source code and runtime environment directly from this link (t.cn/R3usDi9).

When unzipped, you should see the following configuration files in the directory:

  • environment.yml
  • postBuild
  • Pipfile

Yml and postBuild are for MyBinder. You can ignore them while local clones run the environment.

Notice the Pipenv file in it.

Let’s open the Pipenv file and see what it says:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
ipykernel = "*"
plotnine = "*"
requests = "*"

[dev-packages]

[requires]
python_version = "3.6"

Copy the code

The requires area, which illustrates the environment used in this tutorial, is Python version 3.6.

The Packages section tells Pipenv what packages need to be prepared for us.

Let’s take a look at how to clone the runtime using Pipenv.

You need to check that Anaconda 3 is already installed.

If you haven’t already installed it, please refer to the tutorial “How to Install The Python runtime Environment Anaconda?” I made for you to install it, and learn how to access the decompressed package on your terminal.

I’ve recorded a video of how to do this in detail, and you can just click on this link (t.cn/R1cWIWr) and play the video.

If you have previous installation experience, you can also follow the instructions below.

After that, execute the following statement:

pip install pipenv
Copy the code

This will install the Pipenv tool for us to process the Pipfile.

Step 2: Execute:

pipenv install --skip-lock
Copy the code

This command will allow Python to automatically build the environment based on our current Pipenv configuration, and pull all of the required dependency packages off the web and install them.

Step 3: Execute:

pipenv run python -m ipykernel install --user --name=wangshuyi
Copy the code

This command will help you install a core block for the Jupyter Notebook, and put the package information you just installed into the block.

I named this block Wangshuyi to make it easier to reuse the code in this series of tutorials.

For normal tutorial source code, the above steps will do. But because of “How to Collect and analyze Network Data with Python and apis?” This article involves drawing, and it appears in Chinese.

We need to do some processing to make the program run smoothly and ensure that Chinese characters are displayed properly.

Do this by executing the following statement:

pipenv run python handle_matplotlib_chinese.py
Copy the code

Finally, open Jupyter Notebook:

jupyter notebook
Copy the code

At this point, you should see the familiar Jupyter Notebook interface.

Note that in this example code, you need to enter Your own AppCode, so you need to replace “Your AppCode here” with Your own AppCode value. Otherwise, the following run will report an error.

After the replacement, click “Cell” in the menu bar and select “Run All” to see if All the code can Run normally and the graph of analysis result is displayed.

If all is well, it means your Python runtime environment clone is complete.

Congratulations to you!

background

A reader left a comment asking why two different virtual package management tools were used.

Because, unfortunately, as of this writing, MyBinder does not support the Pipfile configuration file.

In fact, the Pipfile we use here is not the configuration file format for pipenv.

It is the official specified configuration file format for PIP, the future Python package management tool.

In the future, if you install software with PIP, you’ll have to deal with pipfiles.

However, many moves by the Python open source community have been slow.

Python 3, for example, has been around for so long that Python 2 hasn’t retired yet.

How can PIP, the low-level package management approach, change radically in a day or two?

Pipenv is here because people don’t want to wait.

Now, right now, we’re going to use the most humane Python package management tool.

There are many benefits to using Pen V.

To take just one example, with Pipenv, you can view the dependencies between the current project packages with a single command.

How do you feel?

If you’re interested in learning more about PipenV and its features and evolution, check out Kenneth Reitz’s presentation at PyCon 2018 (t.cn/R1cYQSU).

Who is Kenneth Reitz?

If you’ve read my how to Collect and Analyze Network Data with Python and apis? A cent? You’ve already used his work.

Yeah, he wrote requests, the HTTP tool for humans.

discuss

Would you prefer to run Python tutorial code online with MyBinder, or clone an entire runtime environment locally? Why is that? Welcome to leave a message, share your experience and thinking to everyone, we exchange and discuss together.

If you like, please give it a thumbs up. You can also follow and top my official account “Nkwangshuyi” on wechat.

If you’re interested in data science, check out my series of tutorial index posts entitled how to Get started in Data Science Effectively. There are more interesting problems and solutions.