I installed both Python and GitBash. I was wondering how I could allow gitbash to become a terminal for a python file I would like to run.
Thanks.
I am using Python 3.4 btw.
Related
I just downloaded VS Code this week and have run into a problem accessing different versions of Python.
When I run a python file called set.py from the command line via python set.py, VS Code is accessing the Python 2.7 version that came with my MacBook. When I run python --version it confirms that VS Code is using Python 2.7.16.
However, when I right-click in the editor and select Run Python File In Terminal it appears to be using the updated version 3.9.4. I have tested this by using an fstring (available in 3.9 but not in 2.7). The fstring code throws an error in the first method but clears fine in the second. Here's my code:
s = set()
s.add(1)
s.add(2)
s.add(3)
print(f"The set has {len(s)} elements.")
And here's my terminal with the results of both methods shown.
Does anyone know what exactly is going on, and how I can execute python files via the command line?
TIA
Specs:
I am running VS Code 1.55.2 and have downloaded Python 3.9.4 to my MacBook Pro running Big Sur 11.2.3
I have also downloaded the Python extension in VS Code and have selected Python 3.9.4 64-bit as my interpreter.
On macOS and linux, you will have a system python that tends to be old and you don't want to touch it because there are system utilities that rely on it.
You will also have the user-installed python, which here is python 3.9.
If the system python is python2, then the command python will typically invoke python2 and the command python3 will invoke python3.
In VS Code on Windows, when you launch a terminal window from the IDE, it will activate the interpreter/environment you've chosen for your project (typically by CTRL-SHIFT-P and then Python: Select Interpreter). I've noticed on Linux that it will not do this (and this may also be the case on macOS), so if I want to use a specific version or environment, I need to specify it or activate it as my first command in the terminal with something like conda activate myenvname.
Unlike in the terminal, VS Code will use the selected interpreter if you run code directly from the IDE (using Run Without Debugging or Control + F5).
In lessons we've been creating and running parallel programs on a virtual machine running Linux and we've been running python programs directly though the terminal with the command line
mpi4exec -n N python my_file.py
when we moved to a mac suit we had to run the command line
export PATH=/Users/admin/anaconda3/bin:$PATH
first before mpi4exec was a recognised commmand. After this the file would run fine.
My question is firstly, what does the mac line actually mean/do and secondly, is there a Win10 equivalent that allows me to run python code directly though my PC terminal?
yes, you can run python code same as that of linux in temrinal/command prompt.
for this you have to set the python path to the window environmental variable path by yourself or you can install the anaconda which do everything, install python, python-packages, spyder ide .
I downloaded this tool to migrate MySQL to PostgreSQL: https://github.com/philipsoutham/py-mysql2pgsql
Python interactive code works properly so the python path is set in the right way.
When I type "py-mysql2pgsql" being in the directory: C:\Users\me, the downloaded tool doesn't run but ask me to choose the program to open that file. The same situation when I'm in C:\Users\me\py-mysql2pgsql
How can I run this tool properly?
Windows does not understand shebang lines in scripts (#!/usr/bin/env python) like Linux and Unix variants do. So Windows does not understand that this is a python script, you need to execute python yourself.
If python executable is in your path, you should be able to run:
python py-mysql2pgsql
If it is not in your path, you should be able to run:
path_to_python\python py-mysql2pgsql (on my machine C:\Python27\python)
Note that this applies to any python script on Windows, not just this tool.
The standard python version of ubuntu 13.04 is python 2.7.
I know that I can call a python script of version 3.3 by calling python3.3 or python3 in terminal instead of only "python", which starts the version 2.7...
e.g. python3 myscript.py
But now I have a version 3.3. script in the system start routine and can only tell the path to the file. The system recognizes it as a python script (in the shebang with #!/usr/bin/python3)
But how to open it with the correct version? It is tried to be opened with the standard python install so it wont work nor even show up.
The shebang line #!/usr/bin/python3 should work if sh, bash, etc. is trying to launch your script.
It it is being run from another script as python myscript.py you'll have to find that script and get it to launch the script using python3 myscripy.py
Can anyone please tell me an IDE for running python programs? Is it possible to run the program through command line?
Take a look at ActiveState's ActivePython. It's quite a nice implementation of Python on Windows. Another way is using Cygwin's Python port. These two are Python implementations. I don't use an IDE, I write my Python code in Notepad++.
To run a python program after saving it to C:\Users\vaibhav\Code\myscript.py:
ActivePython: If I remember right, ActiveState updates the path correctly. So it should be a s simple as:
Press "start" in the task bar
In the search field search for "cmd"
In the appearing box navigate to your folder with the python script: dir Users\vaibhav\Code
call python myscript.py and you're done
Cygwin: After installing Cygwin, you have a full-featured bash terminal on your Windows machine.
click on the Cygwin icon on your desktop
In the appearing window navigate to the folder with your python script: cd /cygdrive/c/Users/vaibhav/Code
type python myscript.py
e voila
IDE for running scripts? You can have any IDE you like, but if you need only to run python scripts you go like this:
python.exe pythonScript.py
I like the EasyEclipse for python distribution. You'd need to have python and java installed of course.
PyDev and Komodo Edit are 2 nice Python IDE on Windows.
I also like the SciTE text editor very much.
These 3 solutions make possible to run Python scripts
I tried to run a Python script with multiprocessing on windows. see this tutorial
It does not work on Windows, but on raspian it went very well. Thus I knew that it was a Windows problem. I installed cygwin and followed this tutorial Installing Python inside Cygwin.
After that I additionally installed numpy with the command easy_install numpy and now i can run python scripts with multiprocessing on windows (from cygwin).