0

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

user2851376
  • 173
  • 3
  • 11
  • Well, is Pycharm setup to use a virtualenv? Is xlwings inside the virtualenv? Did you activate the virtualenv? – OneCricketeer Jul 24 '18 at 05:16
  • Does `pip list` include the `xlwings` package? Is it any different if you run the same command in a pycharm terminal? – ScottMcC Jul 24 '18 at 05:19
  • @cricket_007 did you mean my project interpreter? Yes, xlwings inside the virtualenv and it was activate in Pycharm – user2851376 Jul 24 '18 at 06:15
  • @ScottMcC In pip list terminal contains xlwings and I had run the code in terminal seems like error in `app.kill()` line although there is no issue since yesterday. Now having difficulties to fixing the error. :( – user2851376 Jul 24 '18 at 07:03
  • @ScottMcC already edit the code and had try running in PyCharm and PyCharm terminal both has no issue. But still facing the same issue when trying to run in cmd – user2851376 Jul 24 '18 at 07:37
  • I had found something strange. When I'm trying to `import sys; print(sys.executable)` in pycharm terminal it pointed to my virtualenv but in my cmd in point to default folder `python.exe` is there any way to change it? – user2851376 Jul 24 '18 at 07:45
  • You need to activate the virtualenv in the cmd – OneCricketeer Jul 24 '18 at 11:58
  • @cricket_007 Yup, already found the set to activate virtualenv in this [link](https://www.youtube.com/watch?v=ynDlb0n27cw&t=38s) and its works! The things is, how can I apply in my python script because I need to auto-run it? Do I need to make sure all package that I use in virtualenv also available in default Python? – user2851376 Jul 25 '18 at 01:05
  • I believe the default behavior of virtualenv is to pick up all libraries from the parent python, but it's always encouraged to install all dependencies into that venv. And you can simply modify your run scripts to use `/path/to/venv/python script.py` without activation – OneCricketeer Jul 25 '18 at 05:09
  • @cricket_007 I see...ok thanks about that. Now, taking next step to search how to auto install the list of package using python script :) – user2851376 Jul 25 '18 at 06:49
  • `pip install -r requirements.txt` is the canonical way – OneCricketeer Jul 25 '18 at 12:08
  • @cricket_007 woahhh...awesome! thanks for your help :) – user2851376 Jul 26 '18 at 03:33

1 Answers1

3

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245