user2851376
user2851376

Reputation: 175

How to Run Python script that contains xlwings package in Command Prompt

I'm trying to run python3 script in command prompt Windows(cmd) but facing some issue if python script contains xlwings package. I'm using xlwings package to read and write the information that I needed in excel file. I had go through some research but all of the solution is pointed to run python from excel (meaning call python script in vba) and I don't want to do that. Here is the sample of the python code for testing purpose.

import xlwings as xw

bookName= r'C:\Users\Desktop\Python_Projects\Test.xlsm'
wb= xw.Book(bookName)#specified full name of excel file
sht = wb.sheets('Sheet1')
app= xw.apps.active #need to kill excel apps

sht.api.Cells(1,1).Value="test"
sht.api.Cells(1,1).Font.Bold = True

wb.save(bookName)
wb.close()
app.kill() #or del app

I'm trying to run the script and hit this issue:

No module named xlwings

I'm running the code from my IDE Pycharm, no issue and able to run. Note: I don't have admin right permission in my Windows.

Addition: I had try to search and find the post similar with my environment(using Pycharm-not run via terminal, setup as a virtual environment) but different issue facing. I'm not very understand the answer in this post. Thus, not sure whether the answer is fixing my current issue or not. If the solution is same, hope that someone can describe it further details. Here is the link: Python script works in PyCharm but throws path errors in windows cmd

Upvotes: 0

Views: 863

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191864

Moving comments down to answer...

Sounds like PyCharm had been setup to use a virtualenv, so you found a link where you found that you need to activate the virtualenv

From there, that's where any PyCharm ran a pip install into, and from which you can run python and try to import any modules.

If you don't activate the environment, you would need to run your scripts by giving the full path to the Python executable - C:\path\to\venv\python script.py

To backup a list of installed modules from a virtualenv, you can do pip freeze > requirements.txt

Then pip install -r requirements.txt will restore those into any freshly created virtualenv

Upvotes: 3

Related Questions