admin管理员组

文章数量:1312895

I'm on a Windows 10 host using VS Code and Python 3.12 with about 15 separate projects and 8 common library modules shared among them. Below is an outline of the directory structure I'm currently using. Each module folder has an init.py file with a list of the .py files in the folder.

Python
  |
  |- Common
  |    |
  |    |- module1
  |    |- module2
  |- Project1
  |- Project2

I have 2 questions:

  1. Is this the best approach to anizing my code? I'm relatively new to VS Code and Python.
  2. How is this even working? I have projects that have "from Common import module1 as m1" statements that actually work, but when I create a new project and try the same statement it report "No module named Common"

Relatively inexperienced here so would appreciate any guidance.

I'm on a Windows 10 host using VS Code and Python 3.12 with about 15 separate projects and 8 common library modules shared among them. Below is an outline of the directory structure I'm currently using. Each module folder has an init.py file with a list of the .py files in the folder.

Python
  |
  |- Common
  |    |
  |    |- module1
  |    |- module2
  |- Project1
  |- Project2

I have 2 questions:

  1. Is this the best approach to anizing my code? I'm relatively new to VS Code and Python.
  2. How is this even working? I have projects that have "from Common import module1 as m1" statements that actually work, but when I create a new project and try the same statement it report "No module named Common"

Relatively inexperienced here so would appreciate any guidance.

Share Improve this question asked Jan 31 at 20:45 WV_MapperWV_Mapper 3813 silver badges14 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 0

You might use .env, so you can defientively tell the locations of your imports. It is also commonly used when you need to share your code but you have either device specific or personal information like API keys.

First you need to get

pip install python-dotenv

Then you need to create a file .env isnide your project folder besides your main.py. So your structure becomes

Python
  |
  |- Common
  |    |
  |    |- module1
  |    |- module2
  |- Project1
  |    |
  |    |- .env
  |    |- main.py
  |- Project2

After you need to configure your .env file using a text editor ( including vs code)

PYTHONPATH=/path/to/Python/Common

This says your python interpreter to look at you common folder to find requested imports ensuring it will find. You can also do it as a relative path which is better when you planning to share your structures,

PYTHONPATH=../Common

Since this only needs my main.py to be inside of a folder with same directory as common, so you can zip and share. Absolute path needs to be corrected if you plan to share with your .env. But dont share your .env if it holds ciritical data like passkeys.

After this point VScode can autoload the .env file if you have the extension but if you call your script outside of vscode it might cause problems so to make scripts work even without vscode you need to update your main as

import dotenv 
dotenv.load_dotenv()
from Common import module1 as m1

You can see yo need to load env telling you python interpreterte tell where to look and then you can import modules.

As an extra you can put your api key inside .env, use secret_key = os.getenv('SECRET_KEY') inside your main.py and when you send your code to someone else they can use it with their .env and their secret key is used.

A quick and simple solution to project that cant see your common can be

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'Common')))

from Common import module1 as m1

Adding your common folder to path so your interpreter looks inside, __file__ telling current file .. says go one back and isnide common

本文标签: Windows directory structure for VS Code Python projectsStack Overflow