How to run ABAQUS python script from Visual Studio Code? - python

I am trying to use Visual Code Studio for editing and running Python scripts for ABAQUS (finite element software using internal Python 2.7 interpreter with a lot of libraries).
The standard situation is that I run this command from Windows cmd prompt
abaqus cae noGUI="myscript.py"
==========================================================
EXPLANATION:
The command "abaqus" is a reference to this batch file
C:\SIMULIA\Commands\abaqus.bat
containing
#echo off
"C:\SIMULIA\Commands\abq2018.bat" %*
and the latter batch file abq2018.bat contains this
#echo off
setlocal
set ABA_COMMAND=%~nx0
set ABA_COMMAND_FULL=%~f0
"C:\SIMULIA\CAE\2018\win_b64\code\bin\ABQLauncher.exe" %*
endlocal
This is a standard configuration used for ABAQUS software.
==========================================================
Normally, I edit the Python scripts in PSPad and hit F9 which runs the edited script using the macro (therein called Highlighter)
"c:\SIMULIA\Commands\abaqus.bat" cae nogui="%File%"
and everything is perfect.
I tried to set up a similar "one click to run" scenario in Visual Studio Code by playing with "Workspace", "Interpreter", "launch.json" and "python.pythonPath" settings but without any success. The default Python interpreter 3.7 works normally for standard Hello-world etc. scripts.
My goal is to use the debugging features in Visual Studio Code...
Thanks for any help
Robert

Although the question is asked almost two years ago, if somebody is looking to use Visual Code Studio for editing and running Python scripts for ABAQUS, see pyabaqus.
Documentation:
https://pyabaqus.haiiliin.com/

Yes, I use it with VS Code and I am able to run it direcly from VSC. I have python 3.11 and installed pyabaqus package. You don’t tell VSC the location of abaqus instead you add the .bat file to path. The Pyabaqus finds the location of Abaqus. This package is not specific to VSC and python interpreter should work. I didn’t see any type hints tough.
From pyabaqus doc:
“
Setup your Abaqus Environment
In order to use Abaqus command to execute the Python script and submit the job, you need to tell pyabaqus where the Abaqus command is located. Usually, Abaqus command locates in a directory like this:
C:/SIMULIA/Commands/abaqus.bat
You can add the directory C:/SIMULIA/Commands to the system environment variable Path, or you can create a new system variable named ABAQUS_BAT_PATH, and set the value to the file path of the Abaqus command, i.e., C:/SIMULIA/Commands/abaqus.bat. “

Related

Executing command line program (Abaqus-python script) from python script

I am trying to run an Abaqus-python script file from another python script executed in Spyder (python 3.7, windows 10).
If I run the following command in CMD
abaqus cae -noGUI model.py
the code executes as expected. However, if I try to run cp = subprocess.run('abaqus cae -noGUI model.py',shell=True)
I don't get any output. Below is what is returned in cp.
Notes:
The Abaqus-python file I am trying to run is in the same folder as the python script I have.
I have tried various sp.call, sp.run, os.systems in different string and list formats.
"C:\Abaqus\Commands" and "C:\SIMULIA\Commands" are both in my PATH environment variable.
I think the -noGUI call is important to the problem. I can't seem to find others on SO with this issue.
After a fair amount of digging, I think the issue is that the PATH for my python install is done thought Anaconda so I have a different path this vs CMD. I fixed this by adding the full Abaqus location to the command. See below.
subprocess.call(r'C:\SIMULIA\Commands\abaqus job=Job-1 ask_delete=OFF interactive', shell=True)
I am not sure what your problem is due to, but note that Abaqus scripting is based on python 2.7, therefore you might need to downgrade it

How would I allow others to use my script from anywhere in the shell without having to type out the file extension?

