如何在命令行复制 PyCharm 是 运行 我的 Python 3.4 项目的方式?
How do I replicate the way PyCharm is running my Python 3.4 project at the command line?
我的项目是这样的:
running-pycharm-project-at-cmd
- main.py
- c
- run_project.py
- z
- __init__.py
- the_module.py
- y
- __init__.py
- template.md
- the_module_module.py
- the_support_function.py
.py文件内容如下:
main.py
from c.run_project import run
print('running main.py...')
run()
c/run_project.py
from c.z.the_module import the_module_function, the_module_write_function
def run():
print('Running run_project.py!')
the_module_function()
# write a file:
the_module_write_function(read_file='./z/y/template.md', write_file='../README.md')
if __name__ == '__main__':
run()
c/z/the_module.py
from c.z.y.the_module_module import the_module_module_function
def the_module_function():
print('the_module_function is running!')
the_module_module_function()
pass
def the_module_write_function(read_file, write_file):
with open(read_file, 'r') as fid:
with open(write_file, 'w') as fid_out:
contents = fid.read()
contents.replace('{}', 'THE-FINAL-PRODUCT!')
fid_out.write(contents)
c/z/y/the_module_module.py
from .the_support_function import this_support_data
def the_module_module_function():
print('The module module function is running!')
print("Here is support data: {}".format(this_support_data))
pass
c/z/y/the_support_function.py
this_support_data = "I am the support data!"
GitHub 使复制更容易的回购:running-pycharm-project-at-cmd
问题:在 Pycharm 中加载以 running-pycharm-project-at-cmd
作为根的项目。然后我右键单击 运行 run_project.py 文件。一切正常。我不知道如何以相同的方式从命令行 运行 run_project.py
。创建main.py
是,但由于模板文件的相对引用而失败(在实际项目中,y
文件夹是一个git子模块,因此不能绝对其中的参考文献)。
如果您将 main.py 文件复制到 c
文件夹并将其重命名为 __init__.py
,那么您可以 运行:
$ python -m c
-m
参数告诉 python 到 运行 模块或包(在本例中为 c
)。 Python 将在 c
的文件夹中查找 __init__.py
文件和 运行 文件。您只需要确保文件夹 c 在您的 PYTHONPATH 中或者是当前工作目录的子文件夹即可。
我又回到了这里,并且能够让它工作。请注意,我使用的是 Windows 7,其中一些可能取决于平台。为了在不更改任何代码的情况下使其正常工作,关键是在 运行ning 之前设置 PYTHONPATH 环境变量。 PyCharm 自动执行此操作(默认情况下,但可在 运行 配置选项中配置)。
步骤:
重现问题:
- 将 github 存储库克隆到 G:\TEST。
- 导航到 G:\TEST\running-pycharm-project-at-cmd-master\c
python run_project.py
Traceback (most recent call last):
File "run_project.py", line 1, in <module>
from c.z.the_module import the_module_function, the_module_write_function
ImportError: No module named 'c'
最简单的解决方案(如果能够从脚本位置 运行 Python):
- 在 运行ning 之前设置 PYTHONPATH 环境变量。例如
SET PYTHONPATH=G:\TEST\running-pycharm-project-at-cmd-master
- 现在
python run_project.py
Running run_project.py!
the_module_function is running!
The module module function is running!
Here is support data: I am the support data!
从远程位置到 运行(这绝对是 Windows 特定的):
- 创建一个 .bat 文件,在 运行 宁 Python 之前导航到正确的工作目录。即:
START cmd /K "cd G:\TEST\running-pycharm-project-at-cmd-master\c & python run_project.py"
G:\TEST\myotherlocation
run_it.bat
Running run_project.py!
the_module_function is running!
The module module function is running!
Here is support data: I am the support data!
我的项目是这样的:
running-pycharm-project-at-cmd
- main.py
- c
- run_project.py
- z
- __init__.py
- the_module.py
- y
- __init__.py
- template.md
- the_module_module.py
- the_support_function.py
.py文件内容如下:
main.py
from c.run_project import run
print('running main.py...')
run()
c/run_project.py
from c.z.the_module import the_module_function, the_module_write_function
def run():
print('Running run_project.py!')
the_module_function()
# write a file:
the_module_write_function(read_file='./z/y/template.md', write_file='../README.md')
if __name__ == '__main__':
run()
c/z/the_module.py
from c.z.y.the_module_module import the_module_module_function
def the_module_function():
print('the_module_function is running!')
the_module_module_function()
pass
def the_module_write_function(read_file, write_file):
with open(read_file, 'r') as fid:
with open(write_file, 'w') as fid_out:
contents = fid.read()
contents.replace('{}', 'THE-FINAL-PRODUCT!')
fid_out.write(contents)
c/z/y/the_module_module.py
from .the_support_function import this_support_data
def the_module_module_function():
print('The module module function is running!')
print("Here is support data: {}".format(this_support_data))
pass
c/z/y/the_support_function.py
this_support_data = "I am the support data!"
GitHub 使复制更容易的回购:running-pycharm-project-at-cmd
问题:在 Pycharm 中加载以 running-pycharm-project-at-cmd
作为根的项目。然后我右键单击 运行 run_project.py 文件。一切正常。我不知道如何以相同的方式从命令行 运行 run_project.py
。创建main.py
是y
文件夹是一个git子模块,因此不能绝对其中的参考文献)。
如果您将 main.py 文件复制到 c
文件夹并将其重命名为 __init__.py
,那么您可以 运行:
$ python -m c
-m
参数告诉 python 到 运行 模块或包(在本例中为 c
)。 Python 将在 c
的文件夹中查找 __init__.py
文件和 运行 文件。您只需要确保文件夹 c 在您的 PYTHONPATH 中或者是当前工作目录的子文件夹即可。
我又回到了这里,并且能够让它工作。请注意,我使用的是 Windows 7,其中一些可能取决于平台。为了在不更改任何代码的情况下使其正常工作,关键是在 运行ning 之前设置 PYTHONPATH 环境变量。 PyCharm 自动执行此操作(默认情况下,但可在 运行 配置选项中配置)。
步骤:
重现问题:
- 将 github 存储库克隆到 G:\TEST。
- 导航到 G:\TEST\running-pycharm-project-at-cmd-master\c
python run_project.py
Traceback (most recent call last):
File "run_project.py", line 1, in <module>
from c.z.the_module import the_module_function, the_module_write_function
ImportError: No module named 'c'
最简单的解决方案(如果能够从脚本位置 运行 Python):
- 在 运行ning 之前设置 PYTHONPATH 环境变量。例如
SET PYTHONPATH=G:\TEST\running-pycharm-project-at-cmd-master
- 现在
python run_project.py
Running run_project.py!
the_module_function is running!
The module module function is running!
Here is support data: I am the support data!
从远程位置到 运行(这绝对是 Windows 特定的):
- 创建一个 .bat 文件,在 运行 宁 Python 之前导航到正确的工作目录。即:
START cmd /K "cd G:\TEST\running-pycharm-project-at-cmd-master\c & python run_project.py"
G:\TEST\myotherlocation
run_it.bat
Running run_project.py!
the_module_function is running!
The module module function is running!
Here is support data: I am the support data!