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

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:
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

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

Related

PYSimpleGUI module not found

I made a program in python which I now wanted to turn into an .exe so that other people at my office, who do not have python or any python skills can use it too. This is not the first time I did this, and I am still working on the same machine, however this time I run into the "module not found" error when trying to execute the exe.
Basically I created a GUI with PySimpleGUI and then followed my own guide from last time, where I created a spec file with pyi-makespec, specifying the paths in which the packages are located. These are two locations: in C:// where python is installed, and in the "venv" folder of my Pycharm project. PYSimpleGUI is located in "venv" but not in "C".
After creating the spec file I create the exe with pyinstaller. It was suggested to use --hidden-import=PySimpleGUI as additional flag, which I did do, but I still got the same error. I also made sure python is added to my PATH, but maybe I did something wrong there? Tha path I used is the one where python is installed: C:\Users\Username\AppData\Local\Programs\Python\Python39\Lib\site-packages is that right?
I am completely stuck and can't find any helpful information. How do I solve this issue? Also important: I do not have admin rights on my computer, so installing stuff is always linked to me having to call the support desk...
If more information about this project is needed, let me know.
Usually, modules not found tend to be the issues with virtual environment. Python uses those venvs as some sort of folders where it stores the Python interpreter and packages installed with pip. If you have an error saying some module is not found, you should either install it on your current environment or switch to the one where ot is already installed. For more details, I recommend you to take a look at this tutorial how to use and setup them.
Additionally, you do not need to compile Python code if you can afford relying on the interpreter installed. If you know your target audience has the interpreter installed (an interpreter is usually shipped with various Linux distributives), then you can just give them your source code or even pycache (.pyc) files that it can interpret as well.
I tried three different things at the same time, one of them seemed to have solved the problem:
1.) update pip with the following commands in the PyCharm Terminal
pip install --upgrade PySimpleGUI
2.) update PySimpleGUI with the following commands in the PyCharm Terminal
C:\Users\Username\AppData\Local\Programs\Python\Python39\python.exe -m pip install --upgrade pip
3.) add python39 to PATH
I checked again and I did have \Python39\Scripts in my PATH, but not \Python39 itself, so I added it. The way to do it on windows is to search for "environment variables" (Umgegungsvariablen in german), edit the "Path" Variable (double click or click and press edit, which opens a new window), then add new Path (mine is C:\Users\Username\AppData\Local\Programs\Python\Python39)
I sort them with Python39 being above Python39\Scripts.
This solved my problem with PySimpleGUI, my program started as expected, but then ran into another missing module error (xlsxwriter not found), which - fair enough - was not imported. (interestingly when running the code in pycharm I didn't need xlsxwriter). I imported it, added it to venv, and when starting the program again, the error showed up even before showing the GUI. I then upgraded xlsxwriter with
pip install --upgrade xlsxwriter
I again created the .spec file and ran pyinstaller to create the exe. This time it worked.
Now I only have issues left which are not connected to missing modules (just variable referenced before assigned).

openai command not found (mac)

I'm trying to follow the fine tuning guide for Openai here.
I ran:
pip install --upgrade openai
Which install without any errors.
But even after restarting my terminal, i still get
zsh: command not found: openai
Here is the output of echo $PATH:
/bin:/usr/bin:/usr/local/bin:/Users/nickrose/Downloads/google-cloud-sdk/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Here is the output of which python:
/usr/bin/python
Any tips for how to fix this? I'm on MacOS Big Sur 11.6.
Basically pip installs the packages under its related python directory, in a directory called site-packages (most likely, I'm not a python expert tbh). This is not included in the path you provided. First, ask pip to show the location to the package:
pip show openai
The output would be something like this:
Name: openai
Version: 0.22.0
Summary: Python client library for the OpenAI API
Home-page: https://github.com/openai/openai-python
Author: OpenAI
Author-email: support#openai.com
License:
Location: /Users/<USER>/DIR/TO/SOME/PYTHON/site-packages
Requires: numpy, openpyxl, pandas, pandas-stubs, requests, tqdm
Required-by:
So your package will be available in
/Users/<USER>/DIR/TO/SOME/PYTHON/site-packages/openai
Either add /Users/<USER>/DIR/TO/SOME/PYTHON/site-packages/ to your path, or use the complete address to your package, or try to access it using your python:
python -m openai # -m stands for module
To get more information about the -m flag, run python --help.
Update
So as you mentioned in the comments, you get permission denied after you add the directory to your package. This actually means that the package exists, but it's not permitted by your OS to execute. This is the thing you have to do, locate your package, and then:
sudo chmod +x /PATH/TO/script
And the reason you're getting command not found after you use sudo directly with the package, is that you update your path variable in zsh, but when you use sudo, superuser uses sh instead of zsh.
This doesn't answer the question directly but specifies an alternative if you only want to prepare the data set and create the new model for finetunning. It doesn't matter which system you have.
After a lot of struggle I decided it was not worth the hassel to run the cli on my specific machine because of so many different configurations and the mess. My end goal was just to create a model and upload it to OpenAI.
So if someone else stumbles on this post, just use Google Colab. I have also shared one of mine with steps to follow in here.
In case the links don't work in the future I'll list the steps here below as well:
(Step 1)
Set your API key (The already added api key is fake so please replace it with your own):
%env OPENAI_API_KEY=sk-Kz8Weh1234ddgYBmsdfinsdf7ndsfg55532432
(Step 2)
Install the openai package with pip like the following:
!pip install -Uq openai
(Step 3)
Import the openai package like the following:
import openai
(Step 4)
Make sure to upload the promptdata.csv file in the Google Colab folders.
The way to do it is:
On the right side you'll see a Hamburger Menu icon click on it.
You'll see the "Table of Contents"
Click on the last folder icon on the top. If you hover on the icon it says "Files".
Now you'll see a folder called "sample_data".
Click on the three dots menu for "sample_data" and then select "upload".
You should be able to upload your csv file
It is not mandatory to upload a csv file. You can also upload any type of TSV, XLSX, JSON or JSONL file as listed by the OpenAI documentation here. But it will always be converted to JSONL file after runnning the below command.
Once you're done uploading the file you can run the below command to prepare your data set which will return you a new JSONL file at the same location where the original file was with all the corrections the tool provides.
!openai tools fine_tunes.prepare_data -f "/content/sample_data/promptdata.csv"
(Step 5)
Run the below command once again after the corrections and it will most likely say "No remediations found".
!openai tools fine_tunes.prepare_data -f "/content/sample_data/promptdata_prepared.jsonl"
(Step 6)
Finally run the below command using the file promptdata_prepared.jsonl and create a model.
!openai api fine_tunes.create -t "/content/sample_data/promptdata_prepared.jsonl"
(Step 7)
Once the model is created note the name of the "Uploaded model"
So what happens is that after installing the package there are no actual executables available. That's why you get the error message when you try to execute for example:
openai --help
What i managed to find is that the actual parsing of the commands is done in
/Users/<USER>/DIR_TO_PYTHON/site-packages/openai/_openai_scripts.py
That's just a python script which by default is not executable, so you have to make a workaround of which I find the easiest is creating an executable which basically calls it with the given arguments. Below are the steps which I've done to make it work on "macOS Monterey 12.0.1"
Locate the "openai" package which should be in
/Users/<USER>/DIR_TO_PYTHON/site-packages/
Make sure you are in the "openai" package folder and run
sudo vim /bin/openai
That should create a new file, put in the following command and make sure the path to the file is correct
python3 /Users/<USER>/DIR_TO_PYTHON/site-packages/openai/_openai_scripts.py $#
$# is for the params that you pass when you call the executable
After saving the file the next step is making it executable which is done with
chmod +x /bin/openai
Last step is adding it to the PATH which is done by adding the file path in /etc/paths and after restarting the terminal, you should have fully working openai command globally
I was facing similar issue. It might due to global python in your machine is not maching with the pip installation path and it might be installing in some other python folder like in 3.9 and you have 3.10 python version globally set in your Mac.
First install fresh python using homebrew
brew install python
It will install the latest python into your machine. Then try to install openai again using
pip3 install openai
OR using pip (you can try installing using both and see which works as per your system config)
pip install openai
Now
ENJOY a cup of coffee ;)

Can't run any Python files on Ubuntu(WSL)

I have just started working on my new pc and just to get a feel for it I wanted first to start working on python files, so I started first by just wanting to run WSL on windows and it installed correctly but when I want to run any python using the run python file on the top right on VS code, this is what gets executed $ C:/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe f:/Projects/hello.py
And this is the error: -bash: C:/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe: No such file or directory
I have no idea what's causing it but when I run the file using 'Shift + Enter' which is: Python: Run Selection/Line in Python Terminal it seems to run the single line correctly but it gives me this error instead:
print("Hello, world")
-bash: syntax error near unexpected token `"Hello, world"'
but when I run it using python3 hello.py, it works perfectly fine?! I'm so lost as to why this is happening and how could I fix it.
Might be relevant: I'm using windows 10, installed python 3.10.2 from windows store, all of that is in VS code and the python code is one line: print("Hello, world") and I changed the permissions of Local/Microsoft/WindowsApps so it's now accessible by all users to view/read/edit/run, made sure that python3.10.exe exists(on the WindowsApps and it works perfectly) and reinstalled it many times, tired python3.9, and tried to install python from the website instead of the windows store and still the same, manually added python to PATH and tried .venv and didn't work. when I launch python3.10.exe outside vs code it seems to run perfectly, I have worked with python before and it used to work fine now I don't know what's wrong.
I have seen other questions of the same problem I'm having here but none of them solve the problem.
No such file or directory C:/Users/...
For wsl, the Windows filesystem is accessible, but it has a different path. It is mounted under the /mnt folder. So you would find your python .exe under /mnt/c/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe. This said, the executable file is meant to work on Windows, and it doesn't really makes sense to use it on Linux when you could run python within your wsl distro.
python3 works perfectly fine
This is because most Linux distributions come with python3 pre-installed, so you can use it already. To see where it is located, you can run the command which python3, or python3 --version to check its version.
If you want to change version, you may consider download it from you package manager, apt.
I also suggest to install python3-pip if you don't have it already to get the pip package manager for python.
In my case when I ran into this.. I discovered pyenv. This allows you to download more than one version of python. You can then go into a specific directory, such as your python project and issue a python local 3.10.0 (for example). Here's a link on how to install it as well as poetry which is a virtual environment manager that is become very popular. You can also create an alias for python that works off of this. I add this command to my alias file and source it from my .bashrc. alias python='pyenv exec python3'

Module Not Found in python script

I'm a python beginner, and I'm following this tutorial for a webscraper https://hackernoon.com/building-a-web-scraper-from-start-to-finish-bb6b95388184
I'm on Windows 10, have setup a venv, activated and installed 2 modules using pip, and moved my script into the Scripts folder (from my understanding, this is the equivalent of the /bin/ folder on linux installations). The modules are bs4 and requests. I see both of these in the /Lib/ folder of my venv. I am using the Atom editor from atom.io, and the Scripts Package to run my script.
My script errors with a "module not found" error. Relevant snippet below:
scraper.py
from bs4 import BeautifulSoup
import requests
I get the error on both imports, indicating I've setup my project/imports incorrectly. I have no shebang line in my script, and suspect this is the problem.
My project structure looks like:
\ScraperProject
|-\ScrEnv
|-\Include
|-\Lib
|-\site-packages
|-\bs4
|-\requests
|-\Scripts
|-scraper.py
|-pyvenv.cfg
What is the proper way for me to import these modules into my script in a Windows environment?
with what python version are you working in the venv? Maybe try to uninstall those update pip and reinstall those again, making sure that you install them with pyhton3
My issue is related to running it from the Atom editor, something I hadn't considered before. It's calling the python executable from my PATH variable, and not the one in my venv. When running the script from the cmd window, and calling it with the python from my project folder/venv, it runs as expected. On to figuring out how to configure Atom to use the venv executables. Thank you very much for the interest and help!

pip in venv on Windows 10: 'Fatal error in launcher: Unable to create process using '"c:\users\..." ' encountered during flask tutorial

I'm going through the Flask tutorial, and I encounter a problem where I can't use pip (and thus anything else really) a venv in Powershell. The same flask app runs correctly in an Ubuntu terminal with Python 3.6.6 in WSL.
The problem seems to depend on the directory, which makes me think it's somehow related to file path length; I enabled long file paths in the windows Group Editor but this hasn't fixed the problem. In the below steps my venv directory is c:\users\rwgpu\google-drive\code\flask-tutorial\winEnvflaskr\ and I experience the error, but everything works correctly in the directory C:\python\test\testVenv. (in each case I'm running the commands in the directory one up from the listed above, flask-tutorial and test respectively)
Minimal steps to reproduce are:
Completely fresh Python 3.7 installation.
Change directory to app folder.
py -m venv winEnvFlaskr
./winEnvFlaskr/Scripts/activate
pip list
The ultimate goal is to then
pip install FLask
and run the Flask tutorial app. Again, all steps work correctly in Bash on Ubuntu in WSL (running its own Python) and in a different windows directory with the same Python 3.7.
After pip list I get the error:
Fatal error in launcher: Unable to create process using '"c:\users\rwgpu\google-drive\code\flask-tutorial\winenvflaskr\scripts\python.exe" "C:\Users\rwgpu\Google-Drive\Code\flask-tutorial\winEnvFlaskr\Scripts\pip.exe" list'
and if I try
python -m pip list
I get nothing; the terminal hangs for a second and returns with no output. If I run
py -m pip install -U pip
in the bugged venv it will try to install and report success. It will do this again, and will never report "requirement already satisfied" which would be correct.
I had the same problem on Windows with running flask in command line from venv(for example, "(venv)...\flask run"). I resolved the problem with changing path in flask.exe code(open "your_venv\Scripts\flask.exe" with notepad or etc.): in my way I correct the 436-th line at the end; you need to put there "your_absolute_path_to_venv\Scripts\python.exe" instead of the path indicated there.
Good luck!
Well, I still don't know why this was happening -- it persisted through deleting and recreating the venv (obviously), but when I deleted the entire folder, that somehow cleared it up. I just copied the code into a new directory in the same parent and everything seems to be working ¯\_(ツ)_/¯
If anyone knows what would cause this in my case I still welcome input.

Categories

Resources