Excuse the awkward question wording.
I've made a script. I would like for others to download it from github, and run it by typing programName argument1 argument2, similar to any other popular app used through the terminal such as Jupyter or even opening Atom/Sublime/etc. (ex:jupyter notebook, atom .). However, unlike Jupyter or sublime, my script isn't launching another app, it's a small app meant to be used in the shell.
Currently, to use my script, one must type into the command line python programName.py arg1 etc from within the file's directory.
How do I allow others to dl it and use it from anywhere (not having to be within the directory), without having to type out the whole python programName.py part, and only having to type programName arg1?
This blog post explains step by step how to create a distribution that you can install and it would turn into an executable.
You can refer to this github repo for a sample application.
The full documentation of setuptools is available here.
In general, you should configure your setup.py in order to use the command in the entry-point option:
setup(
name = "your_app_name",
packages = ["package_name"],
entry_points = {
"console_scripts": ['cmd_name = package_name.package_name:main']
},
....
)
This solution would work on every OS where you have installed python.
Your script may need to have an interpreter, "shebang", besides being "reachable" by the $PATH
#!interpreter [optional-arg]
For example, you could have something like
#!/usr/bin/env python
or to force a specific version
#!/usr/local/bin/python2.7
Next, your script needs to be available within the $PATH, check this answer that covers that part: https://unix.stackexchange.com/q/29608/53084
You can simply add your script to PATH variable in order to launch it from anywhere.
In Linux distros, you can simply do it by using a bash command PATH=$PATH:/path/to/your/script.
Make sure you don't have the space around the "=" operator.
Now, the second thing is you don't want your script to be named as pythonProgram.py.You can simply remove the extension .py from PythonProgram.py by adding a single line to the starting of your script.
Open up your script and at the very begining type #!/usr/bin/python.This should be the first line of your code.This line is called shebang and is used to tell the bash which interpreter to be used for compiling the script.
If everything went right, you will be able to run your script as pythonProgram arg1.
In addition to mabe02: Python is a scripting language and usually not compiled, which means you will need an interpreter to run your program.
Programms made in C f.e. can be run on its own because they were compiled into machine code by a compiler.
An interpreter is similar to a compiler as it reads your script and interprets it at runntime. That is why you need to write python before your programm, to tell your computer to interpret your script on runntime, using python. This doesn't mean that there are no possibilities to compile python as can be seen in the other answer and in this link Can a python program be run on a computer without Python? What about C/C++? (py2exe and py2app).

PyCharm: How to run behave.exe?

I want to use behave lib in my program. I write code as in behave-tutorial.
But, when I "Run" my tutorial-project, I see in PyCharm-console:
C:\Python27\virtualenv\Scripts\python.exe "C:/Program Files (x86)/JetBrains/PyCharmProjects/tutorial/steps/tutorial.py"
Process finished with exit code 0
I think what I must to use behave.exe for run my project:
C:\Python27\virtualenv\Scripts\behave.exe
How to get it?
You can configure your run as per your purpose please have a look to screenhots here:
taken from: http://automated-testing.info/t/bdd-framework-debug-testov-stepov/3392/3
Even though behave support is now in the Professional Edition, it's currently not in the Community Edition.
You can still call the behave module for a given feature directory.
Add a Python Run configuration, with no script and these options:
Interpreter options: -m behave
Working directory: path/to/feature/directory
I tinkered around with previous answers and got PyCharm CE 5.x working with Behave I have python 2.7 installed system-wide, and behave installed via PyCharm package management
Here is my run config:
Script: put dot (.) in here [this way PyCharm recognizes the configuration as valid and doesn't show red cross mark]
Working Directory points to the dirctory where .feature file is
Interpreter options: -m behave
For PyCharm Community 2016.3 on mac, setup a run configuration with:
Script: /usr/local/bin/behave
Script Parameters: path/to/feature/directory
Working Directory: path/to/project/directory
This way the run & DEBUG both work.
I have 2 features file (createX.feature and createY.feature) at feature directory but I want to select only 1
My configuration is
Script: createY.feature
Script parameters: --tags=#int --tags=#e2e
Interpreter options: -m behave
Working directory: $features_directory
The "Run" window shows this execution command
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 -m behave createY.feature --tags=#int --tags=#e2e
but pycharm chooses the other feature file :(
But if I run the command at a terminal window, the command works!!
Edit: I have changed the feature file I want to execute for a better understanding: I try to execute the last one following alphabetical order
Edit 2: Solved It seems that behave does not like to have "scratch" files for control tasks at steps directory ... Once I deleted them, that handsome configuration works!!

changing .emacs to use IronPython.exe and using code completion for IronPython modules?

I configured my Emacs for code completion and other help using this link (from another question here on SO). I am a complete newbie to emacs.
Can anyone tell me what should I change so it (rope, ropemacs, pymacs, yasnippet etc) picks up symbols of IronPython modules for code completion and snippets.
Also I want to map:
C-x RET - to invoke IronPython.exe and not Python.exe (any clue on how to do that)
PS I am using Emacs with Cygwin on XP machine
The variable python-command contains the path of the python interpreter to be run. By default, this is usually just set to "python", which runs the first python in your path.
Try changing it to point directly to the IronPython exe file:
(setq python-command "C:/Program Files/IronPython 2.6/ipy.exe")

Running Python from the Windows Command Line

How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?
Wouldn't you simply save your Python code into a file, and then execute that file using Python?
Save your code into a file called Test.py.
And then run it?
$ C:\Python24\Python.exe C:\Temp\Test.py
If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.
If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.
When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.
If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test (if that is a module), or finally from the debugger.
If you put the Python executable (python.exe) on your path, you can invoke your script using python script.py where script.py is the Python file that you want to execute.
Open a command prompt, by pressing Win+R and writing cmd in that , navigate to the script directory , and write : python script.py
A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.
You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables
In DOS you can use edit to create/modify text files, then execute them by typing python [yourfile]

Categories