Python error - cannot import my requirements - python

I'm trying to use several imports inside of a Python file. However, none of them seem to be imported correctly. I'm using a miniconda environment, and I've tried running Python versions 2.7 and 3.8 .
The following lines cause failures as soon as I run my app. If I comment-out the first couple of lines, I just get the same error about openpyxl instead of dotenv.
from dotenv import load_dotenv
load_dotenv()
from openpyxl import Workbook
I have both of them listed in a requirements.txt file, and I'm able to run "pip install -r requirements.txt" without issues, but I still get errors when I try to run "python -m src.main".
The errors just look like this (below). Any ideas on what else I can try?
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/path/a/b/src/main.py", line 2, in <module>
from dotenv import load_dotenv
ImportError: No module named dotenv
... or this:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/path/a/b/src/main.py", line 5, in <module>
from openpyxl import Workbook
ImportError: No module named openpyxl
EDIT:
Here's some additional information. I'm thinking these paths got corrupted along the way. Can someone tell me the expected output of these commands, inside of a miniconda environment?
(MyEnv2) MyMacbook:my-rep my-user$ which python
/usr/bin/python
(MyEnv2) MyMacbook:my-rep my-user$ which pip
/Users/my-user/opt/miniconda3/envs/MyEnv2/bin/pip
(MyEnv2) MyMacbook:my-rep my-user$ which pip3
/usr/bin/pip3
Note - none of those paths match this one from my errors:
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
Also, here is the output from echo $PATH:
/Users/my-user/.nvm/versions/node/v8.14.1/bin
:/usr/local/bin
:/usr/bin
:/bin
:/usr/sbin
:/sbin
:/Library/Python/2.7/bin
:/Library/apache-maven-3.6.3/bin
:/usr/local/share/dotnet
:~/.dotnet/tools
:/Users/my-user/opt/miniconda3/envs/MyEnv2/bin
:/Users/my-user/opt/miniconda3/condabin
:/Users/my-user/google-cloud-sdk/bin
:/Users/my-user/.nvm/versions/node/v8.14.1/bin
:/Users/my-user/.nvm/versions/node/v8.14.1/bin

I resolved this by removing the following from: /etc/paths:
/Library/Python/2.7/bin
Once I had that line removed, I was able to see (nearly) identical output for which pip, which pip3, and which python. All 3 output the following (with the appropriate app at the end of the path):
/Users/my-user/opt/miniconda3/envs/MyEnv2/bin/python

Related

Trouble downloading Python modules

I was making a Tetris game and at the top of the Python code, I put in import pygame, since that was the thing I was thinking of doing and the thing I thought I could only do. My game was complete, the terminal was open, and I ran the code. The terminal gives an error:
Traceback (most recent call last):
File "[removed for safety]", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
So I tried pip installing it with the command pip install pygame, but it just returns a similar error:
Traceback (most recent call last):
File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main
"__main__", mod_spec)
File "C:\Python34\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python34\Scripts\pip.exe\__main__.py", line 5, in <module>
File "C:\Python34\lib\site-packages\pip\__init__.py", line 1, in <module>
from typing import List, Optional
ImportError: No module named 'typing'
I was using
VSCode, with Python version 3.10.5, 64-bit.
First of all you can uninstall python 3.4 from your system, with this link then you can reinstall python from from the python.org website. Then hopefully pip install pygame would work. Or, if pygame is installed run pip uninstall pygame then pip install pygame
File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main
Python34 is mentioned in the error code, which indicates that the terminal runtime retrieves python3.4 instead of 3.10 you mentioned.
This indicates that there are multiple versions of Python in your environment.
You can manually select the Python interpreter by using shortcut "ctrl+shift+P" and typing "python: select interpreter" or click this icon to change the interpreter:
After choosing the correct interpreter, you can use command pip install pygame to install python package at the correct python-lib.

Running docker from python's venv

I have created a python module. Works on ubuntu/xenial with python3.5. I built it and uploaded to python repository. Then I created virtual environment, installed this module and tried to run. As a result I got:
(hdfnc_venv) svc#PL1LXD-529389:~/$ python3 -m my_package.my_module -h
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/my_package/my_module.py", line 4, in <module>
import docker
File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/docker/__init__.py", line 2, in <module>
from .api import APIClient
File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/docker/api/__init__.py", line 2, in <module>
from .client import APIClient
File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/docker/api/client.py", line 222
f'Error while fetching server API version: {e}'
^
SyntaxError: invalid syntax
So apparently there are multiple issues here. One is this invalid syntax which I suppose is because python3.5 does not recognize f-string syntax introduced in python3.6. Wasn't there an f-string, an exception would be raised anyway.
The bigger problem is why it there is error with just importing docker. Docker is properly installed on this machine, so is the module docker (5.0.3) in my venv. The module works when I run it directly in the system from the source code, not from my venv installation. Also when I run it this way, python3.5 works just fine, so invalid_syntax is not a problem anymore - most likely python didn't reach this part of the code, which means it managed to import docker module (also 5.0.3) without any problem.

