Python code in OSX Terminal not compiling right - python

So I am trying to teach myself to program in Python while on spring break and I have run into a roadblock. I can get my code to compile in PyCharm, but I would really like to get it to compile correctly in Terminal because vim is my text-editor of choice. Does anyone have any idea of why my code may not be compiling correctly?
Here is my code compiling correctly in PyCharm
Here is my code compiling incorrectly in Terminal
Thank you in advance for any help.

Any recent OSX version comes with Python 2.7 as a standard. When you install Python 3.x, you have both versions on your system. The standard way of using python -command- in the terminal calls Python 2.7. You can call a command using python3 -command- instead to use Python 3.x. You could set an alias on python3 in .bash_profile to call it instead when you use python.

You have configured PyCharm to use an installed Python 3, but at the terminal you appear to be using Python 2. There is a difference in how the two versions output the results of print. Try modifying your terminal session to direct your path to use the installed Python 3 instead of the installed Python 2.

Related

Terminal's python version and Eclipse's python version mismatch

My MacBook came preinstalled with Python 2.7.16, and I downloaded Python 3.8.5. To my understanding, the operating system needs Python 2.x so I did not uninstall it.
Eclipse (using Pydev) is the IDE I'm using.
I set up two interpreters, one for python and the other python3.
I set up one project for each interpreter to make sure I set them up correctly.
The script is:
import sys
print(sys.version)
When I run it with the python interpreter, I correctly get version 2.7.16.
When I run it with the python3 interpreter, I instead get 3.8.2.
Running python -V yields ``Python 2.7.16. Running python3 -VyieldsPython 3.8.5```.
Why does the interpreter return one version and the terminal another?
I'm at a loss for how to troubleshoot or fix this, or if it is a non-issue.
To clarify, you get the 2.7.16 and 3.8.2 versions when running your program from within Eclipse. The python -V is clearly from the command line. The interpretation is that your Eclipse environment came with its own python interpreter which happened to be 3.8.2. Have you tried running your script from the command line with python3 path/to/your/script.py? This probably gives 3.8.5. I don't see a real problem here in most cases you do not care whether you have python 3.8.2 or 3.8.5.
The "biggest" issue is a cosmetic one that you have two python3 installations which is a bit of a waste. When using additional libraries, you may find that you have to install them in your Eclipse environment and in your command line if you want to use your scripts in both environments which would be a bit tedious. If this does turn out to be a problem, check in Eclipse whether there is any way to change your python3 configuration to use the interpreter used by the command line (sorry cannot be more specific, it's a long time that I used Eclipse).

Can I temporarily set python to default to an earlier version?

Can I temporarily set python to default to an earlier version?
I have been trying to run a few scripts I downloaded from GitHub written in python 2.7 and my default python version is 3.7.6. I know that I can run one script with 2.7 using: python2 FoEC.py -i ./testdata/ for instance. However, this script calls other .py scripts also written in 2.7 that I believe are running using 3.7.6 (the keep getting syntax errors which don't appear when run in 2.7). Is there a way to ensure that all scripts are run using python 2.7?
I hope that this makes sense.
Thanks,

I changed "python.exe" to "python3.exe" but now pip returns an error, how can i fix this?

So in order to make my life easier, I changed Python 2.7 to "python2.exe and Python 3.6 to "python3.exe". I then added them to my path and am able to reference them in which python2 opens Python 2.7 and python3 opens Python 3.6.
This is exactly what I want but now since I changed the names the pip command returns this:
Fatal error in launcher: Unable to create process using '"'
I still want to be able to reference python3 and run Python 3.6, but I need pip to work. Do I change a pip file to reference it? or is it something else? Please help if you can.
I strongly recommend you change the names back, because numerous scripts (pip likely included) expect the original filename.
To make your life easier when launching python files on windows, python comes with a more flexible solution. If you have a recent python3 installed on windows, the Python Launcher for Windows should be installed as well. That means you can run python 2 and 3 scripts with a py <file> command. The launcher respects shebang lines specifying the python version or you can pass a flag to choose the version manually: py -2 <file>

Running or compiling a Python script on Windows

I want to modify this Python script on Windows 7.
https://github.com/Jeremy1980/LDBoxer
The original author seems to have compiled the program into a Windows executable. Is it possible to run the script without compiling it? I tried the following at the command prompt:
python LDBoxer.py
But Windows says it does not recognize 'python'. What do I need to install and what is the correct command line syntax? According to the docs, this is the correct way to run the executable:
LDBoxer_2017a.exe ldraw_library_location ldraw_model_location_for_conversion
Thanks.
You need to make sure you have python installed, then make sure you are running the correct python compiling command. Sometimes when installing python 2 you need to run the command python2 or python27.
You can install python here. It looks like they wrote it with python 2.x so I would recommend installing python version 2.7 unless you want to manually convert it to python 3.x.
You should be able to run the .exe just by double clicking, or right click then run.

Run python program from terminal

I have downloaded a python program from git.
This program is python 3.
On my laptop i have both python 2.7 and python 3.4. Python 2.7 is default version.
when i want run this program in terminal it gives some module errors because of it used the wrong version.
how can i force an name.py file to open in an (non) default version of python.
I have tried so search on google but this without any result because of lack of search tags.
also just trying things like ./name.py python3 but with same result(error)
When you type "python", your path is searched to run this version. But, if you specify the absolute path of the other python, you run it the way you want it.
Here, in my laptop, I have /home/user/python3_4 and /home/user/python2_7. If I type python, the 3.4 version is executed, because this directory is set in my path variable. When I want to test some scripts from the 2.7 version, I type in the command line: /home/user/python2_7/bin/python script.py. (Both directory were chosen by me. It's not the default for python, of course).
I hope it can help you.
Use the shebang #!<path_to_python_version_you_want>
As in:
#!/usr/bin/env python
at the very top of your .py file
Also checkout: Should I put #! (shebang) in Python scripts, and what form should it take?
The Method of #Tom Dalton and #n1c9 work for me!
python3 name.py

Categories

Resources