Tag Archives: pycharm

Error reporting of importing tensorflow package from pycharm

 [ Note] pycharm imports the tensorflow package and reports an error

If you also encounter this problem, it means you don’t understand where tensorflow is

After installing Anaconda 3.6, set interpreter in pycharm. This interpreter determines how to execute the code you write in pycharm environment

If your setting is python.exe under anaconda. You will find that import tensorflow is written in pycharm   When as TF, an error will be reported, indicating that there is no tensorflow module. This is because python.exe in Anaconda file is version 3.6, and the CONDA environment you created is under anaconda, and the tensorflow you installed is in CONDA environment, not Anaconda environment. Therefore, if you want to import tensorflow, you need to select python.exe in CONDA environment as the interpreter

When your interpreter selects python.exe under Anaconda in the red box shown in the figure below, you will be prompted that the tensorflow module is not found when importing tensorflow in pycharm environment

 

If you want to use tensorflow, the interpreter should choose the following path (Anaconda – > envs-> tenfowflow-> Python.exe under python. Exe)

The correct selection of the interpreter is shown in the figure. Click the setting button indicated by the arrow in the figure below, select add local, and select python.exe of the path shown in the figure above instead of python.exe under anaconda. After selecting python.exe under CONDA environment, the settings shown in the red box in the figure below will be displayed

 

Handling method of PIP error in pycharm installation

When using pycharm these days, I found that I reported it when installing the software  module ‘pip’ has no attribute ‘main’  , later, the online methods and analysis error prompts were integrated because there was a packaging in the pycharm installation directory_There are two pieces of code in the tool.py file. There are some problems with the configuration and some changes need to be made

The original code is as follows:

 1 def do_install(pkgs):
 2     
 3     try:
 4         import pip
 5     except ImportError:
 6         error_no_pip()
 7     return pip.main(['install'] + pkgs)
 8 
 9 
10 def do_uninstall(pkgs):
11     try:
12         import pip      
13     except ImportError:
14         error_no_pip()
15     return pip.main(['uninstall', '-y'] + pkgs)

Modify it to the following code:

 1 def do_install(pkgs):
 2     
 3     try:
 4         #import pip
 5             try:
 6                     from pip._internal import main   
 7             except Exception:
 8                     from pip import main
 9     except ImportError:
10         error_no_pip()
11     return main(['install'] + pkgs)
12 
13 
14 def do_uninstall(pkgs):
15     try:
16         #import pip
17             try:
18                   from pip._internal import main
19             except Exception:
20                   from pip import main        
21     except ImportError:
22         error_no_pip()
23     return main(['uninstall', '-y'] + pkgs)

If you report an error after modification, it may be a problem of indentation. You must pay attention to the indentation of the code. Python’s indentation requirements can be called abnormal

 

Solve the problem of error reporting when installing Python Library in pycharm

Recently, I was playing wechat Turing robot, but I installed some libraries, reported errors, and searched the Internet for a long time. I summarized two methods and recorded them

 

method 1:

Install manually, go directly to the official website and download the python library you need locally

Put it in the python installation path, C: \ users \ blue silver heart \ appdata \ local \ programs \ Python \ Python 37 \ lib \ venv \ scripts

PIP install – U library name

 

 

method 2:

Install in pycharm, but because many libraries are foreign and the domestic connection is not very friendly, some library installations will fail or fail to connect directly due to unstable network, such as

 

 

 

 

Solution

PIP source for domestic, I personally think Tsinghua source is relatively stable

Tsinghua source

https://pypi.tuna.tsinghua.edu.cn/simple/

 

 

 

 

Install again, successful

 

 

last

In comparison, it’s easier to install Python libraries automatically in Python. Manual installation is too cumbersome. I don’t like it

 

Error reporting of import basemap in pychar

The direct cause is that the system cannot find this proj_Lib environment variable, so an error is reported

Root cause: when calling the basemap module, the proj4 module that the basemap depends on will be called. Therefore, when installing the basemap module on anaconda, you can see that the proj4 module is also installed. But the most pitiful thing is that when installing proj4 under Anaconda root, you will not actively set the environment variables of proj4 module, which leads to such errors

Solution: first find the installation path of proj4 in Anaconda installation directory. Mine is in D: \ software \ Anaconda \ Pkgs \ proj4-4.9.3-hfa6e2cd_8. Then open this folder and enter library – > Share folder, select and copy all the files inside, and paste them into the folder D: \ software \ Anaconda \ share \ proj (if there is no proj folder in the root directory of anaconda, create one)

Then, set the environment variable, that is, create a new one under the user variable

Solve the problem of clear read only status move failure in pychar move file under win7

Front end developers must read! Starting from scratch, teach you to build a low code website platform in stages>>>

Problem Description:

If you move the files in pychar to the specified folder or drag other files to the specified folder in pychar, the following problems will occur, resulting in the failure of File Movement:

The causes and solutions of this problem are as follows:

First, the files created under pychar are read-only by default. We need to enter the attribute of the specified folder and remove the check of read-only

