Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing openpyxl
#1
I have imported openpyxl. However, if I use the following line

import openpyxl
I get the

ModuleNotFoundError: No module named 'openpyxl'?
I was in C:\Users\Aziz\AppData\Local\Programs\Python\Python310\Scripts\ when I pressed cmd in the bar at the top of Windows and entered pip install openpyxl.

I am using PyCharm.
Reply
#2
Why did you pip from a cmd window instead of PyCharm? If you install inside PyCharm you are guaranteed that the module is installed in the Python being used by PyCharm (usually a virtual environment). If you pip in a cmd window it is going to install the package in whatever Python is active for the cmd window. Might be a virtual environment, but it might be "system" Python. Could be a different virtual environment. Do you know?
Reply
#3
You can do it from PyCharm as deanhystad mention,
but you should make sure that python and pip work from command line(from any folder).
Here a basic run from cmd,also look at cmder for a better shell.
# Versions
C:\>python -V
Python 3.10.5

C:\>where python
C:\python310\python.exe

C:\>pip -V
pip 22.2.2 from C:\python310\lib\site-packages\pip (python 3.10)

# Install
C:\>pip install openpyxl --upgrade
Requirement already satisfied: openpyxl in c:\python310\lib\site-packages (3.0.9
)
Collecting openpyxl
  Downloading openpyxl-3.0.10-py2.py3-none-any.whl (242 kB)
     -------------------------------------- 242.1/242.1 kB 1.9 MB/s eta 0:00:00
Requirement already satisfied: et-xmlfile in c:\python310\lib\site-packages (fro
m openpyxl) (1.1.0)
Installing collected packages: openpyxl
  Attempting uninstall: openpyxl
    Found existing installation: openpyxl 3.0.9
    Uninstalling openpyxl-3.0.9:
      Successfully uninstalled openpyxl-3.0.9
Successfully installed openpyxl-3.0.10

# Test that work
C:\>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import openpyxl
>>> openpyxl.__version__
'3.0.10'
>>> exit()
Now can look at Configure a Python interpreter,then can choose same Path to interpreter as command line,then it will work.
Then can look at late eg how virtual environment works.
Reply
#4
Thanks, got it to work in the virtual environment as far as the line

import openpyxl as xl
is concerned. However, when I add

wb = xl.load_workbook('transanctions.xlsx')
I get the error

"D:\Power BI & Python\venv\Scripts\python.exe" "D:/Power BI & Python/Test.py" 
Traceback (most recent call last):
  File "D:\Power BI & Python\Test.py", line 3, in <module>
    wb = openpyxl.load_workbook('transanctions.xlsx')
  File "D:\Power BI & Python\venv\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "D:\Power BI & Python\venv\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "D:\Power BI & Python\venv\lib\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "C:\Users\Aziz\AppData\Local\Programs\Python\Python310\lib\zipfile.py", line 1249, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'transanctions.xlsx'

Process finished with exit code 1
If I use
wb = xl.load_workbook('D:\Power BI & Python\transanctions.xlsx')
I get the error

"D:\Power BI & Python\venv\Scripts\python.exe" "D:/Power BI & Python/Test.py" 
Traceback (most recent call last):
  File "D:\Power BI & Python\Test.py", line 3, in <module>
    wb = xl.load_workbook('D:\Power BI & Python\transanctions.xlsx')
  File "D:\Power BI & Python\venv\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "D:\Power BI & Python\venv\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "D:\Power BI & Python\venv\lib\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "C:\Users\Aziz\AppData\Local\Programs\Python\Python310\lib\zipfile.py", line 1249, in __init__
    self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: 'D:\\Power BI & Python\transanctions.xlsx'

Process finished with exit code 1
Reply
#5
You most do commands from command line(cmd),and not interactive interpreter whic has >>>.
Reply
#6
Are you trying to type pip install in the Python console, in a PyCharm console?

For new versions of PyCharm (2021 and newer) you should use the Python Packages tool window. Before that you used the interpreter settings window. Both are discussed here:

https://www.jetbrains.com/help/pycharm/i...ool-window

If you want to use pip install in PyCharm, you can use the Terminal. Terminal may not be available in free versions of PyCharm. I am not a PyCharm user. Tried it. Hated it. Moved on to VSCode.

You can use pip install in a cmd shell (or powershell) if you set the python interpreter. If you are using "system" Python as your PyCharm Python interpreter, you shouldn't have to do anything. If you are using a virtual environment in PyCharm you'll need to activate the environment in the shell before using pip.
Reply
#7
Using PyCharm. Installed openpyxl successfully.

Tried the same code in VSC and getting the same errors.
Reply
#8
(Sep-26-2022, 07:30 PM)azizrasul Wrote: Using PyCharm. Installed openpyxl successfully.

Tried the same code in VSC and getting the same errors.
Reply
#9
Seems to be OK now! May have misspelled the file name.

Many thanks all. I'm sure will be back with another issue as I am new to Python but enjoying it.
Reply
#10
VSCode will not fix the problem of pip installing to the wrong Python. If using a virtual environment in VSCode you need to activate the environment before calling pip install. VSCode will automatically do this for terminals it opens if the python interpreter is a virtual environment. I have noticed that this does not happen for the first terminal that VSCode opens. My guess is the first terminal is opened prior to setting the Python environment.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020