How do I execute Python 2.7 and above in Bash commandline? - bash

I am learning python and using idle. But I wanted to know how I can execute python programs of 2.7 and 3 in the Bash terminal. Is that possible?
I'd like to be able to execute them separately.

I will often type any of the following lines in order to run my programs under the correct version.
python27 path/to/file.py
python3 path/to/file.py
python34 path/to/file.py
This requires that each of those versions be installed.

Related

Downgrade Python without change behavior of base environment

I'm trying to switch over to using Mamba for my Python installation (Windows 10). Previously, I've just been installing Python using the installer from python.org and pip.
However, I have some one-off scripts I'd like to be able to run in a default environment without having to use source activate $env every time. The newest version of Mamba uses Python 3.10, but most of these scripts were written for Python 3.9.7. I'd like to downgrade the Python installation in the default environment to 3.9.7. I've tried doing this using mamba install python=3.9.7, which initially seems to work.
However, I get the following problem. When I run Python 3.10 using python in a command prompt after installing Mamba (i.e., before running the previous command to downgrade to 3.9.7), the interpreter launches and works correctly. Any packages I add to the base environment become available.
But after I downgrade the base environment to Python 3.9.7, I get the following warning when running python:
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
The interpreter does launch, but packages added to the base environment indeed fail to load when I attempt to import them.
I can get around this by running mamba activate base (though for some reason I don't understand, this is only successful from cmd and not PowerShell, which is annoying, since that's the shell I use by default). After this, python launches the interpreter without any warnings, and packages added to the base environment load as expected.
But what I'd like to do is downgrade Python to 3.9.7 and make it work like the default 3.10 environment, where the base environment is available without needing to manually activate it every time, since this is what I want for my one-off scripts that I want to be able to just run by clicking on them.
Is there a way I can downgrade the version of Python in Mamba's base environment, but make it work like the prepackaged version (i.e., without needing to issue mamba activate base every time)? Is the way to do this just to use an older version of Mamba that comes with Python 3.9.7?
"Is there a way I can downgrade the version of Python in Mamba's base environment...?"
Yes, but you shouldn't. It's best to leave base alone - it should strictly be used for Conda/Mamba infrastructure, not for running user scripts. If you want a default Python 3.9.7 environment, then create one, disable auto_activate_base, and have that new environment activate when your shell starts up.

I can’t get Kivy to work in Visual Studio

I’ve installed Kivy with Python and when I check the version it confirms that it’s there. But when I try to use files in VS, I get the message “kivy files require # kivy”.
I’ve searched for solutions to this but I can’t find anything.
Thanks
Thomas
I had a similar problem early on. What worked for me is as follows:
Going to Terminal or Command Prompt and typing
python3 -m pip install kivy
or
python3
then
>>> pip install kivy
I used another IDE called Mu Code (for Python) and tried it there. If it works for one application, it should work for all of them. Some other applications (like Mu) have a feature called the REPL, a read-evaluate-print-loop, that works like a python version of the terminal.
Also, be sure to check that you have correctly installed the latest version of Python. As of time of writing, the most current Python version is 3.10.5. You can download it from https://www.python.org/downloads/.
You can also try using the Python IDLE.
You can check if it's installed by using python3 -m kivy at the command line.
In the end, what worked for me was a lot of troubleshooting.
Hope that was helpful.

ModuleNotFoundError: No Module Named 'requests' VSCODE Mac OS

I am having a problem running my script in VSCode.
I have tried the following:
Reinstalling requests through pip, easy_install, and sudo pip
Importing requests directly in the Python interpreter (which worked)
This leads me to believe that VSCode isn't using the correct interpreter that actually has the package installed.
You can choose your interpreter in VS Code in several ways. One of them is by clicking the tab in the bottom toolbar (lower-left) which might say "Python 3.9.4 (64-bit)", upon which you will be able to select from a menu of choices, e.g. /usr/bin/python3 or a conda or brew installation, etc.
I think yours is python environment problem. To check whether it's python environment or not, you can use which python (or which python3 if you use python3) command in your both vscode terminal and mac terminal. If you see different python path, then your vscode is using different python environment. You can change the python interpreter in vscode to have same environment as your mac terminal.
Open Command Palette in vscode with ⇧⌘P
Type "Python: Select Interpreter"
Then, choose the same environment as your mac terminal (If you want the same one, you can choose the same python environment path as mac terminal that you get with "which python" or "which python3").
You may be able to find a solution here: ImportError: No module named requests
You haven't mentioned using different versions in your commands. It's possible you need to use pip3 instead of pip, or python3 instead of python.

In setup.py, how to run a system command before any pip install?

I am creating a Python package with setup.py, and I need to run certain shell commands before pip attempts to install dependencies. In fact, I need these commands to run before setuptools makes network calls to PyPI.
(The nitty gritty context is that the system installing this package has an internet gateway which requires a certificate to be installed. I need to apply this system change before setuptools reaches out to the internet)
I'm aware of cmdclass -- do those commands run before the install_requires stage?
You can't run arbitrary commands at install time (for the reasons linked to by phd in a comment to your question).
Maybe there are tricks to make it possible, but they are bad practice and not even worth the trouble.
What I would rather recommend to do is just clearly document the pre-installation steps, and maybe write yourself a shell script (or Python script) that wraps the custom pre-install commands and the actual installation command.
import os
os.system('cmd /c "Your Command Prompt Command"')
write this code to setup.py file before pip install code

Mac OS - Installed Python 3 via Anaconda, but no feed back of which python3

As a Python beginner, I installed Python 3 via Anaconda, and have successfully installed it in my Mac laptop. I have no problem to use Python 3 if I launch Jupyter notebook, but I cannot locate it in the terminal.
Specifically, if is type in:
$ which python
I got the feedback of
/usr/bin/python
but if I type in
$ which python3
There is no feedback at all. Just curious if I have missed anything.
When you install a new environment with Anaconda with a specific version of Python, that version of Python will be accessible only if you activate the Anaconda's environment.
If you want to access to your python 3 of your environnement from the terminal, you need to activate it :
source activate my_env
Then you can write "which python3"
For example, on my computer, you can see two different versions of Python depending on the environnement I use or the version installed on my Mac.
// On my mac
which python3
>> /Users/michaelcaraccio/anaconda/bin/python3
Then when I activate the env :
which python3
>> /Users/michaelcaraccio/anaconda/envs/TM/bin/python3
If you need more information, don't hesitate to ask :)

Resources