Flymake config error while opening python file but correctly setup flake - flake8

When I'm opening a python file in emacs I get the following error message:
Flymake: Configuration error has occured while running (flake8 >..../xyz_flymake.py). Flymake will be switched OFF.
But on the other hand it seems I've configured all the modules needed for elpy to work properly:
Elpy Configuration
Virtualenv........: None
RPC Python........: 3.5.3 (/usr/bin/python3.5)
Interactive Python: /usr/bin/python3.5 (/usr/bin/python3.5)
Emacs.............: 24.5.1
Elpy..............: 1.10.0
Jedi..............: 0.10.2
Rope..............: 0.10.5
Importmagic.......: 0.1.7
Autopep8..........: 0.1.7
Syntax checker....: flake8 (/usr/local/bin/flake8)
You have not activated a virtual env. While Elpy supports this, it is
often a good idea to work inside a virtual env. You can use M-x
pyvenv-activate or M-x pyvenv-workon to activate a virtual env.
The directory ~/.local/bin/ is not in your PATH. As there is no active
virtualenv, installing Python packages locally will place executables
in that directory, so Emacs won't find them. If you are missing some
commands, do add this directory to your PATH.
Options
`Raised' text indicates buttons; type RET or click mouse-1 on a button
to invoke its action. Invoke [+] to expand a group, and [-] to
collapse an expanded group. Invoke the [Group], [Face], and [Option]
buttons below to edit that item in another window.
How can I resolve this issue?

It is unclear what the configuration error is. It's possible that you're running into one of many issues.
You seem to want to use Python 3.5, but there's no clear indicator of what version of Python Flake8 is running on. It could be that elpy is detecting a mismatch and refusing to use Flake8 when it won't provide you any useful information. (Flake8 must be installed on the same version of Python that the code is intended to run on.)
Elpy seems insistent that you use a virtualenv or that you add ~/.local/bin/ to your PATH. I would advise doing both.
You can create a virtualenv by doing virtualenv ~/.elpy-venv and activate it with source ~/.elpy-venv/bin/activate.
You can edit your Shell's configuration file (e.g., ~/.bashrc, ~/.bash_profile, ~/.zshrc, etc.) to do export PATH="$PATH:~/.local/bin".

Related

Numpy and pygame not imported as it should [duplicate]

My Flask App server is running but I have three imports that cannot be resolved.
I have tried:
reinstalling the imports individually
reinstalling requirements.txt
I configured VSCode Workspace with an extra path to my project folder (there is now a .vscode file within the root of my project folder where it was not before)
I have updated my venv path settings in VSCode
Here is my file structure:
- > .vscode
- > client *(React front end)*
- > data
- > server *(Python/Flask back end)*
- > app
- > venv
- config.py
- README.md
- requirements.txt *(this contains the 3 unresolved, along with several that are resolving)*
- .env
- .flaskenv
- .gitignore
- requirements.txt
Unfortunately none of these things have resolved my imports issue and my routes are still not working. Any ideas/suggestions?
Open the Command Palette (Ctrl+Shift+P), then select the Python: Select Interpreter. From the list, select the virtual environment in your project folder that starts with .env.
Run Terminal: Create New Integrated Terminal (Ctrl+Shift+` or from the Command Palette), which creates a terminal and automatically activates the virtual environment by running its activation script.
Install sqlalchemy and mongoengine with command pip install. Once installing them successfully, there will intellisense when you import them and no warnings shown.
Besides, the folder .vscode is to store Workspace settings as well as debugging and task configurations.
If you are using a virtual environment, and even after trying pip installing all the necessary libraries, you have to select the python interpreter that exists in the virtual environment folder.
(Ctrl+Shift+P) then search for "Python: Select Interpreter"
Click "Enter interpreter path" followed by "Find.."
Navigate to your project virtual environment folder
Go into "Scripts" folder and then select "python.exe" as the interpreter.
These steps allow you to select the right python interpreter associated with the project's virtual environment.
Open the Command Palette (Ctrl+Shift+P), then select Python: Clear Cache and Reload window.
Voila, all the import errors vanished.
I specified a path to the python interpreter I'm using within the settings.json file contained in the project repo's .vscode folder.
"python.pythonPath": "path-to-interpreter.python.exe"
Thanks to the following resource! https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b
I ran into this error after an upgrade of my local python version (brew -> manual install), even though the specified interpreter was already /usr/local/bin/python3.
CMD + Shift + P and re-selecting the [same] interpreter fixed the error.
In hindsight, I suspect that a restart of VSCode could have also fixed this. 🤷‍♂️
Perhaps VSCode is using the incorrect Python path for this reason. A base interpreter should be used instead of the vscode interpreter, if necessary.
After verifying my Python interpreter was sourced correctly in VS Code, I simply cleaned my env and reinstalled the project locally and the import errors disappeared. I personally had an odd VS Code/Pylance cache and reinstalling the dependencies and modules fixed it for me.
I was getting the very same error you have and none of the solutions presented here worked me.
I work on a windows machine. I use miniconda to manage my virtual environments. And when I'm coding I launch every program from the command prompt (cmd), including Vscode.
Even tough inside Vscode the python evironment was correctly settled I was getting the very same import error that you mention. The interesting part of it was that I wasn't having any problems to run the code, it was working as usuall. But when coding I wasn't able to see the functions related to the libraries with the import error.
The solution:
Activate the correct conda env for the project before launch Vscode from the command prompt.
Why does this happen?
I BELIVE that this happen because when you don't activate any conda env before launch Vscode. The base conda env is loaded as default. That generates the import conflict. (You can chek this out installing those packagues into the environment and running everything just as you have been doing)

