I'm trying to run a selenium testcase in python. I have testcases that I can run directly from the command line no problem with
python seleniumtest.py
However when I try to run it from within python it is failing.
__import__('seleniumtest')
seleniumtest.py ends with a command
unittest.main()
this command seems to fail when it gets run using the __import__ method. Does know why running this through the import is not working? For my purposes I cannot simply use popen.
You need to have something like this at the end of your selenium program:
if __name__=="__main__"
Related
I have a test script written using pytest and it works fine when I execute it as standalone.
User:~my_workspace/python_project$ pytest path_to_script/my_script.py
Now I have to run this script with different parameters and hence I have created a python file (script_to_invoke_pytest.py) so that I can run the pytest script from this python file.
I tried below approaches within my script_to_invoke_pytest.py
#approach 1
import subprocess
subprocess.Popen(['pytest', r'path_to_script/my_script.py'], shell=True)
#approach 2
import pytest
pytest.main([r'path_to_script/my_script.py'])
Both the approaches didn't work and I get errors related no modules named xyz.
Im using pycharm ide and before running pytest, I execute the docker.
Please let me know how can I invoke my pytest from my python script script_to_invoke_pytest.py
In your second approach, you're not telling python where to find the tests. Try:
import pytest
pytest.main([r'path_to_script/my_script.py'])
I am trying to run Test.py using os.system(path) where I specify the path of the file but I am getting an error. Basically, I want to run Test.py from here and display the output.
import os
os.system(rf"C:\\Users\\USER\\OneDrive-Technion\\Research_Technion\\Python_PNM\\Sept15_2022\\220\\1\\Test.py")
The error is
The system cannot find the path specified.
you are passing a python file, not an executable. You can pass python yourfile.py.
By the way, I would reconsider what you are doing, executing a python script from another python script is quite strange.
i am not getting any output whenever i try to execute any basic command , in python using VS code.
Your Visual Studio Terminal has Python already opened, so when you try to execute the:
& "C:/python 39/python.exe" "C:/route_to_your_file.py"
Is trying to execute a line of code with this, something that has a bad python syntax.
You have two options:
Close python writing exit() and then run your python script with "C:/python 39/python.exe" main2.py or "C:/python 39/python.exe" main3.py
Work directly with the application of python. In that case you can write your code directly on the terminal
print("hello world")
you can (but it is not recommended) import your files as packages, with:
import main2
import main3
Since you are new on python, I recommend you the first option.
You seem to be trying to run python while already in python. Try opening a bash or cmd terminal and typing in python main3.py
Run the code directly:
rather than get into python then run the code:
I have previously only written python code using IDLE but since I am starting to do some more "heavier" programming I figured I should start using Visual Studio Code. I am however having issues doing things that I would like to do while coding to check that my functions are working as intended. The major thing I want to be able to do is if I have saved
def summa(x, y)
return x+y
in a file sum.py, then I would want to test run summa(3, 4) in the terminal.
In IDLE I am used to just running the file containing the function and then use it but I cannot figure out how to do that in Visual Studio Code. However, I realize that it is possible to import the file into a REPL terminal but I would hope that there is some easier way of doing it.
See the answers to this question :
execute python script with function from command line, Linux
You could also use the main function :
if __name__ == '__main__':
summa(sys.argv)
So you just launch the script and it run the function
You can start Python interactively and import your file as a module. Then your function will be available, so you can call it the way you want:
What I did was:
Open new terminal
Type: $ python to start an interactive session
Import module import test or the function from the module: import summa from test
Now I can call summa() and play with it in this manual manner
When I import the wx module in a python interpreter it works as expect. However, when I run a script (ie. test.py) with wx in the imports list, I need to write "python test.py" in order to run the script. If I try to execute "test.py" I get an import error saying there is no module named "wx". Why do I need to include the word python in my command?
PS the most helpful answer I found was "The Python used for the REPL is not the same as the Python the script is being run in. Print sys.executable to verify." but I don't understand what that means.
Write a two line script (named showexe.py for example):
import sys
print sys.executable
Run it both ways as showexe.py and python showexe.py. It will tell you if you're using the same executable in both cases. If not, then it'll depend on your operating system what you have to do to make the two run the same thing.
If you start your script with something like #!/usr/local/bin/python (but using the path to your python interpreter) you can run it without including python in your command, like a bash script.