目前分類:Python (6)

瀏覽方式: 標題列表 簡短摘要

if __name__ == "__main__":

__name__是python的隱含變數,代表『模組名稱』,語法都會出現在Python 檔案 (*.py) 裡面,決定程式加了這段 if條件式 冒號後的指令,是否要執行,

正常狀態下都要加入這行,因為檔案有可能會被其他檔案所引用,加入此語法就可避免此程式被引用後就立即執行了

 

python檔案的兩種使用情形:

(1)單獨檔案進行測試:python abc.py

__name__ 會等於 "__main__"這個值

(2)讓其他檔案引用 import abc.py, 假如檔案叫做def.py

此時__name__ 會等於 "def" 這個值

參考:

JC 發表在 痞客邦 留言(0) 人氣()

查詢OpenCV在ANACONDA雲上有的那些可以下載

https://anaconda.org/search?q=opencv

按照被下載的次數排列, 這裡挑選第一項當試驗

指令: conda install -c conda-forge opencv

參考:https://anaconda.org/conda-forge/opencv

 

Proceed ([y]/n)? y

文章標籤

JC 發表在 痞客邦 留言(0) 人氣()

由C#程式代為執行以下命令 python.exe main.py var1 var2

main.py是python的主要程式碼, 由外部指令傳入兩個參數var1與var2做相乘開根號的運算

main.py的程式碼如下:

#main.py
import numpy as np
import multi
import sys

def func(a,b):
    result=np.sqrt(multi.multiplication(int(a),int(b)))
#    result = a * b
    return result

#x = func
#print(x(3,5))

if __name__ == '__main__':
    print(func(sys.argv[1],sys.argv[2]))

C#程式碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Diagnostics;
using System.IO;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        public string filePythonExePath;
        /// <summary>
        /// ML Sharp Python class constructor
        /// </summary>
        /// <param name="exePythonPath">Python EXE file path</param>
        public void MLSharpPython(string exePythonPath)
        {
            filePythonExePath = exePythonPath;
        }
        /// <summary>
        /// Execute Python script file
        /// </summary>
        /// <param name="filePythonScript">Python script file and input parameter(s)</param>
        /// <param name="standardError">Output standard error</param>
        /// <returns>Output text result</returns>
        public string ExecutePythonScript(string filePythonScript, out string standardError)
        {
            string outputText = string.Empty;
            standardError = string.Empty;
            try
            {
                using (Process process = new Process())
                {
                    process.StartInfo = new ProcessStartInfo(filePythonExePath)
                    {
                        Arguments = (filePythonScript + " 3 3"),
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        CreateNoWindow = true
                    };
                    process.Start();
                    outputText = process.StandardOutput.ReadToEnd();
                    outputText = outputText.Replace(Environment.NewLine, string.Empty);
                    standardError = process.StandardError.ReadToEnd();
                    process.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                string exceptionMessage = ex.Message;
            }
            return outputText;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string err;
            string outResult;
            //    _writer = new ListBoxWriter(listBox1); // DebugWin is the name og my listbox
            //    Console.SetOut(_writer);
            listBox1.Items.Add("Button 3");
            MLSharpPython(@"D:\_APP\Anaconda3\python.exe");
            outResult = ExecutePythonScript(@"d:\main.py", out err);

            textBox3.AppendText("Result= " + outResult + "\n");
            textBox3.AppendText("Error= " + err + "\n");

            textBox3.ScrollToCaret();

        }

    }
}

參考:

■介紹使用C#呼叫Python語法, 應用於影像處理及機器學習的程式架構 (Using C# to run Python Scripts with Machine Learning Models )

https://medium.com/@ernest.bonat/using-c-to-run-python-scripts-with-machine-learning-models-a82cff74b027

■C#呼叫Python的四種方法

https://www.itread01.com/content/1545323704.html

■MSDN - Process.BeginOutputReadLine Method

文章標籤

JC 發表在 痞客邦 留言(0) 人氣()

下載需要的*.hwl, 命令列指令cd到下載的路徑下, ex: d:\temp\abc.whl

pip install d:\abc.hwl

參考:

How do I install a Python package with a .hwl file?

https://stackoverflow.com/questions/27885397/how-do-i-install-a-python-package-with-a-whl-file

 

Python擴展包的Windows非標準二進位文件, 以numpy為例

https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy


文章標籤

JC 發表在 痞客邦 留言(0) 人氣()

一般是因為使用不同編輯器所導致, Tab與Space不能混用, 都改成用Tab或Space即可

 

Spider編輯器如何查看空白的地方是Tab 或 Space,

Tools->Preferences


文章標籤

JC 發表在 痞客邦 留言(0) 人氣()

安裝pytube發生以下的訊息

Could not install packages due to an EnvironmentError: [WinError 5] 存取被拒

解決方法:

修改Anacoda捷徑的內容, 將系統管理員身分執行打勾

完成!  重新在開啟Anaconda就會是系統管理員的身分, 之後pip install pytube安裝就沒問題了.


文章標籤

JC 發表在 痞客邦 留言(0) 人氣()