Difficulty in installing PyPDF2 and PCMUPDF

Python programmers need someone to write user-friendly tools for installing useful libraries such as PyPDF2 and PcmuPDF. A modest fee would be appropriate for using such a tool. My Python program needs to access bookmarks and their page numbers in PDF files but I cannot get past the need to access the PyPDF2 and/or PcmuPDF libraries.

Both of those can be installed (at least for me, on my PC) using simple pip install pypdf2 and pip install pymupdf commands.

Youā€™ll need to explain what you tried, what went wrong, and what error message(s) you received in order for anyone to be able to help.

1 Like

Thank you so much for responding.

At the command prompt level

Pip install pypdf2

Response:

Requirement already satisfied: pypdf2in C:\users\ray\appdata]local\programs\python\python310\lib\site-packages (2.4.1)

(Similarly for pymupdf)

In Jupyter Notebook

Import PyPDF2

Run

Error message:

ModuleNotFoundError

1 import PyPDF2

ModuleNotFoundError: No module named ā€˜PyPDF2ā€™

Hi @BrizzoRay is Jupyter running from the same virtual environment where you are installing PyPDF2?

If the pip installation is not working, maybe you can have some luck installing directly from conda-forge.

1 Like

Itā€™s always a path problem.

The command-line pip is tied to one specific Python environment, usually the system one if thereā€™s only one Python on the system. If you have multiple Pythons, or if you have virtual environments (which you do with Conda, and maybe do with a more standalone Jupyter), thatā€™s a different set of paths, and your packages need to be installed for that environment. Jupyter adds some extra complexities (its own idea of kernels) - itā€™s beyond the scope here to go into it, itā€™s worth doing a search on Stack Overflow for some ideas. Do try from inside Jupyter so you have an idea of where you are:

!which python
!pip list
2 Likes

My problem could be that I have been installing pypdf2 from the Windows command level instead of from the Jupyter Notebook command level. But I cannot find out how to go to the command level within Jupyter Notebook. The following advice does not have any effect in the Jupyter Notepad home screen. " If you click on the prompt or output area of a cell you will enter command mode."

Unfortunately, in my experience, Jupyter has surprisingly limited convenience facilities for easily working in multiple environments compared to other scientific IDEs like Spyder. However, assuming this is just a matter of ensuring packages install in the same environment as the Jupyter kernel you are running, this comes down to one of two options:

  • Launch Jupyter Notebook in whatever Python environment you are installing packages into by typing jupyter notebook at the same command prompt. Of course, this assumes it is installed there (or you install it).
  • Figure out what Python environment Jupyter is using by checking the output of import sys; print(sys.executable) in your Jupyter Notebook. This will give either the path to the base Python install, or to the specific virtual/conda env being used. Then, activate that environment in your command prompt (will vary depending on what it is and how you installed Jupyter), and run your install commands again.

You could also running the install commands directly inside your Jupyter notebook by prefixing them with !, e.g. !pip install pypdf2, but there is no guarantee that actually works correctly, and could cause further problems.

If you still canā€™t figure it out, copy/paste the full, exact output of, at minimum, where python in the command prompt you are installing the packages, and import sys; print(sys.executable) inside your Jupyter notebook, inside a code block (use </> in the toolbar, or the following Markdown syntax:

```
YOUR FULL COMMAND OUTPUT HERE
```

This will allow us to determine where the Python environments youā€™re installing into and running your notebook in, respectively, are located so we can advise you how to use the latter with the former.

1 Like

Another responder named Wayne has solved my problem. His solution was:

You want to be able to type in a notebook cell to install packages from inside a Jupyter notebook.
To install your example package from within a Jupyter notebook, you want to type the following in a normal cell still in normal editing mode:

%pip install PyPDF2

That uses the pip magic command that insures the installation occurs in the environment that is backing the kernel underlying the notebook.

Thank you for your consideration of a novice in distress.

1 Like

Cool. I just found out about this as well - donā€™t use Jupyter much. ā€œpip magic commandā€ indeed :slight_smile:

Firstly I was expecting to see a black window for the command level from Jupyter Notebook just like the Windows command level window. As instructed I went to an empty cell in Jupyter Notebook and typed in the command %pip install pypdf2 and then used the RUN button.

As a retired programmer now aged almost 89 I need to learn Python and write a program because no such suitable program exists. (Actually one command level program does exist but it gives faulty results and does not group the output files.) My project involves the reading of about a thousand PDF files, splitting them by top-level bookmark and then grouping the thousands of output files by bookmark. It gives me something interesting and useful to do. At this point my program gets the user to select the input and output directories and loops through the input files gathering each fileā€™s bookmarks and page numbers. However the bookmark coding is ugly and verbose and I am looking for a better alternative. This is fun.

Mad respectā€”Iā€™d be content if I was even alive at the age of 89, much less able to learn a whole new programming languageā€¦