AWS-CDK cannot import core - python

I have recently been asked to learn the AWS-CDK module in Python to handle deployments and following the examples on in the AWS-CDK documentation I am getting an error that indicates it cannot import 'core'
I have created a virtual environment that is using Python 3.6.6 and am running CDK version 1.16.2 (build 5893301) all running on a Windows 10 64-bit machine in VSCode
from aws_cdk import core
I would expect this to just load the constructor but it just returns the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'core'
I have searched and searched and I cannot seem to find what I am doing wrong or anything that contradicts the documentation on the CDK site. Any insight would be appreciated.

This worked for me. Might be related to conda / venv interactions.
pip3 install -r requirements.txt --user

Is aws_cdk.core installed in your environment?
I have this:
pip list|grep core
aws-cdk.core 1.68.0

I noticed that the cdk.json "app" value can also generate this error.
Example of the cdk.json file which used to generate the error:
{
"app": "python3 app.py",
"context": {
...
}
}
Changing the app value from python3 to python resolved the problem for me. Like this:
{
"app": "python app.py",
"context": {
...
}
}
I am not entirely sure why this happens as I am using python 3.8 and I can see a python.exe in the python path/environment variables. If I deactivate the virtual env, and I have cdk installed globally, this problem goes away regardless of whether I use python or python3 for the app value. Therefore, I suspect it's got to do something with environment paths.

aws-cdk-lib==2.34.0
constructs>=10.0.0,<11.0.0
aws-cdk.aws-kinesisfirehose-alpha
aws_cdk.aws-kinesisfirehose-destinations-alpha
aws_cdk.core
pip list|grep core

If you are unable to load this module that means it is not installed. It is possible that it is not installed in the python virtual environment you are using. When I search aws_cdk module in my working directory using find . -name aws_cdk , I found that I had two virtual environments setup. One was under venv and other one was .venv. And the module installed were installed in one of them but not in the other environment. Switching the environment and installing the module fixed the error.

Related

Numpy module not found when working with Azure Functions in VS Code and virtualenv

