I'm new to VS code. I have downloaded a git project which has demo programs that I can run.
The directory structure is as follows:
Projectparser
>demo
>>alg1_demo.py
>>alg2_demo.py
>>alg3_demo.py
>Projectparser
>>alg1
>>>__init__ [ Has just one line : from alg1 import * ]
>>>alg1.py
>>>calg1.c
>>>calg1.h
>>alg2
>>>__init__ [ Has just one line : from alg2 import * ]
>>>alg2.py
>>>calg2.c
>>>calg2.h
>>alg3
>>>__init__ [ Has just one line : from alg3 import * ]
>>>alg3.py
>>>calg3.c
>>>calg3.h
where > indicates sub-directory. Projectparser is the folder from where I open vs code. It has a sub-directory by the same name as well which contains all the algorithms I'm interested in.
When I try running the alg1_demo.py. The below line is causing an error.
sys.append("../")
from Projectparser import alg1 (line 8)
I'm getting the following error:
ImportError: cannot import name 'alg1' from 'Projectparser' (unknown location)
So I added the line : sys.path.append("../Projectparser")
Then I'm getting the following error :
File "/home/suneha/Projectparser/demo/alg1_demo.py", line 8, in <module>
from Projectparser import alg1
File "../Projectparser/Projectparser/alg1/__init__.py", line 1, in <module>
from alg1 import *
ModuleNotFoundError: No module named 'alg1'
But the module is present in the subdirectory. So I added the line :
sys.path.append("../Projectparser/Projectparser/alg1")
Then I'm getting this error :
Traceback (most recent call last):
File "/home/suneha/Projectparser/demo/alg1_demo.py", line 8, in <module>
from Projectparser import alg1
File "../Projectparser/Projectparser/alg1/__init__.py", line 1, in <module>
from alg1 import *
File "/home//Projectparser/Projectparser/alg1/alg1.py", line 13, in <module>
from ..logmatch import regexmatch
ImportError: attempted relative import with no known parent package
The same problem persists for all the three algorithms alg1, alg2, alg3. I'm not sure how to fix this and is using sys.append.path() the best way to solve the above mentioned problems.
Can anyone suggest how to solve the final import error : ImportError: attempted relative import with no known parent package
and if there is any other compact way of solving the other import errors instead of using sys.path.append().
Thanks in advance
Use the following statement to add the path of the file that needs to be imported to the system path to help VSCode find it:
import os,sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
The following is part of the project I created that you provided:
Result:
The following points need to be noted:
When importing, the subfolders are connected with.
Please avoid using files and folders with the same name to prevent confusion when VSCode finds modules.
If the result is executable but there is still a wave line, you can add in settings.json: "python.linting.pylintArgs": [ "----extension-pkg-whitelist=1xml" ],
Update:
According to the code of the link you provided, I reproduced the problem you described locally, and the solution is as follows:
Comment out the content "from SLCT import *" of logparser-master\logparser\SLCT_init_.py.
Add import os,sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
to SLCT_demo.py
operation result:
I'm getting this error when trying to import a module from the Prov package.
Here is the contents of my file:
#!/usr/bin/env
import sys
egg_path='/Library/Python/2.7/site-packages/prov-1.5.0-py2.7.egg/prov'
sys.path.append(egg_path)
#... rest of code
import model as prov
def main():
# Create a new provenance document
d1 = ProvDocument() # d1 is now an empty provenance document
# Declaring namespaces for various prefixes used in the example
d1.add_namespace('now', 'http://www.provbook.org/nownews/')
d1.add_namespace('nowpeople', 'http://www.provbook.org/nownews/people/')
d1.add_namespace('bk', 'http://www.provbook.org/ns/#')
# Entity: now:employment-article-v1.html
e1 = d1.entity('now:employment-article-v1.html')
# Agent: nowpeople:Bob
d1.agent('nowpeople:Bob')
And here is the output:
Traceback (most recent call last):
File "prov.py", line 6, in <module>
import model as prov
File "/Library/Python/2.7/site-packages/prov-1.5.0-py2.7.egg/prov/model.py", line 25, in <module>
from prov import Error, serializers
ImportError: cannot import name Error
Any ideas or fixes? I installed Prov using easy_install prov.
You need to rename your module file prov.py. It prevents import of the third-party library because the module name conflicts.
Make sure prov.pyc is removed.
I found the error. The name of my file that I was trying to import into was also called prov.py . It was a circular dependency issue.
Thank you guys for such quick responses!
Matplotlib working in this app perfectly.. but not working in build for some reason. Why?
I'll gladly take any advice that can help me.
.exe.log:
Traceback (most recent call last):
File "main.py", line 3, in <module>
File "zipextimporter.pyc", line 82, in load_module
File "matplotlib\__init__.pyc", line 103, in <module>
File "zipextimporter.pyc", line 82, in load_module
File "distutils\__init__.pyc", line 25, in <module>
ImportError: cannot import name dist
main.py is a script that I'm building. line #3 of it:
import matplotlib
build.py:
# encoding: utf-8
import os
import sys
import errno
sys.path.append(os.path.abspath("."))
from distutils.core import setup
import shutil
import py2exe
import matplotlib as mpl
mpl.use('Agg')
distDir = 'dist'
shutil.rmtree('build', ignore_errors=True)
shutil.rmtree(distDir, ignore_errors=True)
try:
os.makedirs(distDir)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(distDir):
pass
else:
raise
icon = 'icon.ico'
includes = ['matplotlib', 'numpy']
packages = ['matplotlib', 'pytz']
excludes = [
'_gtkagg', '_tkagg', 'bsddb', 'curses', 'email',
'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.dialogs',
'tcl', 'Tkconstants', 'Tkinter', 'sqlite3', 'doctest', 'test'
]
dll_excludes = [
'libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'libgdk_pixbuf-2.0-0.dll',
'tcl84.dll', 'tk84.dll', 'w9xpopen.exe'
]
data_files = mpl.get_py2exe_datafiles()
class Target(object):
def __init__(self, **kw):
self.__dict__.update(kw)
icon_resources = [(0, icon)]
GUI2Exe_Target = Target(
script='main.py',
dest_base='app_name',
name='app_name',
company_name='company_name',
copyright='company_name',
version='0.0.1',
icon_resources=icon_resources,
bitmap_resources=[],
other_resources=[]
)
setup(
options={
"py2exe": {
"compressed": 1,
"optimize": 0,
"includes": includes,
"excludes": excludes,
"packages": packages,
"dll_excludes": dll_excludes,
"bundle_files": 1,
"dist_dir": distDir,
"skip_archive": False,
"xref": False,
"ascii": False,
"custom_boot_script": '',
}
},
zipfile=None,
data_files=data_files,
console=[],
windows=[GUI2Exe_Target],
service=[],
com_server=[],
ctypes_com_server=[]
)
pip freeze:
..
matplotlib==1.3.1
numpy==1.8.2
..
python --version:
Python 2.7.6
Okay, I did not find proper solution for this problem.
I solved it with dirty hack, by simply replacing distutils dir in venv by distutils dir of system python. Now it all working and it working in venv! Don't quite sure about drawbacks of that though.
The issue as I can understand it, is that distutils of venv is really weird thing. Seems like venvwrapper or/and python packages changed it for some reasons, I don't know.
If you know something about this situation, please go ahead and add it to the thread as answers or comments. :)
I have cloned a github Repo PyCMake, and got into the root folder of the project. ImpPackage(Package containing several module) and maker.py(have some imports from ImpPackage) are the two files in this folder.
PyCMake/
+Readme & many more things
+setup.py
+pycmake/
__init__
maker.py
ImpPackages/
+__init__
+contains many .py modules
maker.py
....
from pycmake import ImpPackage
...
...
When I run
$python maker.py
Traceback (most recent call last):
File "pycmake/maker.py", line 4, in <module>
from pycmake import ImpPackage
ImportError: cannot import name ImpPackage
But when I make maker.py as:
....
import ImpPackage #removed 'from'
...
...
Its working fine.
I want the import statement to be
from pycmake import ImpPackage
because its a Open Source Project. How will I solve this problem?
I was installing jira-python like written in the docs
$ pip install jira-python
but after installation I try to run the example:
from jira.client import JIRA
options = {
'server': 'https://jira.atlassian.com'
}
jira = JIRA(options)
projects = jira.projects()
keys = sorted([project.key for project in projects])[2:5]
issue = jira.issue('JRA-1330')
import re
atl_comments = [comment for comment in issue.fields.comment.comments
if re.search(r'#atlassian.com$', comment.author.emailAddress)]
jira.add_comment(issue, 'Comment text')
issue.update(summary="I'm different!", description='Changed the summary to be different.')
issue.delete()
getting the following error:
**Traceback (most recent call last):
File "jira.py", line 4, in <module>
from jira.client import JIRA
File "/home/ubuntu/jira.py", line 4, in <module>
from jira.client import JIRA
ImportError: No module named client**
Any idea about the problem here? I tried it also on an Amazon instance, but same problem...
seems like the reason was that my test file was named jira.py :) thanks for your help Inbar!