I'm having trouble with Eclipse PyDev autocomplete when trying to use an import statement in a file in a subfolder.
My folder structure is:
src
--data
--libs
--__init__.py
--myclass.py
--logs
mainscript.py
When I use import in mainscript.py (i.e. import re) everything goes fine, however when I try to use import in myclass.py eclipse doesn't show the autocomplete display.
Related
I'm creating my first package and I can not figure out how to create a simple test.py file to debug my code.
Consider the following file structure.
|--MyPackage
|--test.py
|--PackageName
|--__init__.py
|--module.py
Inside test.py I have: from .src.module import SomeClass
And of course this gives me the dreaded "attempted relative import with no known parent package" message.
I know I could just install the package and then import it with from PackageName.module import SomeClass but then I'm using the code installed on my system and not the code that I am actively editing.
There has got to be some kind of standard way for testing and debugging a package right? Despite all the searching I've done, I can't seem to find any kind of solution.
I'm running test.py with python3 test.py
If it's helpful, here is a screenshot of my actual project folder structure:
I am building on AWS CodeBuild using Python 2.7, but I believe this is much more a generic python import problem. I have a directory setting, shown below. I am running test.py inside the test folder. I would like to import the dependency mainScript.py as part of this testing. However, I cannot seem to get the relative dependencies right and I am having a lot of difficulty importing the mainScript within the test folder. Below is a layout of my directory folder
main
src
mainScript.py
test
test.py
If for example my directory setup was something like
main
test
test.py
mainScript.py
I could have my import be done the following way
from mainScript import *
I have confirmed this works. But, I like it in its own src folder. I have tried all these These are the following relative path attempts I have tried
from ..src/mainScript import * #SyntaxError: invalid syntax
from ..src.mainScript import * #ValueError: attempted relative import beyond top-level package
from mainScript import * #ModuleNotFoundError: No module named 'mainScript'
from src.mainScript import * #ModuleNotFoundError: No module named 'src'
from src/mainScript import * #SyntaxError: invalid syntax
I have been struggling for a bit and I couldn't quite find a question with someone asking about accessing a brother/sister folder script. Thank you in advance for your help.
Python treats directories as packages if they contain a __init__.py file. Updating your structure to the following should do the trick:
__init__.py
src
__init__.py
mainScript.py
test
__init__.py
test.py
Now, from test.py, you could do from ..src import *. For more details on init.py, you can look here: What is __init__.py for?
In addition to adding the init.py files. It ended up being that I had to run python with the -m argument in my command, which was added in Python 2.4.
PEP 338
Python 2.4 adds the command line switch -m to allow modules to be
located using the Python module namespace for execution as scripts.
The motivating examples were standard library modules such as pdb and
profile, and the Python 2.4 implementation is fine for this limited
purpose.
So the command to launch from the top directory is:
python -m test.test
This seems to work and get the right namespace. Then in your test.py file you would import the mainScript the following way
from src.mainScript import *
I am new to windows and I am working on setting up code and modules with visual studio.
I have the following folder structure for my code:
myModule
__init__.py
mymodule.py
myScript
myscript.py
In myscript.py I have the following command:
from myModule import *
In visual studio this command works, but when I run the script command line I get the following error:
ModuleNotFoundError: No Module named myModule
Is there a quick trick in windows to do the job w/o having to install myModule as a package?
Any of the usual tricks that work in linux don't seem to work in windows. I.e.,
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'myModule'))
or,
import ..myModule
The path should have entries that point to the module. Your path entry is pointing inside the module.
What you had:
os.path.join(os.path.dirname(__file__), r"..", 'myModule')
Instead, you want to point to the directory containing myModule, which would be:
os.path.join(os.path.dirname(__file__), r"..")
I'm currently trying to bundle my Python (3.4.4) application with PyInstaller. I'm following the PyInstaller documentation about how to build a .spec file.
This is how my project file tree looks like:
project/
__init__.py
main.py
ui.py
Lib/
__init__.py
History.py
Command.py
Graphics.py
Tools.py
According to the documentation, all I have to do is to run the pyi-makespec main.py function and the Lib module will be detected as long as it is imported from the main.py file.
Documentation:
Because your script includes the statement import helpmod, PyInstaller will create this folder arrangement in your bundled app
https://pythonhosted.org/PyInstaller/spec-files.html#using-data-files-from-a-module
This is how the beginning of the my main.py looks like
# main.py
from Lib.Command import Command
from Lib.History import History
from Lib.Graphics import Graphics
import Lib.Tools as Tools
When I try to run the app afterwards, all I get is this error. I probably missed something, does someone have an idea of the problem ? :D
error log : http://pastebin.com/3dygTqfn
EDIT : Just figured out that the problem comes from my Graphics.py library, which import some bokeh tools I use to generate histograms. Still don't know why the bokeh import makes the whole thing crash, it works perfectly fine when I run it with a python interpreter...
I have a module I have installed called lts_fits, and this is its path:
~/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/lts_fits
So it is clearly in the site packages folder. Within this folder, there is a python script:
lts_linefit.py
Yet when I have this line of code in my script:
from lts_fits import lts_linefit
I get this error:
ImportError: No module named lts_fits
How? It's clearly in there, and I have tried this same syntax with other random scripts and they import just fine. For instance, a file abc.py located in the folder ~/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sympy imports just fine when I have the line from sympy import abc. What could be going wrong?
You need an __init__.py file in that directory (you do not have to put anything into the file, all you need to do is create it).
The easiest way to create said file is by using:
touch __init__.py
from within your lts_fits directory in your command line/terminal/console.
See this SO article: What is __init__.py for?
And the Python Documentation for packages.