How to simply compile a big python program with huge number of files, for linux, mac and windows [duplicate]

I'm trying to convert a fairly simple Python program to an executable and couldn't find what I was looking for, so I have a few questions (I'm running Python 3.6):
The methods of doing this that I have found so far are as follows
downloading an old version of Python and using pyinstaller/py2exe
setting up a virtual environment in Python 3.6 that will allow me to do 1.
downloading a Python to C++ converter and using that.
Here is what I've tried/what problems I've run into.
I installed pyinstaller before the required download before it (pypi-something) so it did not work. After downloading the prerequisite file, pyinstaller still does not recognize it.
If I'm setting up a virtualenv in Python 2.7, do I actually need to have Python 2.7 installed?
similarly, the only python to C++ converters I see work only up until Python 3.5 - do I need to download and use this version if attempting this?
Steps to convert .py to .exe in Python 3.6
Install Python 3.6.
Install cx_Freeze, (open your command prompt and type pip install cx_Freeze.
Install idna, (open your command prompt and type pip install idna.
Write a .py program named myfirstprog.py.
Create a new python file named setup.py on the current directory of your script.
In the setup.py file, copy the code below and save it.
With shift pressed right click on the same directory, so you are able to open a command prompt window.
In the prompt, type python setup.py build
If your script is error free, then there will be no problem on creating application.
Check the newly created folder build. It has another folder in it. Within that folder you can find your application. Run it. Make yourself happy.
See the original script in my blog.
setup.py:
from cx_Freeze import setup, Executable
base = None
executables = [Executable("myfirstprog.py", base=base)]
packages = ["idna"]
options = {
'build_exe': {
'packages':packages,
},
}
setup(
name = "<any name>",
options = options,
version = "<any number>",
description = '<any description>',
executables = executables
)
EDIT:
be sure that instead of myfirstprog.py you should put your .pyextension file name as created in step 4;
you should include each imported package in your .py into packages list (ex: packages = ["idna", "os","sys"])
any name, any number, any description in setup.py file should not remain the same, you should change it accordingly (ex:name = "<first_ever>", version = "0.11", description = '' )
the imported packages must be installed before you start step 8.
Python 3.6 is supported by PyInstaller.
Open a cmd window in your Python folder (open a command window and use cd or while holding shift, right click it on Windows Explorer and choose 'Open command window here'). Then just enter
pip install pyinstaller
And that's it.
The simplest way to use it is by entering on your command prompt
pyinstaller file_name.py
For more details on how to use it, take a look at this question.
There is an open source project called auto-py-to-exe on GitHub. Actually it also just uses PyInstaller internally but since it is has a simple GUI that controls PyInstaller it may be a comfortable alternative. It can also output a standalone file in contrast to other solutions. They also provide a video showing how to set it up.
GUI:
Output:
Alternatively use pyinstaller directly:
pip install pyinstaller
pyinstaller filename
I can't tell you what's best, but a tool I have used with success in the past was cx_Freeze. They recently updated (on Jan. 7, '17) to version 5.0.1 and it supports Python 3.6.
Here's the pypi
https://pypi.python.org/pypi/cx_Freeze
The documentation shows that there is more than one way to do it, depending on your needs.
http://cx-freeze.readthedocs.io/en/latest/overview.html
I have not tried it out yet, so I'm going to point to a post where the simple way of doing it was discussed. Some things may or may not have changed though.
How do I use cx_freeze?
Now you can convert it by using PyInstaller. It works with even Python 3.
Steps:
Fire up your PC
Open command prompt
Enter command pip install pyinstaller
When it is installed, use the command 'cd' to go to the working directory.
Run command pyinstaller <filename>
I've been using Nuitka and PyInstaller with my package, PySimpleGUI.
Nuitka
There were issues getting tkinter to compile with Nuikta. One of the project contributors developed a script that fixed the problem.
If you're not using tkinter it may "just work" for you. If you are using tkinter say so and I'll try to get the script and instructions published.
PyInstaller
I'm running 3.6 and PyInstaller is working great!
The command I use to create my exe file is:
pyinstaller -wF myfile.py
The -wF will create a single EXE file. Because all of my programs have a GUI and I do not want to command window to show, the -w option will hide the command window.
This is as close to getting what looks like a Winforms program to run that was written in Python.
[Update 20-Jul-2019]
There is PySimpleGUI GUI based solution that uses PyInstaller. It uses PySimpleGUI. It's called pysimplegui-exemaker and can be pip installed.
pip install PySimpleGUI-exemaker
To run it after installing:
python -m pysimplegui-exemaker.pysimplegui-exemaker
The best and easiest way is auto-py-to-exe for sure, and I have given all the steps and red flags below which will take you just 5 mins to get a final .exe file as you don't have to learn anything to use it.
1.) It may not work for python 3.9 on some devices I guess.
2.) While installing python, if you had selected 'add python 3.x to path', open command prompt from start menu and you will have to type pip install auto-py-to-exe to install it. You will have to press enter on command prompt to get the result of the line that you are typing.
3.) Once it is installed, on command prompt itself, you can simply type just auto-py-to-exe to open it. It will open a new window. It may take up to a minute the first time. Also, closing command prompt will close auto-py-to-exe also so don't close it till you have your .exe file ready.
4.) There will be buttons for everything you need to make a .exe file and the screenshot of it is shared below. Also, for the icon, you need a .ico file instead of an image so to convert it, you can use https://convertio.co/
5.) If your script uses external files, you can add them through auto-py-to-exe and in the script, you will have to do some changes to their path. First, you have to write import sys if not written already, second, you have to make a variable for eg, location=getattr(sys,"_MEIPASS",".")+"/", third, the location of example.png would be location+"/example.png" if it is not in any folder.
6.) If it is showing any error, it may probably be because of a module called setuptools not being at the latest version. To upgrade it to the latest version, on command prompt, you will have to write pip install --upgrade setuptools. Also, in the script, writing import setuptools may help. If the version of setuptools is more than 50.0.0 then everything should be fine.
7.) After all these steps, in auto-py-to-exe, when the conversion is complete, the .exe file will be in the folder that you would have chosen (by default, it is 'c:/users/name/output') or it would have been removed by your antivirus if you have one. Every antivirus has different methods to restore a file so just experiment if you don't know.
Here is how the simple GUI of auto-py-to-exe can be used to make a .exe file.
PyOxidizer can be an option here. It's pretty popular with 3.3k stars on Github. Its documentation says
PyOxidizer is capable of producing a single file executable - with a copy of Python and all its dependencies statically linked and all resources (like .pyc files) embedded in the executable. You can copy a single executable file to another machine and run a Python application contained within. It just works.
While I'm not sure if it is capable of producing .exe file PyOxidizer definitely helps with packaging and distribution.

Add libraries to PYTHONPATH In Pycharm (Windows 10)

When setting up a Pycharm development environment, I am trying to follow the following instruction, which is for Linux environment. However, my system is virtual environment created by Anaconda, running on Windows 10. How to add Pythonpath in Pycharm with respect to this kind of environment?
Add Libraries to PYTHONPATH
When running locally, the tensorflow/models/research/ and slim directories should be appended to PYTHONPATH.
This can be done by running the following from tensorflow/models/research/:
# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
Note: This command needs to run from every new terminal you start.
You may follow the following steps:
Get the Python executable path for Anaconda:
Go to window's "programs" and search for "Anaconda Command line" then right click on the properties and copy the path then go to the cmd terminal and type the path and then type python.exe at the end till you logged in the python interpreter. This may vary from an OS to another but this works for me under windows 7.
In Pycharm: Add a new Python interpreter by following exactly the instructions listed in https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html.
Use the Anaconda python executable path you got in step1 for the new interpreter called "Conda" for example. Set the new project to use "Conda" interpreter instead of the normal default Python interpreter.
Apply the new changes and restart Pycharm. Open the Settings/Preferences dialog box, and click Project Interpreter page to make sure that the project is pointing to "Conda" interpreter.
Hope this will help!

Pip freeze doesnt show freshly installed packages with Pycharm