Second, there is a lock button in the lower right corner of pychar, which you need to click to open

Details of this lock can be found in the official document:

https://www.jetbrains.com/help/idea/2016.1/changing-read-only-status-of-files.html?origin=old_ help#d1969355e156

Pycharm introduces numpy error: ImportError: Importing the multiarray numpy extension module failed. Most likely you are trying to import a failed build of numpy.

The software is pychar with Anaconda installed

My initial error was that numpy could not be called in pychar, and the error was modulenotfounderror: no module named ‘numpy’. This problem was solved by the blog. In fact, we need to use Anaconda’s python.exe

But when you run the code:

import numpy as np

arr = np.random.randint(1, 9, size=9)
print(arr)

The following error occurred again:

File “C:\python\Anaconda3\lib\site-packages\numpy\core\__ init__. py”, line 26, in < module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you’re working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.

Or error, after their own exploration, found a solution, the original version of their numpy is too low! The solution is as follows:

 

How to update?The best way is to use anacon to update all of them uniformly without making mistakes. The method is as follows:

Step 1: first, start cmd.exe as an administrator
Step 2: to upgrade CONDA (you need to upgrade CONDA before upgrading Anaconda) is: CONDA update CONDA
Step 3: to upgrade anconda is: CONDA update Anaconda
Step 4: to upgrade Spyder is: CONDA update Spyder

Step 3: to upgrade Anaconda

Then restart pychar and run the code:

import numpy as np

arr = np.random.randint(1, 9, size=9)
print(arr)

Finally it worked! And when you type pycharm there are also function parameters and other prompts, by the way in the windows environment: ctrl+p will come up with parameter prompts.
Run the results.

[1 2 2 4 7 1 8 7 4]

[Solved] Pycharm Warning: This inspection detects instance attribute definition outside __init__ method

Example code:

class MiNiCarStore(CarStore):

    def createCar(self, typeName):
        self.carFactory = CarFactory()  # An underline prompt will appear: This inspection detects instance attribute definition outside __init__ method
        return self.carFactory.createCar(typeName)

The reason is: according to the principle of SRP (single scalability principle, SRP), this class should assume a certain interface logic, so it should no longer assume the responsibility of “initialization”. The initialization work should be completed in a separate class, which can make the code more testable (that is, better write unit tests)

It can be rewritten as follows:

class MiNiCarStore(CarStore):
	
	def __init__(self):
        self.carFactory = None
        
    def createCar(self, typeName):
        self.carFactory = CarFactory()
        return self.carFactory.createCar(typeName)

You can also click Settings – > editor -> inspections -> Python uncheck prompt

 

Solve the problem of attributeerror: module ‘Matplotlib’ has no attribute ‘verb

Geeks, please accept the hero post of 2021 Microsoft x Intel hacking contest>>>

Recently, when using Matplotlib in the process of using pychar in Linux, we can’t draw a picture, and always prompt an error

/usr/bin/python3.5 /home/leo/PycharmProjects/untitled1/Euler.py
Traceback (most recent call last):
  File "/home/leo/PycharmProjects/untitled1/Euler.py", line 5, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
    [backend_name], 0)
  File "/home/leo/pycharm-2017.3.4/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 17, in <module>
    verbose = matplotlib.verbose
AttributeError: module 'matplotlib' has no attribute 'verbose'

According to the file prompted with error, enter the file prompted in the last line and enter the file

from matplotlib.backend_bases import FigureManagerBase, ShowBase
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure

HOST = 'localhost'
PORT = os.getenv("PYCHARM_MATPLOTLIB_PORT")
PORT = int(PORT) if PORT is not None else None
PORT = PORT if PORT != -1 else None
index = int(os.getenv("PYCHARM_MATPLOTLIB_INDEX", 0))

rcParams = matplotlib.rcParams
verbose = matplotlib.verbose

The error is in verb = Matplotlib. Verb

Because in Python 3, it’s verb in Matplotlib

!!!!!!!!!! Note: capital V

Change it over and run it. The problem is solved perfectly

After installing BS4 in Python, pychar still reports module not found error: no module named ‘BS4’

Geeks, please accept the hero post of 2021 Microsoft x Intel hacking contest>>>

When learning web crawling, there is a problem in the first step

Executing sample code

from urllib.request import urlopen

from bs4 import BeautifulSoup

html = urlopen(” http://www.pythonscraping.com/exercises/exercise1.html “)

bsObj = BeautifulSoup(html, “html.parser”)

print(bsObj.h1)

Implementation results

Traceback (most recent call last):

File “D:/install/Python_ web Scraping/chapter1/BeautifulSoup.py”, line 2, in < module> from bs4 import BeautifulSoup ModuleNotFoundError: No module named ‘bs4’

Note: beautiful soup has been successfully installed in the terminal, but it still appears in idle

Solutions

1. In pychar Python file options, select the settings option and click in

2. Then select project interpreter

3. Double click pip

4. Search BS4, click Install, and the message of successful installation will be prompted