English 中文(简体)
“市场名录作为源头”确实做了些什么?
原标题:What does "Mark directory as sources root" really do?

在Pycharm,如果你在项目内右侧点击,你可以将其标记为 sources,这样你就可以从这个夹和子里进口模块。

然而,如果我试图从Pycharm外(例如从Osole)处执行,那么,这样做只会使你的方案在Pycharm内得以实施,那么我会抱怨某些单元没有找到,这就是Im所面临的问题。

If I mark a certain folder as sources root my program runs fine, but I need to understand what does it do so I can configure the program to find this modules even if not using Pycharm.

我想知道这一选择究竟做了些什么,我如何能够在不使用这一选择的情况下获得同样的行为。

It is just adding a __init__.py file in the root folder? Is it doing something like:

import sys
sys.path.insert(0, my_folder)
最佳回答

First __init__.py marks a directory as a regular package directory(this is pre 3.3, in 3.3+ its not required anymore). With this, python will look for submodules inside that directory for imports.

"Mark directory as sources root" sets the PATH(or PYTHONPATH) for the environment. In shell, it will be like,

export PYTHONPATH="${PYTHONPATH}:/your/source/root"

PYTHONPATH holds the default search path for module files. This will enable the interpreter to search for the modules in the appended path. The default value is installation dependent(normally it contains path to Python binaries).

您还可以使用<条码>sys.path<>条码/代码>,在笔迹内操纵这一搜索途径。

问题回答

After struggling with PyCharm s Mark as project root I found a solution. Keep in mind that I m doing this from a Windows 11 machine.

www.un.org/spanish/ecosoc 答复: 基本上,“市场名录作为源头”在<代码>上添加了贵国项目(<代码>src 倍]。 缩略语

One can easily check this by doing the following:

  1. On a terminal (outside PyCharm - I did this on Windows 11 CMD) navigate to your project root (the same place where you have the src folder) and activate the virtual environment;
  2. With the virtual environment activated execute the command python -c "import sys; print( .join(sys.path))" to make the terminal print all the values of the sys.path variable, the equivalent of the PYTHONPATH;
  3. You will notice that the root of your project isn t listed there - and this is what Mark as project root does, it adds your project root to the sys.path values;
  4. You can replicate what PyCharm does by then executing the command: set PYTHONPATH=%PYTHONPATH%;C:\--PATH--\--TO--\--PROJECT ROOT--; and this command will add your project s root to the PYTHONPATH variable. (executing the command python -c... again will show it);
  5. Finallly, if you now execute whatever command you executed inside PyCharm that worked, will also work from the terminal;




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签