I use Pycharm to create and manage my virtualenvs in my projects.
The problem is that after adding a library with pycharm, when I type the command (pip3 freeze --user), the library does not appear in the command result.
I have to manually type the pip install command each time so that the library is visible.
What manipulation should I do in PyCharm to solve this problem?
For what you are saying, the first thing that comes to mind is that you should use:
pip freeze
And not
pip3 freeze
Because the command mapped to the pip version when you have virtualenv activated is the first. Note that for installing you seem to use pip, and not pip3
Moreover, the --user option afaik is related to the packages installed in the user folder:
--user Install to the Python user install directory for your platform. Typically
~/.local/, or %APPDATA%\Python on
Windows. (See the Python documentation for site.USER_BASE for full details.)
If your packages are installed in the virtualenv folder, I would tell you to not use that option.
Also please make sure you have your virtualenv activated. In linux you can do so by source path/to/virtualenv/activate
Edit
I understand that the reason you are using pip3 is because you may have different versions of Python in your machine. Let me explain you a bit further how it works, because version management is usually a headache for many programmers and it is common to find problems when doing so.
If you install different versions of Python in your linux machine, and you do that as root, then the installation will proceed for the whole system. Usually Python2 installation folder for Linux machines is /usr/bin/python. However, I am uncertain of which directory is used for Python3 installations. You can check that easily by doing whereis python3. You can serach the path to binary of any command by doing whereis command. Note that this works also for whereis python as far as you don't have virtualenv activated.
Aditionally, the link to the binary of a command (or the set of instructions to be exectued, more broadly) is defined in certain folders in Linux, depending on whether you created the command as root or as a user, and possibly also on the distro. This works differently in Windows, that uses the Registry Edit utility to handle command mappings. When you enable your virtualenv, what you are doing is creating an environment that enables mapping system commands such as python to the Python installation in your virtualenv folder.
When you disable the virtualenv, the command points again to the default installation path. Same happens with pip, so incorrect usage of this tool may result in different packages being installed in different locations, and therefore not appearing available for the right Python version at any given circumstance.
In Linux, environment variables are shell dependent, though you can write them out with echo $variable and set them with variable=value (from bash). The search path is simply called PATH and you can get yours by typing echo $PATH.
Source: https://askubuntu.com/a/262073/426469
I encourage you to check other questions in SE network such as this: https://unix.stackexchange.com/a/42211/96121, to learn more about this.
Addendum
Quick tip: it is common to use the pip freeze command as follows:
pip freeze > requirements.txt
It is a standard that leads to understanding that modules in such file are required for the correct functioning of your application. That lets you easily exclude the virtualenv folder when you install the program in another computer, since you can readily know the requriments for a fresh installation. However, you can use the command as you want.