I'm new to working with azure functions and tried to work out a small example locally, using VS Code with the Azure Functions extension.
Example:
# First party libraries
import logging
# Third party libraries
import numpy as np
from azure.functions import HttpResponse, HttpRequest
def main(req: HttpRequest) -> HttpResponse:
seed = req.params.get('seed')
if not seed:
try:
body = req.get_json()
except ValueError:
pass
else:
seed = body.get('seed')
if seed:
np.random.seed(seed=int(seed))
r_int = np.random.randint(0, 100)
logging.info(r_int)
return HttpResponse(
"Random Number: " f"{str(r_int)}", status_code=200
)
else:
return HttpResponse(
"Insert seed to generate a number",
status_code=200
)
When numpy is installed globally this code works fine. If I install it only in the virtual environment, however, I get the following error:
*Worker failed to function id 1739ddcd-d6ad-421d-9470-327681ca1e69.
[15-Jul-20 1:31:39 PM] Result: Failure
Exception: ModuleNotFoundError: No module named 'numpy'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound*
I checked multiple times that numpy is installed in the virtual environment, and the environment is also specified in the .vscode/settings.json file.
pip freeze of the virtualenv "worker_venv":
$ pip freeze
azure-functions==1.3.0
flake8==3.8.3
importlib-metadata==1.7.0
mccabe==0.6.1
numpy==1.19.0
pycodestyle==2.6.0
pyflakes==2.2.0
zipp==3.1.0
.vscode/settings.json file:
{
"azureFunctions.deploySubpath": ".",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.pythonVenv": "worker_venv",
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~2",
"debug.internalConsoleOptions": "neverOpen"
}
I tried to find something in the documentation, but found nothing specific regarding the virtual environment. I don't know if I'm missing something?
EDIT: I'm on a Windows 10 machine btw
EDIT: I included the folder structure of my project in the image below
EDIT: Added the content of the virtual environment Lib folder in the image below
EDIT: Added a screenshot of the terminal using the pip install numpy command below
EDIT: Created a new project with a new virtual env and reinstalled numpy, screenshot below, problem still persists.
EDIT: Added the launch.json code below
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
SOLVED
So the problem was neither with python, nor with VS Code. The problem was that the execution policy on my machine (new laptop) was set to restricted and therefore the .venv\Scripts\Activate.ps1 script could not be run.
To resolve this problem, just open powershell with admin rights and and run set-executionpolicy remotesigned. Restart VS Code and all should work fine
I didn't saw the error, due to the many logging in the terminal that happens
when you start azure. I'll mark the answer of #HuryShen as correct, because the comments got me to the solution. Thank all of you guys
For this problem, I'm not clear if you met the error when run it locally or on azure cloud. So provide both suggestions for these two situation.
1. If the error shows when you run the function on azure, you may not have installed the modules success. When deploy the function from local to azure, you need to add the module to requirements.txt(as Anatoli mentioned in comment). You can generate the requirements.txt automatically by the command below:
pip freeze > requirements.txt
After that, we can find the numpy==1.19.0 exist in requirements.txt.
Now, deploy the function from local to azure by the command below, it will install the modules success on azure and work fine on azure.
func azure functionapp publish <your function app name> --build remote
2. If the error shows when you run the function locally. Since you provided the modules installed in worker_venv, it seems you have installed numpy module success. I also test it in my side locally, install numpy and it works fine. So I think you can check if your virtual environment(worker_venv) exist in the correct location. Below is my function structure in local VS code, please check if your virtual environment locates in the same location with mine.
-----Update------
Run the command to to set execution policy and then activate the virtual environment:
set-executionpolicy remotesigned
.venv\Scripts\Activate.ps1
I could solve my issue uninstalling python3 (see here for a guide https://stackoverflow.com/a/60318668/11986067).
After starting the app functions via F5 or func start, the following output was shown:
This version was incorrect. I have chosen python 3.7.0 when creating the project in the Azure extension. After deleting this python3 version, the correct version was shown and the Import issue was solved:

i tried to install pyinstaller with pip install pyinstaller and got this: [duplicate]

I just installed Python 3.6.1 for MacOS X
When I attempt to run the Console(or run anything with Python3), this error is thrown:
AttributeError: module 'enum' has no attribute 'IntFlag'
$ /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
Failed to import the site module
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 544, in <module>
main()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 282, in addusersitepackages
user_site = getusersitepackages()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 258, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 248, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 601, in get_config_var
return get_config_vars().get(name)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 580, in get_config_vars
import _osx_support
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_osx_support.py", line 4, in <module>
import re
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 142, in <module>
class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'
The class IntFlag exists within enum.py. So, why is the AttributeError being thrown?
It's because your enum is not the standard library enum module. You probably have the package enum34 installed.
One way check if this is the case is to inspect the property enum.__file__
import enum
print(enum.__file__)
# standard library location should be something like
# /usr/local/lib/python3.6/enum.py
Since python 3.6 the enum34 library is no longer compatible with the standard library. The library is also unnecessary, so you can simply uninstall it.
pip uninstall -y enum34
If you need the code to run on python versions both <=3.4 and >3.4, you can try having enum-compat as a requirement. It only installs enum34 for older versions of python without the standard library enum.
Not sure whether you still have this issue. I had a similar issue and I was able to resolve it simply by unsetting PYTHONPATH
$ unset PYTHONPATH
For me this error occured after installing of gcloud component app-engine-python in order to integrate into pycharm. Uninstalling the module helped, even if pycharm is now not uploading to app-engine.
If anyone coming here because of getting this error while running a google app engine Python 3.7 standard environment project in PyCharm then all you need to do is
Make sure the configuration to run is for Flask, not Google App Engine configuration.
Then disable Google App Engine support under Preferences >> Languages & Framework >> Google App Engine
The reason being as per this link
The overall goal is that your app should be fully portable and run in any standard Python environment. You write a standard Python app, not an App Engine Python app. As part of this shift, you are no longer required to use proprietary App Engine APIs and services for your app's core functionality. At this time, App Engine APIs are not available in the Python 3.7 runtime.
I guess when we create a python 3.7 project in PyCharm as a Google app engine project it still tries to do the same way it does for a python2.7 app
I got this issue while installing transformers library from HuggingFace. It was due to the fact package enum34 was installed in my environment which was overriding built-in enum in Python. This package was probably installed by something as for forward compatibility which is no longer needed with Python 3.6+. So the solution is simply,
pip uninstall -y enum34
DISCLAIMER: Please, #juanpa.arrivillaga, if you see this answer, feel free to write your own and I will remove this post.
#juanpa.arrivillaga had mentioned above:
Is there a file name enum.py in your working directory, by any chance?
This was the issue I encountered. I was not aware of the enum module on python at the time and had named my test file enum.py.
Since the file name is the module name, there was a conflict. More info on modules here: https://docs.python.org/2/tutorial/modules.html
Installing version 1.1.8 of enum34 worked for me.
I was able to fix this by adding enum34 = "==1.1.8" to pyproject.toml.
Apparently enum34 had a feature in v1.1.8 that avoided this error, but
this regressed in v1.1.9+. This is just a workaround though. The
better solution would be for packages to use environment markers so
you don't have to install enum34 at all unless needed.
Source: https://github.com/python-poetry/poetry/issues/1122
Disabling "Google App Engine Support" in pycharm preferences fixed this issue for me.
Håken Lid's answer helped solved my problem (thanks!) , in my case present in Python3.7 running Flask in a Docker container (FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.7).
In my case, enum34 was being installed by another library (pip install smartsheet-python-sdk).
For those coming with a similar Docker container problem, here is my final Dockerfile (stripped to the relevant lines):
FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.7
...
RUN pip install -r requirements.txt
RUN pip uninstall -y enum34
...
In case you have to keep PYTHONPATH for both python2 and python3, you can write alias statements to set the proper PYTHONPATH in your bash_profile:
Hardcode your PYTHONPATH2, and PYTHONPATH3 variables in your ~/.bash_profile, and add the following aliases at the end of it:
alias python='export PYTHONPATH=${PYTHONPATH2};python'
alias python3='export PYTHONPATH=${PYTHONPATH3};python3'
My python (refers to python2) as I use python2 more often.
I have Python 2 and Python 3 installed on my computer. For some strange reason I have in the sys.path of Python 3 also a path to the sitepackage library directory of Python2 when the re module is called. If I run Python 3 and import enum and print(enum.__file__) the system does not show this Python 2 path to site-packages. So a very rough and dirty hack is, to directly modify the module in which enum is imported (follow the traceback paths) and insert the following code just before importing enum:
import sys
for i, p in enumerate(sys.path):
if "python27" in p.lower() or "python2.7" in p.lower(): sys.path.pop(i)
import enum
That solved my problem.
When ever I got this problem:
AttributeError: module 'enum' has no attribute 'IntFlag'
simply first i run the command:
unset PYTHONPATH
and then run my desired command then got success in that.
I did by using pip install <required-library> --ignore-installed enum34
Once your required library is installed, look for warnings during the build.
I got an Error like this:
Using legacy setup.py install for future, since package 'wheel' is not installed
ERROR: pyejabberd 0.2.11 has requirement enum34==1.1.2, but you'll have enum34 1.1.10 which is incompatible.
To fix this issue now run the command: pip freeze | grep enum34. This will give you the version of the installed enum34. Now uninstall it by pip uninstall enum34 and reinstall the required version as pip install "enum34==1.1.20"
If you having this issue when running tests in PyCharm, make sure the second box is unchecked in the configurations.
I had this problem in ubuntu20.04 in jupyterlab in my virtual env kernel with python3.8 and tensorflow 2.2.0. Error message was
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel_launcher.py", line 15, in <module>
from ipykernel import kernelapp as app
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel/__init__.py", line 2, in <module>
from .connect import *
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel/connect.py", line 13, in <module>
from IPython.core.profiledir import ProfileDir
File "/home/hu-mka/.local/lib/python2.7/site-packages/IPython/__init__.py", line 48, in <module>
from .core.application import Application
File "/home/hu-mka/.local/lib/python2.7/site-packages/IPython/core/application.py", line 23, in <module>
from traitlets.config.application import Application, catch_config_error
File "/home/hu-mka/.local/lib/python2.7/site-packages/traitlets/__init__.py", line 1, in <module>
from .traitlets import *
File "/home/hu-mka/.local/lib/python2.7/site-packages/traitlets/traitlets.py", line 49, in <module>
import enum
ImportError: No module named enum
problem was that in symbolic link in /usr/bin/python was pointing to python2. Solution:
cd /usr/bin/
sudo ln -sf python3 python
Hopefully Python 2 usage will drop off completely soon.
Even I had this issue while running python -m grpc_tools.protoc --version
Had to set the PYTHONPATH till site-packages and shutdown all the command prompt windows and it worked. Hope it helps for gRPC users.
Unfortunately none of the suggestions helped me, but after some more googling this
pip install aenum
solved it for me
I faced the same issue, but I couldn't change the environment the script is run inside. So the fixes here were not applicable for me.
What I did instead was
import os
import sys
os.environ["PYTHONPATH"] = os.pathsep.join(
(p for p in os.environ["PYTHONPATH"].split(os.pathsep) if "python2" not in p)
)
sys.path = [p for p in sys.path if "python2" not in p]
# Now that the paths are clean, we can import re
import re
2022 UPDATE
If you are using a modern version of Python (in my case 3.9.x), you don't need "enum34" at all, as this library is described as "Python 3.4 Enum backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and 2.4"
So in my case, the solution was to get rid of it entirely:
pip uninstall enum34
Now Pycharm runs my app perfectly.
My problem was that I marked a directory of mine as a Test Sources Root in pycharm which caused the python ast package to look for enum within <my_test_src_root_dir>/__init__.py (which was empty). I found this by clicking on the line that threw and then controlling (mac CMD-B) into the enum package and seeing that it went to the __init__.py file of the marked directory.
The file that threw:
"/opt/homebrew/Cellar/python#3.10/3.10.2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py"
The line that threw:
from enum import IntEnum, auto
The error:
ImportError: cannot import name 'IntEnum' from 'enum' (.../path/to/_init_.py)
If anyone is having this problem when trying to run Jupyter kernel from a virtualenv, just add correct PYTHONPATH to kernel.json of your virtualenv kernel (Python 3 in example):
{
"argv": [
"/usr/local/Cellar/python/3.6.5/bin/python3.6",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3 (TensorFlow)",
"language": "python",
"env": {
"PYTHONPATH": "/Users/dimitrijer/git/mlai/.venv/lib/python3.6:/Users/dimitrijer/git/mlai/.venv/lib/python3.6/lib-dynload:/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6:/Users/dimitrijer/git/mlai/.venv/lib/python3.6/site-packages"
}
}
ensure there is no file or folder named enum in your codebase

Cannot install custom python module

I am having trouble installing a custom python module I have written.
Here are my steps so far:
Navigate to the module directory C:\Users\myname\repos\mymodulename where the setup.py file is in the anaconda prompt. Type: python setup.py install
The command prompt returns (plus some other things)
Extracting mymodulename-0.1-py3.7.egg to c:\users\myname\appdata\local\continuum\anaconda3\lib\site-packages
mymodulename 0.1 is already the active version in easy-install.pth
Installing myclass-script.py script to C:\Users\myname\AppData\Local\Continuum\anaconda3\Scripts
Installing myclass.exe script to C:\Users\myname\AppData\Local\Continuum\anaconda3\Scripts
Installed c:\users\myname\appdata\local\continuum\anaconda3\lib\site-packages\mymodulename-0.1-py3.7.egg
Processing dependencies for mymodulename==0.1
...
Using c:\users\myname\appdata\local\continuum\anaconda3\lib\site-packages
Finished processing dependencies for mymodulename==0.1
To me that looks like it has installed. Opening up the console and trying to import:
>>> import mymodulename.myclassas ce
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mymodulename'
>>>
It appears in hasn't. Checking the list of modules in anaconda with help('modules') confirms that it has not been imported.
I thought that perhaps i had installed it to the wrong environment:
(base) C:\Users\myname>conda env list
# conda environments:
#
base * C:\Users\myname\AppData\Local\Continuum\anaconda3
py2 C:\Users\myname\AppData\Local\Continuum\anaconda3\envs\py2
Only a python 2 environment which mymodule wouldn't be compatible with.
Does anyone have any suggestions about what I can try to resolve this? Happy to elaborate on any of the points
Thanks in advance.
EDIT: Some more information that may be relevant.
This package was initially installed in site-packages. I have reinstalled there and the package works. The reason I moved the package is because I am aware it is bad practice to store custom packages there.

Instaling airflow on python virtual environment [duplicate]

I just installed Python 3.6.1 for MacOS X
When I attempt to run the Console(or run anything with Python3), this error is thrown:
AttributeError: module 'enum' has no attribute 'IntFlag'
$ /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
Failed to import the site module
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 544, in <module>
main()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 282, in addusersitepackages
user_site = getusersitepackages()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 258, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 248, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 601, in get_config_var
return get_config_vars().get(name)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 580, in get_config_vars
import _osx_support
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_osx_support.py", line 4, in <module>
import re
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 142, in <module>
class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'
The class IntFlag exists within enum.py. So, why is the AttributeError being thrown?
It's because your enum is not the standard library enum module. You probably have the package enum34 installed.
One way check if this is the case is to inspect the property enum.__file__
import enum
print(enum.__file__)
# standard library location should be something like
# /usr/local/lib/python3.6/enum.py
Since python 3.6 the enum34 library is no longer compatible with the standard library. The library is also unnecessary, so you can simply uninstall it.
pip uninstall -y enum34
If you need the code to run on python versions both <=3.4 and >3.4, you can try having enum-compat as a requirement. It only installs enum34 for older versions of python without the standard library enum.
Not sure whether you still have this issue. I had a similar issue and I was able to resolve it simply by unsetting PYTHONPATH
$ unset PYTHONPATH
For me this error occured after installing of gcloud component app-engine-python in order to integrate into pycharm. Uninstalling the module helped, even if pycharm is now not uploading to app-engine.
If anyone coming here because of getting this error while running a google app engine Python 3.7 standard environment project in PyCharm then all you need to do is
Make sure the configuration to run is for Flask, not Google App Engine configuration.
Then disable Google App Engine support under Preferences >> Languages & Framework >> Google App Engine
The reason being as per this link
The overall goal is that your app should be fully portable and run in any standard Python environment. You write a standard Python app, not an App Engine Python app. As part of this shift, you are no longer required to use proprietary App Engine APIs and services for your app's core functionality. At this time, App Engine APIs are not available in the Python 3.7 runtime.
I guess when we create a python 3.7 project in PyCharm as a Google app engine project it still tries to do the same way it does for a python2.7 app
I got this issue while installing transformers library from HuggingFace. It was due to the fact package enum34 was installed in my environment which was overriding built-in enum in Python. This package was probably installed by something as for forward compatibility which is no longer needed with Python 3.6+. So the solution is simply,
pip uninstall -y enum34
DISCLAIMER: Please, #juanpa.arrivillaga, if you see this answer, feel free to write your own and I will remove this post.
#juanpa.arrivillaga had mentioned above:
Is there a file name enum.py in your working directory, by any chance?
This was the issue I encountered. I was not aware of the enum module on python at the time and had named my test file enum.py.
Since the file name is the module name, there was a conflict. More info on modules here: https://docs.python.org/2/tutorial/modules.html
Installing version 1.1.8 of enum34 worked for me.
I was able to fix this by adding enum34 = "==1.1.8" to pyproject.toml.
Apparently enum34 had a feature in v1.1.8 that avoided this error, but
this regressed in v1.1.9+. This is just a workaround though. The
better solution would be for packages to use environment markers so
you don't have to install enum34 at all unless needed.
Source: https://github.com/python-poetry/poetry/issues/1122
Disabling "Google App Engine Support" in pycharm preferences fixed this issue for me.
Håken Lid's answer helped solved my problem (thanks!) , in my case present in Python3.7 running Flask in a Docker container (FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.7).
In my case, enum34 was being installed by another library (pip install smartsheet-python-sdk).
For those coming with a similar Docker container problem, here is my final Dockerfile (stripped to the relevant lines):
FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.7
...
RUN pip install -r requirements.txt
RUN pip uninstall -y enum34
...
In case you have to keep PYTHONPATH for both python2 and python3, you can write alias statements to set the proper PYTHONPATH in your bash_profile:
Hardcode your PYTHONPATH2, and PYTHONPATH3 variables in your ~/.bash_profile, and add the following aliases at the end of it:
alias python='export PYTHONPATH=${PYTHONPATH2};python'
alias python3='export PYTHONPATH=${PYTHONPATH3};python3'
My python (refers to python2) as I use python2 more often.
I have Python 2 and Python 3 installed on my computer. For some strange reason I have in the sys.path of Python 3 also a path to the sitepackage library directory of Python2 when the re module is called. If I run Python 3 and import enum and print(enum.__file__) the system does not show this Python 2 path to site-packages. So a very rough and dirty hack is, to directly modify the module in which enum is imported (follow the traceback paths) and insert the following code just before importing enum:
import sys
for i, p in enumerate(sys.path):
if "python27" in p.lower() or "python2.7" in p.lower(): sys.path.pop(i)
import enum
That solved my problem.
When ever I got this problem:
AttributeError: module 'enum' has no attribute 'IntFlag'
simply first i run the command:
unset PYTHONPATH
and then run my desired command then got success in that.
I did by using pip install <required-library> --ignore-installed enum34
Once your required library is installed, look for warnings during the build.
I got an Error like this:
Using legacy setup.py install for future, since package 'wheel' is not installed
ERROR: pyejabberd 0.2.11 has requirement enum34==1.1.2, but you'll have enum34 1.1.10 which is incompatible.
To fix this issue now run the command: pip freeze | grep enum34. This will give you the version of the installed enum34. Now uninstall it by pip uninstall enum34 and reinstall the required version as pip install "enum34==1.1.20"
If you having this issue when running tests in PyCharm, make sure the second box is unchecked in the configurations.
I had this problem in ubuntu20.04 in jupyterlab in my virtual env kernel with python3.8 and tensorflow 2.2.0. Error message was
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel_launcher.py", line 15, in <module>
from ipykernel import kernelapp as app
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel/__init__.py", line 2, in <module>
from .connect import *
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel/connect.py", line 13, in <module>
from IPython.core.profiledir import ProfileDir
File "/home/hu-mka/.local/lib/python2.7/site-packages/IPython/__init__.py", line 48, in <module>
from .core.application import Application
File "/home/hu-mka/.local/lib/python2.7/site-packages/IPython/core/application.py", line 23, in <module>
from traitlets.config.application import Application, catch_config_error
File "/home/hu-mka/.local/lib/python2.7/site-packages/traitlets/__init__.py", line 1, in <module>
from .traitlets import *
File "/home/hu-mka/.local/lib/python2.7/site-packages/traitlets/traitlets.py", line 49, in <module>
import enum
ImportError: No module named enum
problem was that in symbolic link in /usr/bin/python was pointing to python2. Solution:
cd /usr/bin/
sudo ln -sf python3 python
Hopefully Python 2 usage will drop off completely soon.
Even I had this issue while running python -m grpc_tools.protoc --version
Had to set the PYTHONPATH till site-packages and shutdown all the command prompt windows and it worked. Hope it helps for gRPC users.
Unfortunately none of the suggestions helped me, but after some more googling this
pip install aenum
solved it for me
I faced the same issue, but I couldn't change the environment the script is run inside. So the fixes here were not applicable for me.
What I did instead was
import os
import sys
os.environ["PYTHONPATH"] = os.pathsep.join(
(p for p in os.environ["PYTHONPATH"].split(os.pathsep) if "python2" not in p)
)
sys.path = [p for p in sys.path if "python2" not in p]
# Now that the paths are clean, we can import re
import re
2022 UPDATE
If you are using a modern version of Python (in my case 3.9.x), you don't need "enum34" at all, as this library is described as "Python 3.4 Enum backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and 2.4"
So in my case, the solution was to get rid of it entirely:
pip uninstall enum34
Now Pycharm runs my app perfectly.
My problem was that I marked a directory of mine as a Test Sources Root in pycharm which caused the python ast package to look for enum within <my_test_src_root_dir>/__init__.py (which was empty). I found this by clicking on the line that threw and then controlling (mac CMD-B) into the enum package and seeing that it went to the __init__.py file of the marked directory.
The file that threw:
"/opt/homebrew/Cellar/python#3.10/3.10.2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py"
The line that threw:
from enum import IntEnum, auto
The error:
ImportError: cannot import name 'IntEnum' from 'enum' (.../path/to/_init_.py)
If anyone is having this problem when trying to run Jupyter kernel from a virtualenv, just add correct PYTHONPATH to kernel.json of your virtualenv kernel (Python 3 in example):
{
"argv": [
"/usr/local/Cellar/python/3.6.5/bin/python3.6",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3 (TensorFlow)",
"language": "python",
"env": {
"PYTHONPATH": "/Users/dimitrijer/git/mlai/.venv/lib/python3.6:/Users/dimitrijer/git/mlai/.venv/lib/python3.6/lib-dynload:/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6:/Users/dimitrijer/git/mlai/.venv/lib/python3.6/site-packages"
}
}
ensure there is no file or folder named enum in your codebase

Running VIM from a Python virtulenv using Python plugins

When I start VIM after activating an Python virtulenv, Python plugins can't find their modules, because they are installed in the system, not in the project's virtualenv.
I am using the aw-watcher-vim plugin, which uses a Python library.
When I create a virtualenv and activate it:
virtualenv -p python3 my_env
. my_env/bin/activate
VIM can't find the module at /usr/local/lib/python3.6/dist-packages/aw_core/ anylonger and prints the following errors:
['Traceback (most recent call last):',
' File "~/.dot-file-repo/neovim/plugged/aw-watcher-vim/plugin/vimwatcher.py", line 6 , in <module>',
' from aw_core.log import setup_logging',
'ModuleNotFoundError: No module named ''aw_core''',
'']
(Slightly reformated for readablity.)
It would be nice to use autocompletion using the code from the virtualenv (via Jedi or so), but let the other plugins find their Python modules. Is there a good solution to this use case?
While some comments suggest to import globally instaled pacckages to virtualenv, I'd suggest to set options in /vimrc to use the system python:
set pythondll=/usr/bin/python
set pythonthreedll=/usr/bin/python3
or whatever your system python is.
The advantage is that you virtual envs are untouched and are used according to your needs.

Categories

Resources