2018年01月31日 16:24:43 phs999 阅读数:11177 标签: python 更多
个人分类: Python
总的解决思路就是用pip命令安装缺少的包(库)。假定python中已经默认安装了pip命令,假如我缺少numpy包的话,在cmd中python3执行:
pip3 install numpy
pyhton2的话执行:
pip install numpy
如果提示不能执行pip命令或pip命令没有安装,则进行下面的pip安装操作后,再进行上面的安装包的操作。
在安装了基本python环境后,浏览器打开下列网址,右键-另存为get-pip.py。保存到python安装目录,如:“C:\Python36”
https://bootstrap.pypa.io/get-pip.py
然后双击执行本地的get-pip.py文件(或者打开cmd,执行python get-pip.py).
安装完毕后,可使用pip命令安装相应的包。
由此解决了
python:ModuleNotFoundError:No module named “numpy”问题。
其他类似问题用pip安装相应的包即可。
1---------------------------------------
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class ImageViewer
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new ImageViewerFrame();
frame.setTitle("手写识别系统");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class ImageViewerFrame extends JFrame
{
private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH = 600;
private static final int DEFAULT_HEIGHT = 768;
private void welcomePage()
{
ImageIcon icon = new ImageIcon("./welcomePage.jpg");
icon.setImage(icon.getImage().getScaledInstance(DEFAULT_WIDTH, DEFAULT_HEIGHT, Image.SCALE_DEFAULT));
label.setIcon(icon);
}
private void initOptionPane()
{
UIManager.put("Label.font", new Font("Dialog", Font.PLAIN, 78));
}
private String getAnswer(String path)
{
PyCaller.writeImagePath(path);
PyCaller.execPy();
return PyCaller.readAnswer();
}
public ImageViewerFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocationRelativeTo(null);
// use a label to display the images
label = new JLabel();
add(label);
// set up the file chooser
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
// set up the menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
welcomePage();
openItem.addActionListener(event -> {
// show file chooser dialog
int result = chooser.showOpenDialog(null);
// if file selected, set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION) {
String path = chooser.getSelectedFile().getPath();
ImageIcon icon = new ImageIcon(path);
icon.setImage(icon.getImage().getScaledInstance(DEFAULT_WIDTH, DEFAULT_HEIGHT, Image.SCALE_DEFAULT));
label.setIcon(icon);
// my codes
initOptionPane();
String answer = getAnswer(path);
JOptionPane.showMessageDialog(null, "识别结果:" + answer + " ");
}
});
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(event -> System.exit(0));
}
}
---------------------------------------
2
import java.io.*;
class PyCaller
{
private static final String DATA_SWAP = "temp.txt";
private static final String PY_URL = System.getProperty("user.dir") + "\\test.py";
public static void writeImagePath(String path)
{
PrintWriter pw = null;
try
{
pw = new PrintWriter(new FileWriter(new File(DATA_SWAP)));
}
catch (IOException e)
{
e.printStackTrace();
}
pw.print(path);
pw.close();
}
public static String readAnswer()
{
BufferedReader br;
String answer = null;
try
{
br = new BufferedReader(new FileReader(new File(DATA_SWAP)));
answer = br.readLine();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return answer;
}
public static void execPy()
{
Process proc = null;
try
{
System.out.println(System.getProperty("user.dir") + "\\test.py");
// proc = Runtime.getRuntime().exec("ipython3 " + PY_URL);
proc = Runtime.getRuntime().exec("python " + PY_URL);
proc.waitFor();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException, InterruptedException
{
writeImagePath("D:\\labs\\Yao\\test.jpg");
execPy();
System.out.println(readAnswer());
}
}
3-----------------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test
{
public Test()
{
}
private static String filePathTxt = "C:\\workspace\\JAVAAndPython\\temp2.txt";
public static String readAns()
{
String str = "";
BufferedReader br;
String answer = null;
try
{
br = new BufferedReader(new FileReader(new File(filePathTxt)));
answer = br.readLine();
// String regEx = "count(\\d+)(df)";
String regEx1 = "([1-9a-zA-Z]+)\\$\\$\\$([1-9a-zA-Z]+)\\$\\$\\$([1-9a-zA-Z]+)\\$\\$\\$\\$([1-9a-zA-Z]+)\\$\\$([1-9a-zA-Z]+)\\$\\$\\$";
// String regEx = "([1-9a-zA-Z]+)\\$\\$\\$";
// answer = "111$$$";
Pattern pat = Pattern.compile(regEx1);
Matcher mat = pat.matcher(answer);
if(mat.find())
{
System.out.println(mat.group(0));
System.out.println(mat.group(1));
System.out.println(mat.group(2));
System.out.println(mat.group(3));
System.out.println(mat.group(4));
System.out.println(mat.group(5));
}
else
{
System.out.println("-----not match");
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return answer;
}
public static void main(String[] args)
{
readAns();
}
}