'python3' is not recognized as an internal or external command, operable program or batch file

I am using Python 3.5.2 version on Windows 7 and tried using python3 app.py. I am getting this error message:
'python3' is not recognized as an internal or external command,
operable program or batch file.
Is there any specific cause about why the python3 command is not working?
I also verified that the PATH is added to environment variables.
There is no python3.exe file, that is why it fails.
Try:
py
instead.
py is just a launcher for python.exe. If you have more than one python versions installed on your machine (2.x, 3.x) you can specify what version of python to launch by
py -2 or
py -3
You can also try this:
Go to the path where Python is installed in your system. For me it was something like C:\Users\\Local Settings\Application Data\Programs\Python\Python37
In this folder, you'll find a python executable. Just create a duplicate and rename it to python3. Works every time.
Python3.exe is not defined in windows
Specify the path for required version of python when you need to used it by creating virtual environment for your project
Python 3
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
Python2
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
then activate the environment using
.\environment\Scripts\activate.ps1
Yes, I think for Windows users you need to change all the python3 calls to python to solve your original error. This change will run the Python version set in your current environment. If you need to keep this call as it is (aka python3) because you are working in cross-platform or for any other reason, then a work around is to create a soft link. To create it, go to the folder that contains the Python executable and create the link. For example, this worked in my case in Windows 10 using mklink:
cd C:\Python3
mklink python3.exe python.exe
Use a (soft) symbolic link in Linux:
cd /usr/bin/python3
ln -s python.exe python3.exe
In my case I have a git hook on commit, specified by admin. So it was not very convenient for me to change the script (with python3 calls).
And the simplest workaround was just to copy python.exe to python3.exe.
Now I could launch both python and python3.
If python2 is not installed on your computer, you can try with just python instead of python3
For Python 27
virtualenv -p C:\Python27\python.exe django_concurrent_env
For Pyton36
virtualenv -p C:\Python36\python.exe django_concurrent_env
Enter the command to start up the server in that directory:
py -3.7 -m http.server
I had a related issue after installing windows 11, where python3 in cmd would open the windows store. I was able to sort it out between this post and this other one. In short, I reinstalled python and made sure to add it to PATH. Then, in settings, Apps > Apps & Features > App Execution aliases. Here, all I had to do was make sure that every single python .exe (including idle and pip) were turned off EXCEPT FOR the python3.exe alias. Now it works like a charm.
FWIW:
The root of this issue is not with you or with python. Apparently, Microsoft wanted to make installing python easier for young kiddos getting interested in coding, so they automatically add an executable to PATH. For those of us that already have this executable, it can cause these issues.
Found out instead press the play button the top right and it should work in visual studios:
Do not disable according to first answer
Saying python3 in the command will not work by default.
After figuring out the problem with the modules (Solution): https://youtu.be/paRXeLurjE4
Summary:
To import python modules in case of problem to import modules:
Hover over python in search:
Click open in folder
Hover over and right click
click properties
copy everything in path before \python.exe
close those windows
For cmd (administrator):
cd --path that was copied--
then python -m pip install --upgrade pip
cd Scripts
pip install "Name of Package" such as pip install --module (package) --
Im on win10 and have 3.7, 3.8 and 3.10 installed.
For me "python" launches version 3.10 and does not accept commands (like -3.7), "py" launches newest version but does accept commands, and "python3" does nothing.
Uninstalled 3.10 and "python" now does nothing, and "py" launches 3.8.
I am unable to add a comment, but the mlink option presented in this answer above https://stackoverflow.com/a/55229666/8441472 by #Stanislav preserves cross-platform shebangs at the top of scripts (#!/usr/bin/env python3) and launches the right python.
(Even if you install python from python.org, Windows will direct you to the app marketplace nowadays if you type python3 on the command line. If you type python on the same cli it will launch the python.org version repl. It leads to scripts that generate no output, but more likely silently failed completely. I don't know ho common this is but have experienced it on a couple of different devices)
If you have this at the top of your script to ensure you launch python3 and don't feel like editing everything you own, it is not a bad approach at all... lol.

Resources