Flask on PyCharm - ImportError: attempted relative import with no known parent package

I am trying to create and run a simple flask server on PyCharm community 2019.3
I work with python 3.7.
I used venv to create the project.
Flask was installed like this:
(venv) pip install flask
from the pycharm terminal on c:\Users\Damo\PycharmProjects\myflaskproj\venv\Lib\site-packages\flask\
In the Edit configuration I created a new python run configuration.
I used the above path as my script path and set "run" as the parameters.
I added FLASK_APP=myflaskproj.py to the environment parameters.
Finally I set C:\Users\Damo\PycharmProjects\myflaskproj as my working directory.
When I run the project, I get :
C:\Users\Damo\PycharmProjects\myflaskproj\venv\Scripts\python.exe C:/Users/Damo/PycharmProjects/myflaskproj/venv/Lib/site-packages/flask run
Traceback (most recent call last):
File "c:\Users\Damo\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\Users\Damo\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:/Users/Damo/PycharmProjects/myflaskproj/venv/Lib/site-packages/flask\__main__.py", line 13, in <module>
from .cli import main
ImportError: attempted relative import with no known parent package
Where do I go wrong?
Thanks
This is the complete answer...
Running flask server configuration for PyCharm

Problems with initialize Cuckoo

I tryed to install last Cuckoo version on my physical machine Windows 10. And I use python 2.7.16. I have Cuckoo installed and its dependencies too, but I cann't initialize it:
File "c:\python27\lib\runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "c:\python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\Scripts\cuckoo.exe\__main__.py", line 5, in <module>
File "c:\python27\lib\site-packages\cuckoo\__init__.py", line 6, in <module>
from cuckoo import (
File "c:\python27\lib\site-packages\cuckoo\processing\__init__.py", line 10, in <module>
__file__, "cuckoo.processing", globals(), Processing
File "c:\python27\lib\site-packages\cuckoo\core\plugins.py", line 46, in enumerate_plugins
"review its contents and/or validity!" % (module_name, e)
cuckoo.common.exceptions.CuckooOperationalError: Unable to load the Cuckoo plugin at cuckoo.processing.procmemory: cannot import name XOR. Please review its contents and/or validity!
I finally understood what the problem it was. When there were problems with dependencies, I tryed to install dependencies using command pip install ... but it was mistake. For successful installation of the program you should install Microsoft Visual C++ Compiler for Python 2.7

pyinstaller, import error: no module named pywintypes

I want to create exe from a py script using pyinstaller. I wrote the following command in cmd "pyinstaller --onefile --windowed filename.py", but there is an error showed by the cmd. How do I solve this?
Traceback (most recent call last):
File "c:\python27\lib\runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "c:\python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\Scripts\pyinstaller.exe\__main__.py", line 5, in <module>
File "c:\python27\lib\site-packages\PyInstaller\__main__.py", line 21, in <module>
import PyInstaller.building.build_main
File "c:\python27\lib\site-packages\PyInstaller\building\build_main.py", line 32, in <module>
from ..depend import bindepend
File "c:\python27\lib\site-packages\PyInstaller\depend\bindepend.py", line 38, in <module>
from ..utils.win32.winmanifest import RT_MANIFEST
File "c:\python27\lib\site-packages\PyInstaller\utils\win32\winmanifest.py", line 97, in <module>
from PyInstaller.utils.win32 import winresource
File "c:\python27\lib\site-packages\PyInstaller\utils\win32\winresource.py", line 20, in <module>
import pywintypes
ImportError: No module named pywintypes
Edit: I have tried to "pip install pypiwin32, but still got the same ImportError
You should try to work in a virtualenv if you are on Windows. I recommand Anaconda. You will be able to easily install most of python packages.
Once Anaconda is installed, just run conda install -c conda-forge pyinstaller=3.2.1
and you should be fine.
Hope this helps.
In some case you have to manually mention modules that need to be packes through the keyword "--hidden-import
So in your case:
pyinstaller --onefile --hidden-import=pywintypes --windowed filename.py

Categories