Bex
Bex

Reputation: 27

Using PYTHONPATH in Spyder with no access to command line

I've just started using Python with Spyder at work, which means I'm far more restricted than normal as I have no access to the command line.

I'm trying to access the PyPDF2 library, which I have downloaded as a ZIP file, and then pointed to this file with the PYTHONPATH manager. I still can't access it:

from PyPDF2 import PdfFileMerger, PdfFileReader

gets: "ImportError: No module named 'PyPDF2'"

All the walk throughs I've seen of using PYTHONPATH involve using the command line. Can anyone help with how to do this without this access? Sorry am relatively new to this and really stuck!

Thanks

Upvotes: 1

Views: 4113

Answers (1)

Dan
Dan

Reputation: 102

I don't know anything about Spyder, but in Anaconda there is a way to install packages from the Anaconda Navigator. If Spyder doesn't have this feature, you can do the following:

  1. Create a folder somewhere called PyPDFPath
  2. Unzip PyPDF2 into this directory, making sure your directory structure looks like this, with all of the PyPDF2 code inside the PyPDF2 directory

PyPDF directory structure

  1. At the top of your script, before any other imports, add the following code, where PYPDFPATH is the location of the PyPDFPath folder

    import sys
    sys.path.append('PYPDFPATH')

  2. In your script, try importing PyPDF2 as you did in your question. If you've done everything right, you should have no problems.

The sys.path variable is a list that contains all of the folders that Python should look for modules. If you add a folder to this list with modules you'd like to import in it before you import them, Python will look for those modules in this folder in addition to the default folders it looks for modules in.

Note that if you downloaded the PyPDF2 zip from GitHub, your PyPDF2 directory needs to contain the PyPDF2 directory from inside the zip instead of the entire repository.

I hope this helps!

Upvotes: 2

Related Questions