Importing project module TO project subdir, WITHOUT relative path The 2019 Stack Overflow Developer Survey Results Are InGet the source directory of a Bash script from within the script itselfHow to import a module given the full path?Import a module from a relative pathRunning unittest with typical test directory structureHow can I import a C++ python extension into a module in another directory?Relative imports for the billionth timeHow are absolute imports possible from within a subpackage?PyCharm error: 'No Module' when trying to import own module (python script)Importing the current project as a module in pythonCannot import modules in jupyter notebook; wrong sys.pathHow do I import module in jupyter notebook directory into notebooks in lower directories?
Straighten subgroup lattice
How can I define good in a religion that claims no moral authority?
Why not take a picture of a closer black hole?
Inverse Relationship Between Precision and Recall
Button changing its text & action. Good or terrible?
Why doesn't UInt have a toDouble()?
Geography at the pixel level
What's the name of these plastic connectors
How to notate time signature switching consistently every measure
If a sorcerer casts the Banishment spell on a PC while in Avernus, does the PC return to their home plane?
Cooking pasta in a water boiler
Mathematics of imaging the black hole
Why does the nucleus not repel itself?
How to charge AirPods to keep battery healthy?
Slides for 30 min~1 hr Skype tenure track application interview
How to support a colleague who finds meetings extremely tiring?
How do you keep chess fun when your opponent constantly beats you?
How do PCB vias affect signal quality?
Why doesn't shell automatically fix "useless use of cat"?
Does HR tell a hiring manager about salary negotiations?
What do I do when my TA workload is more than expected?
Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?
Likelihood that a superbug or lethal virus could come from a landfill
Why “相同意思的词” is called “同义词” instead of "同意词"?
Importing project module TO project subdir, WITHOUT relative path
The 2019 Stack Overflow Developer Survey Results Are InGet the source directory of a Bash script from within the script itselfHow to import a module given the full path?Import a module from a relative pathRunning unittest with typical test directory structureHow can I import a C++ python extension into a module in another directory?Relative imports for the billionth timeHow are absolute imports possible from within a subpackage?PyCharm error: 'No Module' when trying to import own module (python script)Importing the current project as a module in pythonCannot import modules in jupyter notebook; wrong sys.pathHow do I import module in jupyter notebook directory into notebooks in lower directories?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This must have been asked before, but I cannot for the life of me find it.
I have a project structure something like this
ROOT
├── README.md
├── modules
│ ├── __init__.py
│ ├── mod1.py
│ └── mod2.py
├── tests
│ ├── test_mod1.py
│ └── test_mod2.py
├── notebooks
│ ├── nb1.ipynb
│ ├── nb2.ipynb
│ └── sub_dir
│ ├── sub_nb.ipynb
│ ├── generate_py
│ └── py_files
│ └── sub_nb.py
├── definitions
└── main.py
So from main.py
I am able to import anything from definitions
, or any module from ROOT/modules
.
What I want is to be able to import from these from anywhere within the notebooks
directory tree. I know I could do this using:
import sys
sys.path.append("..")
But the notebooks
directory tree has many layers, and I don't want my code to start looking like this:
import sys
sys.path.append("../../../../")
What's more, the file generate_py
is a bash script, that converts the jupyter notebooks (.ipynb
) to .py
files and stashes the .py
files into the ./py_files
subdir.
With the above method i end up having to manually edit every file to put an extra ../
into the sys.append()
. This is annoying.
If I run files from within pycharm all works well (I'm guessing it updates your PYTHONPATH to include the project root when you create main.py
?)
To further complicate, this project is run on several machines, shared via git. So absolute references are out.
But running from terminal, or from within jupyter it cant gind modules, or definitions without going through the sys.append()
process. And I feel there must be a better way.
So what is the best practice here?
python import pycharm project directory-structure
add a comment |
This must have been asked before, but I cannot for the life of me find it.
I have a project structure something like this
ROOT
├── README.md
├── modules
│ ├── __init__.py
│ ├── mod1.py
│ └── mod2.py
├── tests
│ ├── test_mod1.py
│ └── test_mod2.py
├── notebooks
│ ├── nb1.ipynb
│ ├── nb2.ipynb
│ └── sub_dir
│ ├── sub_nb.ipynb
│ ├── generate_py
│ └── py_files
│ └── sub_nb.py
├── definitions
└── main.py
So from main.py
I am able to import anything from definitions
, or any module from ROOT/modules
.
What I want is to be able to import from these from anywhere within the notebooks
directory tree. I know I could do this using:
import sys
sys.path.append("..")
But the notebooks
directory tree has many layers, and I don't want my code to start looking like this:
import sys
sys.path.append("../../../../")
What's more, the file generate_py
is a bash script, that converts the jupyter notebooks (.ipynb
) to .py
files and stashes the .py
files into the ./py_files
subdir.
With the above method i end up having to manually edit every file to put an extra ../
into the sys.append()
. This is annoying.
If I run files from within pycharm all works well (I'm guessing it updates your PYTHONPATH to include the project root when you create main.py
?)
To further complicate, this project is run on several machines, shared via git. So absolute references are out.
But running from terminal, or from within jupyter it cant gind modules, or definitions without going through the sys.append()
process. And I feel there must be a better way.
So what is the best practice here?
python import pycharm project directory-structure
Instead of adding ROOT to yourPYTHONPATH
dynamically in every file, every time it runs, you can add it by modifying the environment variable, i.e. addexport PYTHONPATH="$PYTHONPATH:/path/to/ROOT"
to your.bashrc
or similar. Since ROOT might be in different locations on different users' machines, you should either have a README telling users to do this, or else asetup
shell script which computes the path and does this automatically.
– Phydeaux
Mar 8 at 11:48
I did meangenerate_py
.
– FinleyGibson
Mar 8 at 17:01
I did think about editing PYTHONPATH in .bashrc, but it still seems a bit inelegant. I will then forever expose any project I work on to accidentally importing fromdefinitions
, when I inevitably forget to remove the line later. I still feel there must be a cleaner way.
– FinleyGibson
Mar 8 at 17:04
You can usevirtualenv
(optionally alsovirtualenvwrapper
for a nicer experience) to make an environment on a per-project basis, and add theexport PYTHONPATH...
to the environment'sactivate
script instead, to avoid polluting your.bashrc
– Phydeaux
Mar 8 at 17:14
add a comment |
This must have been asked before, but I cannot for the life of me find it.
I have a project structure something like this
ROOT
├── README.md
├── modules
│ ├── __init__.py
│ ├── mod1.py
│ └── mod2.py
├── tests
│ ├── test_mod1.py
│ └── test_mod2.py
├── notebooks
│ ├── nb1.ipynb
│ ├── nb2.ipynb
│ └── sub_dir
│ ├── sub_nb.ipynb
│ ├── generate_py
│ └── py_files
│ └── sub_nb.py
├── definitions
└── main.py
So from main.py
I am able to import anything from definitions
, or any module from ROOT/modules
.
What I want is to be able to import from these from anywhere within the notebooks
directory tree. I know I could do this using:
import sys
sys.path.append("..")
But the notebooks
directory tree has many layers, and I don't want my code to start looking like this:
import sys
sys.path.append("../../../../")
What's more, the file generate_py
is a bash script, that converts the jupyter notebooks (.ipynb
) to .py
files and stashes the .py
files into the ./py_files
subdir.
With the above method i end up having to manually edit every file to put an extra ../
into the sys.append()
. This is annoying.
If I run files from within pycharm all works well (I'm guessing it updates your PYTHONPATH to include the project root when you create main.py
?)
To further complicate, this project is run on several machines, shared via git. So absolute references are out.
But running from terminal, or from within jupyter it cant gind modules, or definitions without going through the sys.append()
process. And I feel there must be a better way.
So what is the best practice here?
python import pycharm project directory-structure
This must have been asked before, but I cannot for the life of me find it.
I have a project structure something like this
ROOT
├── README.md
├── modules
│ ├── __init__.py
│ ├── mod1.py
│ └── mod2.py
├── tests
│ ├── test_mod1.py
│ └── test_mod2.py
├── notebooks
│ ├── nb1.ipynb
│ ├── nb2.ipynb
│ └── sub_dir
│ ├── sub_nb.ipynb
│ ├── generate_py
│ └── py_files
│ └── sub_nb.py
├── definitions
└── main.py
So from main.py
I am able to import anything from definitions
, or any module from ROOT/modules
.
What I want is to be able to import from these from anywhere within the notebooks
directory tree. I know I could do this using:
import sys
sys.path.append("..")
But the notebooks
directory tree has many layers, and I don't want my code to start looking like this:
import sys
sys.path.append("../../../../")
What's more, the file generate_py
is a bash script, that converts the jupyter notebooks (.ipynb
) to .py
files and stashes the .py
files into the ./py_files
subdir.
With the above method i end up having to manually edit every file to put an extra ../
into the sys.append()
. This is annoying.
If I run files from within pycharm all works well (I'm guessing it updates your PYTHONPATH to include the project root when you create main.py
?)
To further complicate, this project is run on several machines, shared via git. So absolute references are out.
But running from terminal, or from within jupyter it cant gind modules, or definitions without going through the sys.append()
process. And I feel there must be a better way.
So what is the best practice here?
python import pycharm project directory-structure
python import pycharm project directory-structure
edited Mar 8 at 17:06
FinleyGibson
asked Mar 8 at 10:47
FinleyGibsonFinleyGibson
19511
19511
Instead of adding ROOT to yourPYTHONPATH
dynamically in every file, every time it runs, you can add it by modifying the environment variable, i.e. addexport PYTHONPATH="$PYTHONPATH:/path/to/ROOT"
to your.bashrc
or similar. Since ROOT might be in different locations on different users' machines, you should either have a README telling users to do this, or else asetup
shell script which computes the path and does this automatically.
– Phydeaux
Mar 8 at 11:48
I did meangenerate_py
.
– FinleyGibson
Mar 8 at 17:01
I did think about editing PYTHONPATH in .bashrc, but it still seems a bit inelegant. I will then forever expose any project I work on to accidentally importing fromdefinitions
, when I inevitably forget to remove the line later. I still feel there must be a cleaner way.
– FinleyGibson
Mar 8 at 17:04
You can usevirtualenv
(optionally alsovirtualenvwrapper
for a nicer experience) to make an environment on a per-project basis, and add theexport PYTHONPATH...
to the environment'sactivate
script instead, to avoid polluting your.bashrc
– Phydeaux
Mar 8 at 17:14
add a comment |
Instead of adding ROOT to yourPYTHONPATH
dynamically in every file, every time it runs, you can add it by modifying the environment variable, i.e. addexport PYTHONPATH="$PYTHONPATH:/path/to/ROOT"
to your.bashrc
or similar. Since ROOT might be in different locations on different users' machines, you should either have a README telling users to do this, or else asetup
shell script which computes the path and does this automatically.
– Phydeaux
Mar 8 at 11:48
I did meangenerate_py
.
– FinleyGibson
Mar 8 at 17:01
I did think about editing PYTHONPATH in .bashrc, but it still seems a bit inelegant. I will then forever expose any project I work on to accidentally importing fromdefinitions
, when I inevitably forget to remove the line later. I still feel there must be a cleaner way.
– FinleyGibson
Mar 8 at 17:04
You can usevirtualenv
(optionally alsovirtualenvwrapper
for a nicer experience) to make an environment on a per-project basis, and add theexport PYTHONPATH...
to the environment'sactivate
script instead, to avoid polluting your.bashrc
– Phydeaux
Mar 8 at 17:14
Instead of adding ROOT to your
PYTHONPATH
dynamically in every file, every time it runs, you can add it by modifying the environment variable, i.e. add export PYTHONPATH="$PYTHONPATH:/path/to/ROOT"
to your .bashrc
or similar. Since ROOT might be in different locations on different users' machines, you should either have a README telling users to do this, or else a setup
shell script which computes the path and does this automatically.– Phydeaux
Mar 8 at 11:48
Instead of adding ROOT to your
PYTHONPATH
dynamically in every file, every time it runs, you can add it by modifying the environment variable, i.e. add export PYTHONPATH="$PYTHONPATH:/path/to/ROOT"
to your .bashrc
or similar. Since ROOT might be in different locations on different users' machines, you should either have a README telling users to do this, or else a setup
shell script which computes the path and does this automatically.– Phydeaux
Mar 8 at 11:48
I did mean
generate_py
.– FinleyGibson
Mar 8 at 17:01
I did mean
generate_py
.– FinleyGibson
Mar 8 at 17:01
I did think about editing PYTHONPATH in .bashrc, but it still seems a bit inelegant. I will then forever expose any project I work on to accidentally importing from
definitions
, when I inevitably forget to remove the line later. I still feel there must be a cleaner way.– FinleyGibson
Mar 8 at 17:04
I did think about editing PYTHONPATH in .bashrc, but it still seems a bit inelegant. I will then forever expose any project I work on to accidentally importing from
definitions
, when I inevitably forget to remove the line later. I still feel there must be a cleaner way.– FinleyGibson
Mar 8 at 17:04
You can use
virtualenv
(optionally also virtualenvwrapper
for a nicer experience) to make an environment on a per-project basis, and add the export PYTHONPATH...
to the environment's activate
script instead, to avoid polluting your .bashrc
– Phydeaux
Mar 8 at 17:14
You can use
virtualenv
(optionally also virtualenvwrapper
for a nicer experience) to make an environment on a per-project basis, and add the export PYTHONPATH...
to the environment's activate
script instead, to avoid polluting your .bashrc
– Phydeaux
Mar 8 at 17:14
add a comment |
0
active
oldest
votes
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%2f55061564%2fimporting-project-module-to-project-subdir-without-relative-path%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f55061564%2fimporting-project-module-to-project-subdir-without-relative-path%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
Instead of adding ROOT to your
PYTHONPATH
dynamically in every file, every time it runs, you can add it by modifying the environment variable, i.e. addexport PYTHONPATH="$PYTHONPATH:/path/to/ROOT"
to your.bashrc
or similar. Since ROOT might be in different locations on different users' machines, you should either have a README telling users to do this, or else asetup
shell script which computes the path and does this automatically.– Phydeaux
Mar 8 at 11:48
I did mean
generate_py
.– FinleyGibson
Mar 8 at 17:01
I did think about editing PYTHONPATH in .bashrc, but it still seems a bit inelegant. I will then forever expose any project I work on to accidentally importing from
definitions
, when I inevitably forget to remove the line later. I still feel there must be a cleaner way.– FinleyGibson
Mar 8 at 17:04
You can use
virtualenv
(optionally alsovirtualenvwrapper
for a nicer experience) to make an environment on a per-project basis, and add theexport PYTHONPATH...
to the environment'sactivate
script instead, to avoid polluting your.bashrc
– Phydeaux
Mar 8 at 17:14