Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env file not working. Workspace dir not scanned for modules by default. #2206

Closed
gauravojha opened this issue Jul 20, 2018 · 7 comments
Closed
Assignees
Labels
feature-request Request for new features or functionality

Comments

@gauravojha
Copy link

gauravojha commented Jul 20, 2018

Environment data

  • VS Code version: 1.25.1
  • Extension version (available under the Extensions sidebar): 2018.7.0
  • OS and version: macOS 10.13.6
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7, 3.6 (tested on both)

Actual behavior

Modules defined in the current workspace folder cannot be imported. The .env file made in the workspace dir has no effect.

Expected behavior

The modules which are created as part of a project must be read, if not automatically by default then through some setting which can be enabled.

Steps to reproduce:

I have a very simple project structure as shown.
image

From within package_1.module_1, I am trying to import package_2.module_2. And the error which is thrown is as shown.

sample /usr/local/bin/python3 /Users/gojha/Documents/sample/package_1/module_1.py
Traceback (most recent call last):
  File "/Users/gojha/Documents/sample/package_1/module_1.py", line 1, in <module>
    from package_2.module_2 import hello_world
ModuleNotFoundError: No module named 'package_2'

As per the documentation available, I created a .env file in the workspace folder ('sample' in this case), and then added the PYTHONPATH environment variable there, pointing to the source code ('/Users/gojha/Documents/sample' here). But running the python module stills sees the same error. This is getting really weird now, as I can't find any trace anywhere of how this is actually supposed to work, so that I could validate whether what I am doing is correct.

image

image

(If there is some problem in the configuration above, let me know)

Compared to VS Code, other IDEs like PyCharm run such sample code without any configuration problems. What I noticed was that PyCharm before running the module, appends the source folder to sys.path, thus these problems are not faced.

image

It would be great if something similar could be done for VS Code as well.

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Starting Jedi Python language engine.
##########Linting Output - pylint##########

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

##########Linting Output - pylint##########

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

##########Linting Output - pylint##########

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

##########Linting Output - pylint##########

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)

log.ts:159  INFO no standard startup: panel is active
@brettcannon brettcannon added bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster feature-interpreter labels Jul 23, 2018
@brettcannon
Copy link
Member

Can you provide the .env file so we can test against it?

@gauravojha
Copy link
Author

gauravojha commented Jul 23, 2018

The .env file just contained one line.

PYTHONPATH=/Users/gojha/Documents/sample , which was the location of the source code. The structure of the package is the same as show in the above images.

@brettcannon brettcannon added needs verification and removed info-needed Issue requires more information from poster labels Sep 5, 2018
@brettcannon
Copy link
Member

Can you verify, @d3r3kk ?

@d3r3kk
Copy link

d3r3kk commented Sep 11, 2018

I can run this in the debugger, but I cannot run it in the terminal. Also, I could not run it in the shell without VSCode unless I would set the PYTHONPATH accordingly.

Run from the debugger:

dekeeler@dek-laptop-lnx:~/dev/github/d3r3kk/test/2206_packages_mishap$ cd /home/dekeeler/dev/github/d3r3kk/test/2206_packages_mishap ; env "PYTHONPATH=/home/dekeeler/dev/github/d3r3kk/test/2206_packages_mishap:/home/dekeeler/.vscode-insiders/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd" "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /opt/python/pyenv/versions/3.7-dev/bin/python -m ptvsd --host localhost --port 33709 /home/dekeeler/dev/github/d3r3kk/test/2206_packages_mishap/package_1/module_1.py
sys.version_info(major=3, minor=7, micro=0, releaselevel='final', serial=0)
Hello world!

Run from the terminal:

dekeeler@dek-laptop-lnx:~/dev/github/d3r3kk/test$ /opt/python/pyenv/versions/3.7-dev/bin/python /home/dekeeler/dev/github/d3r3kk/test/2206_packages_mishap/package_1/module_1.py
Traceback (most recent call last):
  File "/home/dekeeler/dev/github/d3r3kk/test/2206_packages_mishap/package_1/module_1.py", line 1, in <module>
    from package_2.module_2 import hello_world
ModuleNotFoundError: No module named 'package_2'

Note: PYTHONPATH is updated according to the contents of the .env file when we run the debugger. It is not updated when we run from the Run Python File in Terminal command.

Indeed, when I run from my bash shell without VSCode running, if I set the PYTHONPATH before my python call it works as well:

dekeeler@dek-laptop-lnx:~/dev/github/d3r3kk/test/2206_packages_mishap$ PYTHONPATH="/home/dekeeler/dev/github/d3r3kk/test/2206_packages_mishap" python package_1/module_1.py 
sys.version_info(major=2, minor=7, micro=15, releaselevel='candidate', serial=1)
Hello world!

Test code:
test_2206.zip

@DonJayamanne DonJayamanne added feature-request Request for new features or functionality needs decision and removed needs PR bug Issue identified by VS Code Team member as probable bug labels Sep 12, 2018
@DonJayamanne
Copy link

Closing as a duplicate of #944

@DonJayamanne
Copy link

DonJayamanne commented Sep 20, 2018

@brettcannon pycham supports this feature. Uusers would expect similar behavior in VSC. Isn't it too early to close this issue #944?

@brettcannon
Copy link
Member

@DonJayamanne reopened the other issue to discuss with Dan.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

4 participants