How to execute latest version python which just been installed? - python-3.x

I just installed the latest python(3.7.6).
Then I open python file as I usually do, my Mac open the file with version 3.7.3, not 3.7.6(as shown in the image)
How to open a python file with latest version of Python installed on PC?
enter image description here

just execute the below code if your .path to py file is test.py.
python3 test.py
if you only have one python in you machine, then you can execute the below command.
python test.py

Related

ModuleNotFoundError: No module named 'docx' on VScode but not in Python itself

I have installed the python-docx module using pip:
python -m pip install python-docx
However, when I try to run my script that only contains import docx, I get the following error
ModuleNotFoundError: No module named 'docx'
When I execute the following command in Python on the command line, it works fine:
import docx
When using Visual Studio Code, make sure that it uses the same Python version as you are using when running the script. You you should set the "python.pythonPath" setting correctly in settings.json (manually edit the file or edit in Visual Studio itself). This can be done system-wide (per user), or per project.
Furthermore, this answer gives some background about how Python files are executed in Windows.

Unable to import Libraries even though Installed in Anaconda on Pycharm

I am using Ubuntu 18.10(dual Boot win10), I have installed anaconda (conda version- 4.6.7) on ubuntu. After this Installation I installed Pycharm via snap on Command Prompt. Since Pycharm(version - 2018.3.5) didnt Detect my python Location automatically I set the python Interpreter via
Adding Location ->System Interpreter (As I didn't want any Venv) -> Path
But when I try to import opencv in Pycharm it says
ModuleNotFoundError: No module named 'cv2'
Python3.6 in Anaconda Bin is shown to which I have linked in Pycharm (I have also tried other Python files there as well but no luck)
When I run in Terminal: python3 -c 'import sys; print(sys.path)'
It shows:
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6
/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/local
/lib/python3.6/dist-packages/setuptools-39.1.0-py3.6.egg', '/usr/lib`/python3
/dist-packages']
So could anyone help me out in this?? Thank You in Advance!!
Edit 1:
I am also unable to access any of the libraries from terminal Itself. So Could anyone help me with that as well. Should I export path of this Python in Anaconda bin folder to bashrc file in Ubuntu?
Edit 2:
The libraries affected is not just opencv. There are few other Libraries like Portaudio which are affected as well along with Opencv

PythonMagick Unable to open config file

I am running Python 3.6 in a venv on 64 bit Windows 10 inside PyCharm. Here are the steps I performed:
Open PyCharm and start a new project using Python 3.6 as the venv.
Downloaded the PythonMagick from a wheel file for Python3.6 from this source:PythonMagick wheel file
Open the terminal in PyCharm and run:
pip install PythonMagick-0.9.19-cp36-cp36m-win_amd64.whl
Download ghostscript from here: Ghostscript 9.25 for Windows (64 bit) and run the exe file.
Add the ghostscript directory C:\Program Files\gs\gs9.25\bin to the user PATH environment variable.
Now I run the sample file from here
import PythonMagick
if __name__ == "__main__":
pdf = 'a.pdf'
p = PythonMagick.Image()
p.read(pdf)
p.write('doc.jpg')
I get the following error:
RuntimeError: Magick: UnableToOpenConfigureFile `delegates.xml' #
warning/configure.c/GetConfigureOptions/714
How do I fix this error?
When installing PythonMagick in a VENV, apparently you need to also add a system variable called MAGICK_HOME so that Magick can find the config files.
Add the following to the User Variable
MAGICK_HOME = %your-project-dir%\venv\Lib\site-packages\PythonMagick
Then restart PyCharm.

Still running python 2.7 when running python in windows 10 command prompt

When I'm running Flask it runs python 2.7, I want it to run on python 3.6.
When I type into the command prompt:
python
it shows it is running python 2.7
So when I type into the command prompt:
echo %PATH%
it shows that python 3.6 is on the top of the variable list.
SO when the path to 3.6 is on top of the list it should automatically run flask in python 3.6 right?
Your PATH is misconfigured, it should only contains path to folders containing binaries, not path to the binary itself
You should change C:\Python36-32\python.exe to C:\Python36-32

How to change .py to .exe

I made a program in python which is in .py format. However, I would like it to be in .exe
The options I have found are:
py2exe
pyinstaller
The problems with those 2 are because I am running python 3.6 and those 2 programs do not support it, HELP!
Python 3.6 still isn't supported by Pyinstaller. So in order to use it you're gonna need Python 3.5 or below. So, you might wanna change your python version to 3.4 or 2.7. Then you can follow the procedure for creating the exe with pyinstaller. Also I think pyinstaller is a better option than py2exe.
However to make things work without switching to other versions, you might wanna try using cx_freeze:
1.Install the latest version of cx_freeze by pip install cx_Freeze.
2.Create a new python file named ‘setup.py’ on the current directory of your script.
3.On the setup.py, code this and save it:(here my prog.py refers to your .py file name)
from cx_Freeze import setup, Executable
base = None
executables = [Executable("my prog.py", base=base)]
packages = ["idna"]
options = {
'build_exe': {
'packages':packages,
},
}
setup(
name = "<any name>",
options = options,
version = "<any number>",
description = '<any description>',
executables = executables
)
4.With shift pressed right click on the same directory to open a command prompt window.
5.Type: python setup.py build.
6.Check the newly created folder ‘build‘. Within that folder you can able to find your application.
pyinstaller
pip install pyinstaller
Basic usage is very simple, just run it against your main script:
pyinstaller /path/to/yourscript.py
cx_Freeze
pip install cx-Freeze
auto-py-to-exe
pip install auto-py-to-exe
Then to run it, execute the following in the terminal:
auto-py-to-exe
With a simple google search, I found this question which gives a link to a version of py2exe which happens to support python 3.3 and up. Here is the link!
On that page you will find the following bit of information:
Py2exe is a distutils extension which allows to build standalone Windows executable programs (32-bit and 64-bit) from Python scripts; Python 3.3 and later are supported. It can build console executables, windows (GUI) executables, windows services, and DLL/EXE COM servers.
Notice:
Python 3.3 and later are supported.
I have created a batch file to do the work, but first, go to https://datatofish.com/add-python-to-windows-path/ and add python to your path.
Then create a new notepad file with this:
SET /P _input1= Enter the file directory:
cd %_input1%
SET /P _input2= Enter the file name:
pyinstaller --onefile %_input2%
ECHO The exe file is in the dict folder in %_input1%
pause
Save this as a .bat file anywhere and run it.
Enter the directory of the python script.
then the name.
Hope this helps!

Resources