py-pdf / fpdf2

Simple PDF generation for Python
https://py-pdf.github.io/fpdf2/
GNU Lesser General Public License v3.0
1.12k stars 254 forks source link

'from fpdf import FPDF' Throws an Error? #317

Closed VikR0001 closed 2 years ago

VikR0001 commented 2 years ago

FPDF has errors with unicode, so I uninstalled it, and installed FPDF2.

I see from the FPDF2 docs that the correct way to import it is:

  from fpdf import FPDF

However, my compiler (intelliJ) throws an error there:

ImportError: cannot import name 'FPDF' from 'fpdf' (unknown location)

How can I correct this?

Lucas-C commented 2 years ago

Hi @VikR0001!

Interesting, let's see if I can be of any help...

First of, JetBrains IntelliJ is a bit more than a compiler, it's an IDE.

Answering the following questions should help you solve this problem:

The error you see could be caused by many things... For example, you could have installed fpdf2 with a different Python version or pip command than the one used by IntelliJ, ending with two independant but coexisting "Python installations" on your computer. It could also be that you installed an old/broken version of fpdf2, or that the library installation simply did not work. Please answer the questions above so that I can further help you 😊

Lucas-C commented 2 years ago

Hi @VikR0001

Have you solved your issue?

Without any feedback from you, I'm going to close this issue soon.

VikR0001 commented 2 years ago

Thanks very much in advance! Here is the requested info:

Python 3.8.9

pip 21.3.1

arabic-reshaper==2.1.3 asgiref==3.4.1 async-generator==1.10 attrs==21.4.0 backports.zoneinfo==0.2.1 beautifulsoup4==4.10.0 Brotli==1.0.9 bs4==0.0.1 certifi==2021.10.8 cffi==1.15.0 charset-normalizer==2.0.10 chromedriver-py==97.0.4692.71 click==8.0.3 cryptography==36.0.1 cssselect==1.1.0 cssselect2==0.4.1 cssutils==2.3.0 Django==4.0.1 django-mysql==4.2.0 feedfinder2==0.0.4 feedparser==6.0.8 filelock==3.4.2 fonttools==4.28.5 fpdf2==2.4.6 future==0.18.2 h11==0.13.0 html5lib==1.1 idna==3.3 jieba3k==0.35.1 joblib==1.1.0 lxml==4.7.1 mysqlclient==2.1.0 newspaper3k==0.2.8 nltk==3.6.7 outcome==1.1.0 pathlib==1.0.1 pdfkit==1.0.0 Pillow==9.0.0 pycparser==2.21 pydyf==0.1.2 pyOpenSSL==21.0.0 PyPDF2==1.26.0 pyphen==0.12.0 python-bidi==0.4.2 python-dateutil==2.8.2 python-dotenv==0.19.2 python-http-client==3.3.5 PyYAML==6.0 regex==2021.11.10 reportlab==3.6.5 requests==2.27.1 requests-file==1.5.1 selenium==4.1.0 sendgrid==6.9.4 sgmllib3k==1.0.0 six==1.16.0 sniffio==1.2.0 sortedcontainers==2.4.0 soupsieve==2.3.1 sqlparse==0.4.2 starkbank-ecdsa==2.0.3 tinycss2==1.1.1 tinysegmenter==0.3 tldextract==3.1.2 tqdm==4.62.3 trio==0.19.0 trio-websocket==0.9.2 urllib3==1.26.8 weasyprint==54.0 webencodings==0.5.1 wsproto==1.0.0 zopfli==0.1.9

Lucas-C commented 2 years ago

Great, thanks!

Let's continue testing: could you try the following commands in the terminal please?

python -c "from fpdf import FPDF"

python -c "import fpdf; print(fpdf)"

pip install --upgrade fpdf2
VikR0001 commented 2 years ago
(venv) my_user_name my_App % python3 -c "from fpdf import FPDF"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'FPDF' from 'fpdf' (unknown location)
(venv) my_user_name my_App % python3 -c "import fpdf; print(fpdf)"
<module 'fpdf' (namespace)>
VikR0001 commented 2 years ago

running pip install updated from fpdf2-2.4.6 to fpdf2-2.5.0.

Checking anomaly...

VikR0001 commented 2 years ago

Hey that worked!

Thanks!

Lucas-C commented 2 years ago

Great! 😊

FYI the fact that the output of the 2nd command, <module 'fpdf' (namespace)>, did not include a path to a Python source file was strange. Maybe the issue here is that you had a file named fpdf.py in your current directory, which conflicted with the library import.