Answer a question

I am working on a python package with VS Code with the following layout of the opened workspace folder in VS Code

workspace
| + tests
| | - test1.py
| | + other_tests
| | | - test2.py 
| + mymodule
| | ...

What I want is to call in test1.py and test2.py the package mymodule with

import mymodule

When I do this, I always get the error no module named 'mymodule'. I know i can load the path via inserting the following to all test files:

import sys
sys.path.append('<path_to>/workspace/')

Then mymodule is correctly loaded. (I know I can use pathlib.Path(__file__).parent[n] for some n to get <path_to>.

But this process is really cumbersome if you have many test files in different folders... and it is just ugly.

Is there any way, to tell VS Code to always include the workspace in the path?

I tried a .env file with

PYTHONPATH=<path_to>/workspace/

and I also tried to add the following to launch.json:

"env": { "PYTHONPATH": "${workspaceFolder}" }

Both did not do the trick. (I am also not sure if PYTHONPATH is at all the right solution to my problem, i just tried it ^^).

In short:

I would like to tell VS Code, that it should always include workspace to the search path whenever a python function tries to load a module, regarding the location of the function, so I do not have to call sys.path.append('<path_to>/workspace/') in every single main file.

Answers

When the python interpreter is importing a package, it looks for the package in the following locations:

the directory containing the input script (or the current directory).

PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

the installation-dependent default.

So if your package and script file are in the same directory, it will be easily found. But obviously you chose a different directory. Then you have to use the methods mentioned in your article to specify the path to the interpreter, such as modifying the PYTHONPATH environment variable or using the sys.path.append() method.

Here's a suggestion for another approach. Put your package where python can find it, say under the lib folder. This way you don't need to use the sys.path.append() method at the beginning of each file.

enter image description here

I am using a virtual environment

UPDATE:

You can use virtual environments. it won't make a difference. If you are worried about some factors and do not want to use it, there is no problem. Just find the lib folder in your current environment and put your package in it.

for example ( As far as my machine is concerned ) :

enter image description here

The lib folder corresponding to the interpreter of this environment is in:

C:\Users\Admin\AppData\Local\Programs\Python\Python310\Lib

enter image description here

The lib folder corresponding to the interpreter of this environment is in:

C:\Users\Admin\anaconda3\Lib

PS : It can also be placed in the site-packages folder one level below the lib folder.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