How to run Python3 function script in terminal Visual Studio Code - python

so far I have been ok with debugging and running in a python debug terminal. Now that I'm being forced into functions, i have to input the arguments when launching the script, for example:
./python.py BIRTHDAY AHMED
and have it spit out something along the lines of this:
have a happy BIRTHDAY enjoy the day AHMED
problem comes down to me not knowing how to setup a terminal for such use. i have a bash terminal, however it wont recognize my python interpreter (#!/usr/bin/python3).
I added "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" to my user settings to gain the bash terminal as well as installed Git.
Any ideas on how i can get a work in terminal that i can input arguments and recieve an output would be most appreciated. (running on windows 10)

You are providing the path of Git Bash, which only provides enough bash-like functionality to use git. That is why running .sh (Bash Scripts) or .py files doesn't work by default.
If you add python to your PATH environment variable, that will make it so running python.exe filename.py [ARGS] will work on the Integrated Terminal.
Another solution is to install WSL if you are on Windows 10, which does provide full* bash functionality under a modified Ubuntu shell. Then just provide VSCode the path to the WSL bash.exe. (How to here)

Related

opening Python IDLE with AutoHotKey

I've been messing around with AutoHotKey, trying to create some useful shortcuts for myself. I can open file explorer, calculator, chrome, etc., but I haven't been able to write a program that opens Python IDLE.
My program says:
!t::
Run pythonw
return
I've tried pythonw.exe, python.exe, python, python3.exe, and a bunch of other combinations. Any idea why it isn't working? Thanks!
So, your problem is that Python IDLE is different from pythonw.exe, python.exe, python, python3.exe .
Python IDLE is an IDE or Code Editor provided with python whereas pythonw.exe, python.exe, python, python3.exe are the python interpreter.
pythonw.exe - for executing python scripts without opening console window(Hidden)
python OR python3.exe OR python3.exe - are the same, that is the normal python interpreter.
So to open IDLE you should execute the IDLE file not the interpreter.
The path for IDLE(in Windows Computer) is usually :
C:\Python39\Lib\idlelib\idle.bat
Here the Python version is 3.9 but it may be different in your case!
Check for the version you installed by opening command prompt and typing :
C:\>python --version
Python 3.9.1
Turning the info that Jaysmito provided into an AutoHotkey Script:
!t::
Run C:\Python39\Lib\idlelib\idle.bat
return
However, the Python39 part of the directory above will vary based on which version of python you have installed. To check which version of Python you are using, use the python --version command in a command prompt window. Then if your version of Python is not in the 3.9.* range, change the directory above to match your version.
For example, if your version is in the 2.7.* range, your directory would be something like Run C:\Python27\Lib\idlelib\idle.bat, with the Python39 from above changed to Python27.
idle.bat is Windows specific, and there are Windows-specific reasons for its existence. The generic way to start IDLE in its default startup mode (partly user customizable) is somepython -m idlelib where somepython refers to and start a python binary. It can be python, pythonw (Windows), python3 (*nix), or py -3.y (Windows). For 2.x, use idlelib.idle instead of idlelib.

bash on Ubuntu on windows Linux, folder recognition, and running Python scripts

I'm new to Linux. I recently downloaded Bash on Ubuntu on Windows 10 (after the Anniversary edition update to Windows 10). Since this update is relatively new, there is not much online regarding troubleshooting. There are two things I need help on:
(1) When I go to the home folder, which seems to be "C:\Users\user\AppData\Local\lxss\home\user" and I add a new folder through Windows, this folder does not show up in Linux with the "ls" command. But when I add a directory using "mkdir" in Linux, the "ls" command shows this folder. Why is it behaving like this? Am I limited to creating folders through "mkdir" when working in this folder?
(2) I have a Python script sitting in that same folder that I'm trying to run and again it is not being found by Linux or the Python interpreter started in Bash on Ubuntu on Windows. I have Python 3 installed (Anaconda) and I'm able to type commands directly in the Python interpreter and it's working. However, I would like to run scripts in files.
Please let me know if more information is needed. Thanks.
The reason why ls is not showing anything is that it shows the Linux directory structure. Try setting it to the Windows directory, in this example the c drive:
cd /mnt/c
Does ls show a folder structure now?
Looks like you are having permissions issues.
To see everything on your home folder try
ls -al
to change permissions check out the chmod command
How about using Python for Windows and NotePad++ to edit and run your Python scripts?
https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe
You can setup NotePad++ as described here.
How to Execute a Python File in Notepad ++?
(I ended up using Cloud9 https://c9.io/ for Python. It is independent of your local environment or OS)

Django-Admin.py in Powershell

I think I'm encountering a weird bug in Windows 8.1 trying to use django-admin.py (and any script really) in Powershell. I've successfully installed Python 2.7.6, and have added C:\Python27\ and C:\Python27\Scripts to my path.
When I try to use django-admin.py or python django-admin.py in Powershell, I get a file not found error.
Interestingly, when I use Command Prompt, I am able to use django-admin.py, even without the python prefix.
To execute scripts in Powershell, you need to set the execution policy to something other than restricted. For example:
Set-ExecutionPolicy RemoteSigned
This should allow general scripts (like .ps1 files) to be run. I still had problems running python files from the path so I added .py files to the PATHEXT variable. In Powershell:
$env:PATHEXT += ";.py"
I don't think this is permanent, so you may need to add it to your profile or change the environmental variables from the Advanced System Settings. This fixed the problem for me.

Running python from the mac terminal

I have installed the new python release and would like to run .py files from the terminal.
How is this done from the terminal? I dont want to include the path in each command to run a .py file.
If you want to override the python command, you can set your PATH variable correctly, e.g. in your ~/.bash_profile:
export PATH=/path/to/python/:$PATH
That said, for managing different versions of components that are also provided by Mac OS X, I suggest to use a package manager such as Homebrew.
if you add a shebang at the start of the python file then you can run a python file by just its name from terminal
add #!/usr/bin/python
for mac(others add your respective path for python)
at the top of your python program and from your terminal you can run it just by filename(if it has executable permissions).
Have a look at the Python package under Applications. There is a shell script there called Update Shell Profile.command
Run this and it should set your path up properly.
Unless you mark you script as executable with chmod +x, you'll need to run python over it first. e.g. `python myscript.py'
I installed all of my python through macports, which has pros and cons. One of the benefits is that you don't have to worry about stuff like this, it just works. You can install python 2.6 and python 2.7 (and others), and then use the python_select utility to set up which python is run when you call "python blah.py"
Since you have installed a working python, the easiest way to run python files from the terminal is to cd your terminal to the directory where the file is located and then just type python my_code.py in the terminal.

How to set python enviroment variable on windows

I've downloaded python installer from http://www.python.org/ftp/python/3.1.2/ , this python-3.1.2.msi file, I need to execute some python files? How do I do that? For example in php I'd do php filename.php from console, I do however have python command line but I don't know how to execute those files.
So if I could set ENV variable to directly execute my file(s) if that is possible that would be great.
There is an option in the installer called "Register Extensions" to associate Python files with the interpreter, so double-clicking them or entering filename.py in the console should work.
Apart from that you might want to add C:\Python31 to your PATH variable (right-click on My Computer, choose Settings, choose the Advanced Tab - there you can access the system variables. Better do this as an admin.
If you type python in the Windows command line, what happens? Is the Python interpreter in your PATH yet?
If not, add the Python installation directory there (here's a good guide). Then just do python script.py just like with PHP.
you can just execute
python yourfile.py
Or if the python command don't work you have to give the absolute path to you python installation or add it to windows path

Categories

Resources