microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.31k stars 1.18k forks source link

configuration for python source files in subfolder #209

Closed DonJayamanne closed 6 years ago

DonJayamanne commented 6 years ago

From @kannix on October 27, 2017 13:25

i have a project where all of our code is in a src subfolder. how do i configure vs code and this plugin to correctly handle that?

Copied from original issue: DonJayamanne/pythonVSCode#1348

DonJayamanne commented 6 years ago

You could just open the src folder vscode

DonJayamanne commented 6 years ago

From @kannix on October 27, 2017 14:6

But i need to edit the whole project ;) Think about the project like something with this data structure (stolen from https://drivendata.github.io/cookiecutter-data-science/#directory-structure)

├── LICENSE
├── Makefile           <- Makefile with commands like `make data` or `make train`
├── README.md          <- The top-level README for developers using this project.
├── data
│   ├── external       <- Data from third party sources.
│   ├── interim        <- Intermediate data that has been transformed.
│   ├── processed      <- The final, canonical data sets for modeling.
│   └── raw            <- The original, immutable data dump.
│
├── docs               <- A default Sphinx project; see sphinx-doc.org for details
│
├── models             <- Trained and serialized models, model predictions, or model summaries
│
├── notebooks          <- Jupyter notebooks. Naming convention is a number (for ordering),
│                         the creator's initials, and a short `-` delimited description, e.g.
│                         `1.0-jqp-initial-data-exploration`.
│
├── references         <- Data dictionaries, manuals, and all other explanatory materials.
│
├── reports            <- Generated analysis as HTML, PDF, LaTeX, etc.
│   └── figures        <- Generated graphics and figures to be used in reporting
│
├── requirements.txt   <- The requirements file for reproducing the analysis environment, e.g.
│                         generated with `pip freeze > requirements.txt`
│
├── src                <- Source code for use in this project.
│   ├── __init__.py    <- Makes src a Python module
│   │
│   ├── data           <- Scripts to download or generate data
│   │   └── make_dataset.py
│   │
│   ├── features       <- Scripts to turn raw data into features for modeling
│   │   └── build_features.py
│   │
│   ├── models         <- Scripts to train models and then use trained models to make
│   │   │                 predictions
│   │   ├── predict_model.py
│   │   └── train_model.py
│   │
│   └── visualization  <- Scripts to create exploratory and results oriented visualizations
│       └── visualize.py
│
└── tox.ini            <- tox file with settings for running tox; see tox.testrun.org
DonJayamanne commented 6 years ago

Why not open the soucre directory itself. It should still work. Have you come across any problems in opening the root folder?

DonJayamanne commented 6 years ago

From @kannix on October 27, 2017 15:46

Why not open the soucre directory itself. It should still work.

What do you mean with this? Open it in a separate VS Code window? I don't do it because i have to work on all files within that project

Have you come across any problems in opening the root folder?

one of the problems is that pylint fails on all imports (i guess because it searches from the project root folder)
another one is running a cell with jupyter doesn't work (most likely because it runs from project root, too)

DonJayamanne commented 6 years ago

one of the problems is that pylint fails on all imports (i guess because it searches from the project root folder)

Ok, now that makes sense. It would be easier to let me know what you have tried and what fails. As I have no idea of the context.

Here's the solution, you can change how pylint searches for files, by changing the PYTHONPATH environment variable.

Here are the details:

This should fix it

DonJayamanne commented 6 years ago

From @kannix on October 31, 2017 13:49

Could you point me to a documentation about that .env file? Is this a pythonVSCode feature or a Visual Studio Code one? I tried multiple paths (relative and full) and different syntax for this and it didn't work at all

DonJayamanne commented 6 years ago

It's a python vscode feature. Sorry I haven't documented this yet.

DonJayamanne commented 6 years ago

From @kannix on October 31, 2017 16:12

okay maybe i made an obvious error: i created a file called .env in the project root with content like this: PYTHONPATH=/Users/kannix/theprojectroot/src and I'm using MacOS

DonJayamanne commented 6 years ago

Here's what we can do to confirm this:

  1. Create a temporary python file named x.py some where within your project

  2. Add the following code:

    import os
    print(os.getenv("PYTHONPATH", "NONE"))
  3. Debug the above file It should print the value for PYTHONPATH with the value you have provided in it (along with the standard PYTHONPATH value).

  4. If this doesn't work, please restart VS Code and try once again.

kannix commented 6 years ago

I don't know what i did exactly.. but after removing my .vscode config + some reinstallations of python and packages.. everything seems to work without any special configuration for the subfolder 👍 feel free to close this issue

eiva commented 6 years ago

Could PYTHONPATH be relative to project or workspace root?

brettcannon commented 6 years ago

@eiva I think Python will accept that; basically the site module will make all paths absolute to where Python is executed from, so that should be the workspace folder that's currently open.