How to open (and keep open) a terminal window using Python to run Appium? - python

I am using python to write an automation script where I would like to start appium programmatically each time it is run. I am running my scripts using a MAC. I have tried many things to get appium to run and all I get are errors about file or directory not found and nothing happens. Here are some details:
Running 'which appium' using terminal gives me /usr/local/bin/appium
Looking at /usr/local/bin/appium shows it's an alias (not sure if that's helpful)
I have tried to run subprocess in various ways in my code with no luck.
I tried all the suggestions in Python subprocess.call a bash alias and nothing worked.
I tried using os.('path/to/appium') and no good.
I created a bash file starting appium located on my desktop which runs great when I manually run it, but comes back with 'line 1: appium: command not found' when I run it.
Apparently when I run python, it can't find appium, but I'm stumped at this point. I have /usr/local/bin in my .bash_profile in PATH. Is this not enough?
or should I be trying to start appium a different way?
Thanks for any help!
Error messages I'm getting are:
FileNotFoundError: [Errno 2] No such file or directory: 'appium --port 4725'

Related

VS Code had a problem in running the code

I tried to run a python program in VS Code. But my program didn't run. The terminal opened and a weird arrow was there in the terminal. This is the screenshot of that.
This is the weird arrow and the program is not running. Any ideas why this is happening and how to fix it?
Thanks in advance.
Firstly, the arrows are included in the default python IDE means that VScode ran the command to execute your code. Give your pc a restart. Now, let us check if python is working or not or VS code is having some trouble. Type the following command in cmd to execute the code-
python "$PATH"
Rember to replace $PATH to the path of the file i.e where your file is stored. For eg. I've my python files stored in D drive in a python folder, so I'll use-
python "D:\Python\Hello.py"
If this works, python is working fine and if not, try reinstalling python and check the box which says Add python to Path or Environment variables. Then open VS code try to run the program again. But click the button only once and be patient because clicking it multiple times causes execute the same command again and cause a problem. It's my personal experience. Wait 5 minutes. Not works. Don't worry, there's a problem with the run extension you are using. I'll recommend the Code runner by Jun han. I personally use it. Type this in the extension search box-
formulahendry.code-runner
Install it and then try again.
Kill the terminal, and retry. If not work, restart the VSCode.

Trying to run Python program from command prompt

Path must be messed up and I can't fix it.
In the command prompt I am trying to open and run a python program that I made in IDLE. I am running Python 3.8.5. According to Automate the Boring Stuff, I should just be able to do:
py birthdays.py
But when I do that I get:
C:\Users\name\AppData\Local\Programs\Python\Python38-32\python.exe: can't open file 'birtdays.py': [Errno 2] No such file or directory
All of my scripts are located in: C:\Users\henri\AppData\Local\Programs\Python\Python38-32.
The only thing that I could think of is that it is going one step too far and searching in python.exe instead of just Python38-32. But Ii'm not even sure it works like that.
I tried to add the exact path using:
set PATH=%PATH%;C:\Users\name\AppData\Local\Programs\Python\Python38-32
But when I do this and press enter nothing happens.
when I want to use CMD to run python scripts, I just use
cd\
to back the main root of drive C. then use
cd folderAddress
for several time until to reach the exact scrip containing folder. Finally, I type
python scriptName.py
In your command prompt type python.The output should be python 3.** (depending on your python version).
If you see the python version it's working fine. In command prompt navigate to the folder that you have your python file using cmd command. then type
python birtdays.py
Don't forget the space after python.

Pyinstaller "Failed to execute script pyiboot01_bootstrap"

I'm using pyinstaller to produce an exectuable for a python program. But, whenever I try to use a generated executable, I get a message window that reads "Failed to execute script ...".
The command I'm running is pyinstaller -w run.py. But, when run with the --debug option a different error is made apparent. "Failed to execute script pyiboot-1_bootstrap". After this error it kills the program.
I tried with the -F option and that did not change the outcome. All the files I created for the project are in the same directory. I did get warnings that dll's weren't being included but I resolved that.
Pyinstaller doesn't like the built in quit() function I was using.
Instead use sys.exit() to close the program.
See this question to see why sys.exit() is better practice for production code.
What I found useful was to make an exe of my .py script in console mode and then run it. For a very short period of time, I saw an error message in the console window. I had to screenshot it to read completely. So one of the imports was missing from the folder in which I had my script. I opened cmd from the location bar of that folder (by typing cmd there) and imported modules using pip install <module_name>.
Sorry for the long answer, I am a newbie.

Ubuntu run python script on system startup that is using Firefox

I wrote python script that uses subprocess.pOpen() module to run and manipulate with 2 GUI programs: Firefox and VLC player. I am using Ubuntu 14.04 LTS operating system in Desktop mode.
My problem is when I try to run that python script when system starts, script is running but Firefox or VLC don't start.
So far, I tried to make shell script to run my python script and then with crontab with #reboot /home/user/startup.sh to execute my python script. I set all permissions for every script that is using. I gave my user root permisions so everything is OK with that.
I also tried to run my script putting command "sudo python /path/to/my/script.py" in /etc/rc.local file but that also is not helping.
I googled and found out people using .desktop files that they put in ~/.config/autostart/ directory but that also failed. Example of what I wrote:
[Desktop Entry]
Type=Application
Exec="sudo python /home/user/path_to_my_script/my_script.py"
X-GNOME-Autostart-enabled=true
Name=screensplayer
Comment=screensplayer
And I saved this as program.desktop in ~/.config/autostart/ directory but it does not work. I am sure there is a way to fix this but don't know how. Any help will be appreciated!
Found solution to my problem. When you are running commands with pOpen in python like this:
FNULL = open(os.devnull, 'w')
_FIREFOX_START = ["sudo", "firefox", "test.com/index.html"]
subprocess.Popen(self._FIREFOX_START, stdout=self.FNULL, stderr=subprocess.STDOUT)
it won't run apps because of "sudo" word, when I removed it, it worked.
Also run gnome-session-properties in terminal and add new startup application, be aware that you have to execute python script without sudo, like this:
python /home/user/path_to_script/script.py
Also, I granted my user root privileges so kepp that in mind.

Permission Denied when executing python file in linux

I am working with my Raspberry Pi 2 B+ and I am using Raspbian. I have a python script located at /home/pi/Desktop/control/gpio.py
When I type /home/pi/Desktop/control/gpio.py into the command line, I get the message
bash: /home/pi/Desktop/control/gpio.py Permission denied
I have tried running sudo -s before running that command also but that doesnt work. My python script is using the Rpi.GPIO library.
If someone could please explain why I am getting this error it would be appreciated!
You will get this error because you do not have the execute permission on your file. There are two ways to solve it:
Not executing the file in the first place. By running python gpio.py python will load the file by reading it, so you don't need to have execute permission.
Granting yourself execute permission. You do this by running chmod u+x yourfile.py.
However, doing so will not work unless you add a shebang at the top of your python program. It will let your linux know which interpreter it should start. For instance:
#!/usr/bin/env python
This would try to run python using your current $PATH settings. If you know which python you want, put it here instead.
#!/usr/bin/python3
Remember the shebang must be the very first line of your program.
do like this maybe work:
cd /home/pi/Desktop/control/
python gpio.py
Because gpio.py is not a executable file, you should run it by python instead

Categories