File importing on python project
I want to have following project directory structure in python:
project
generic.py
subdir1
file1.py
subdir2
file2.py
subdir3
file3.py
within file1.py I want to import generic.py from a root dir of project.
import generic
Unfortunatelly I got error when a try to execute "file1.py"
root:~/project/sidir1# python file1.py
Traceback (most recent call last):
File "file1.py", line 7, in <module>
import generic
ImportError: No module named generic
I want to execute individual file*.py and have generic.py included.
How to correctly include it under such a drirectory structure?
python
add a comment |
I want to have following project directory structure in python:
project
generic.py
subdir1
file1.py
subdir2
file2.py
subdir3
file3.py
within file1.py I want to import generic.py from a root dir of project.
import generic
Unfortunatelly I got error when a try to execute "file1.py"
root:~/project/sidir1# python file1.py
Traceback (most recent call last):
File "file1.py", line 7, in <module>
import generic
ImportError: No module named generic
I want to execute individual file*.py and have generic.py included.
How to correctly include it under such a drirectory structure?
python
add a comment |
I want to have following project directory structure in python:
project
generic.py
subdir1
file1.py
subdir2
file2.py
subdir3
file3.py
within file1.py I want to import generic.py from a root dir of project.
import generic
Unfortunatelly I got error when a try to execute "file1.py"
root:~/project/sidir1# python file1.py
Traceback (most recent call last):
File "file1.py", line 7, in <module>
import generic
ImportError: No module named generic
I want to execute individual file*.py and have generic.py included.
How to correctly include it under such a drirectory structure?
python
I want to have following project directory structure in python:
project
generic.py
subdir1
file1.py
subdir2
file2.py
subdir3
file3.py
within file1.py I want to import generic.py from a root dir of project.
import generic
Unfortunatelly I got error when a try to execute "file1.py"
root:~/project/sidir1# python file1.py
Traceback (most recent call last):
File "file1.py", line 7, in <module>
import generic
ImportError: No module named generic
I want to execute individual file*.py and have generic.py included.
How to correctly include it under such a drirectory structure?
python
python
asked Nov 14 '18 at 15:05
user3428154user3428154
759
759
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You can make your python project a package by making __init__.py
in the root directory.
You can then use relative imports, ex: from .. import generic
I added /__init__.py and within my file1.py did "import ..generic" but still syntax error
– user3428154
Nov 14 '18 at 15:40
@user3428154 try my edit
– Qwerty
Nov 14 '18 at 15:42
add a comment |
Have a Look here:
How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?
usingfrom .. import generic
I got error ValueError: Attempted relative import in non-package by following the recommendation
– user3428154
Nov 14 '18 at 19:43
add a comment |
It's cause of Python path, when you use import
, python will try to find in every directory in sys.path
if module with name given exist, if he don't found, he raise.
For fix your problem, just add parent directory to your python path
import sys
sys.path.append("..")
at which directory should I need to reside when adding sys.path.append("..")?
– user3428154
Nov 14 '18 at 15:43
your working dir is not important, it's relative to the directory of the file that is launched
– iElden
Nov 14 '18 at 15:46
add a comment |
I would encourage you to instead wrap generic.py
within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.
Just build a structure like this:
project
my_package
generic.py
__init__.py
subdir1
file1.py
__init__.py
subdir2
file2.py
__init__.py
subdir3
file3.py
__init__.py
And now in file*.py
do: from my_package import generic
Now on the project
level put a main.py
or similar script where you use the functions that are available in my_package.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303202%2ffile-importing-on-python-project%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can make your python project a package by making __init__.py
in the root directory.
You can then use relative imports, ex: from .. import generic
I added /__init__.py and within my file1.py did "import ..generic" but still syntax error
– user3428154
Nov 14 '18 at 15:40
@user3428154 try my edit
– Qwerty
Nov 14 '18 at 15:42
add a comment |
You can make your python project a package by making __init__.py
in the root directory.
You can then use relative imports, ex: from .. import generic
I added /__init__.py and within my file1.py did "import ..generic" but still syntax error
– user3428154
Nov 14 '18 at 15:40
@user3428154 try my edit
– Qwerty
Nov 14 '18 at 15:42
add a comment |
You can make your python project a package by making __init__.py
in the root directory.
You can then use relative imports, ex: from .. import generic
You can make your python project a package by making __init__.py
in the root directory.
You can then use relative imports, ex: from .. import generic
edited Nov 14 '18 at 15:42
answered Nov 14 '18 at 15:07
QwertyQwerty
835619
835619
I added /__init__.py and within my file1.py did "import ..generic" but still syntax error
– user3428154
Nov 14 '18 at 15:40
@user3428154 try my edit
– Qwerty
Nov 14 '18 at 15:42
add a comment |
I added /__init__.py and within my file1.py did "import ..generic" but still syntax error
– user3428154
Nov 14 '18 at 15:40
@user3428154 try my edit
– Qwerty
Nov 14 '18 at 15:42
I added /__init__.py and within my file1.py did "import ..generic" but still syntax error
– user3428154
Nov 14 '18 at 15:40
I added /__init__.py and within my file1.py did "import ..generic" but still syntax error
– user3428154
Nov 14 '18 at 15:40
@user3428154 try my edit
– Qwerty
Nov 14 '18 at 15:42
@user3428154 try my edit
– Qwerty
Nov 14 '18 at 15:42
add a comment |
Have a Look here:
How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?
usingfrom .. import generic
I got error ValueError: Attempted relative import in non-package by following the recommendation
– user3428154
Nov 14 '18 at 19:43
add a comment |
Have a Look here:
How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?
usingfrom .. import generic
I got error ValueError: Attempted relative import in non-package by following the recommendation
– user3428154
Nov 14 '18 at 19:43
add a comment |
Have a Look here:
How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?
Have a Look here:
How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?
answered Nov 14 '18 at 15:09
FlowFlow
358
358
usingfrom .. import generic
I got error ValueError: Attempted relative import in non-package by following the recommendation
– user3428154
Nov 14 '18 at 19:43
add a comment |
usingfrom .. import generic
I got error ValueError: Attempted relative import in non-package by following the recommendation
– user3428154
Nov 14 '18 at 19:43
using
from .. import generic
I got error ValueError: Attempted relative import in non-package by following the recommendation– user3428154
Nov 14 '18 at 19:43
using
from .. import generic
I got error ValueError: Attempted relative import in non-package by following the recommendation– user3428154
Nov 14 '18 at 19:43
add a comment |
It's cause of Python path, when you use import
, python will try to find in every directory in sys.path
if module with name given exist, if he don't found, he raise.
For fix your problem, just add parent directory to your python path
import sys
sys.path.append("..")
at which directory should I need to reside when adding sys.path.append("..")?
– user3428154
Nov 14 '18 at 15:43
your working dir is not important, it's relative to the directory of the file that is launched
– iElden
Nov 14 '18 at 15:46
add a comment |
It's cause of Python path, when you use import
, python will try to find in every directory in sys.path
if module with name given exist, if he don't found, he raise.
For fix your problem, just add parent directory to your python path
import sys
sys.path.append("..")
at which directory should I need to reside when adding sys.path.append("..")?
– user3428154
Nov 14 '18 at 15:43
your working dir is not important, it's relative to the directory of the file that is launched
– iElden
Nov 14 '18 at 15:46
add a comment |
It's cause of Python path, when you use import
, python will try to find in every directory in sys.path
if module with name given exist, if he don't found, he raise.
For fix your problem, just add parent directory to your python path
import sys
sys.path.append("..")
It's cause of Python path, when you use import
, python will try to find in every directory in sys.path
if module with name given exist, if he don't found, he raise.
For fix your problem, just add parent directory to your python path
import sys
sys.path.append("..")
answered Nov 14 '18 at 15:12
iEldeniElden
684517
684517
at which directory should I need to reside when adding sys.path.append("..")?
– user3428154
Nov 14 '18 at 15:43
your working dir is not important, it's relative to the directory of the file that is launched
– iElden
Nov 14 '18 at 15:46
add a comment |
at which directory should I need to reside when adding sys.path.append("..")?
– user3428154
Nov 14 '18 at 15:43
your working dir is not important, it's relative to the directory of the file that is launched
– iElden
Nov 14 '18 at 15:46
at which directory should I need to reside when adding sys.path.append("..")?
– user3428154
Nov 14 '18 at 15:43
at which directory should I need to reside when adding sys.path.append("..")?
– user3428154
Nov 14 '18 at 15:43
your working dir is not important, it's relative to the directory of the file that is launched
– iElden
Nov 14 '18 at 15:46
your working dir is not important, it's relative to the directory of the file that is launched
– iElden
Nov 14 '18 at 15:46
add a comment |
I would encourage you to instead wrap generic.py
within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.
Just build a structure like this:
project
my_package
generic.py
__init__.py
subdir1
file1.py
__init__.py
subdir2
file2.py
__init__.py
subdir3
file3.py
__init__.py
And now in file*.py
do: from my_package import generic
Now on the project
level put a main.py
or similar script where you use the functions that are available in my_package.
add a comment |
I would encourage you to instead wrap generic.py
within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.
Just build a structure like this:
project
my_package
generic.py
__init__.py
subdir1
file1.py
__init__.py
subdir2
file2.py
__init__.py
subdir3
file3.py
__init__.py
And now in file*.py
do: from my_package import generic
Now on the project
level put a main.py
or similar script where you use the functions that are available in my_package.
add a comment |
I would encourage you to instead wrap generic.py
within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.
Just build a structure like this:
project
my_package
generic.py
__init__.py
subdir1
file1.py
__init__.py
subdir2
file2.py
__init__.py
subdir3
file3.py
__init__.py
And now in file*.py
do: from my_package import generic
Now on the project
level put a main.py
or similar script where you use the functions that are available in my_package.
I would encourage you to instead wrap generic.py
within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.
Just build a structure like this:
project
my_package
generic.py
__init__.py
subdir1
file1.py
__init__.py
subdir2
file2.py
__init__.py
subdir3
file3.py
__init__.py
And now in file*.py
do: from my_package import generic
Now on the project
level put a main.py
or similar script where you use the functions that are available in my_package.
answered Nov 14 '18 at 15:16
KarlKarl
2,40143055
2,40143055
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303202%2ffile-importing-on-python-project%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown