如何每天运行python脚本

时间:2020-06-22 06:51:51

标签: python spyder

每天早上,我都会在python上生成一系列图表。为此,我:

  1. 打开Spyder
  2. 运行我的“ packages”脚本(packages.py),该脚本指示python导入所有必需的模块(例如datetime)
  3. 运行我的图表脚本(Good Morning.py),该脚本创建图表并以PDF格式将其打印在文件夹中。此步骤的进一步复杂之处在于,我无法像在步骤2中那样在Spyder中命中“运行文件”。由于某些原因,Python无法识别我已经导入了模块,并且收到消息“ NameError:name'date “未定义”(可能是因为.py文件位于不同的文件夹中)。因此,我必须突出显示此“ Good Morning.py”文件中的代码行,然后运行有效的选择行或当前行(在Mac上为Fn + F9)。

我是否可以自动执行此任务并使它每天运行而无需执行步骤1?

3 个答案:

答案 0 :(得分:1)

首先创建Exe以避免第一步,并尝试使用Windows Scheduler对其进行调度以每天运行。

**import 'package:galon_app/PlugIn/User.dart';
import 'package:galon_app/pages/maps.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:galon_app/pages/createUser.dart';
import 'NotifOrder.dart';
import 'OnGoingOrder.dart';
import 'Profile.dart';

final googleSignIn = GoogleSignIn();
final StorageReference storageRef = FirebaseStorage.instance.ref();
final userRef = Firestore.instance.collection('users');
final itemnRef = Firestore.instance.collection('item');
final priceRef = Firestore.instance.collection('prices');
User currentUser;

class Home extends StatefulWidget {

  final int pageIndex;

  Home({
    this.pageIndex
  });

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  bool isAuth = false;
  PageController pageController;
  int pageIndex = 0;

  TextEditingController usernmae = TextEditingController();
  TextEditingController password = TextEditingController();


@override
void initState() {
  // TODO: implement initState
  super.initState();
  setPageIndex();
  pageController = PageController(initialPage: pageIndex, keepPage: true, viewportFraction: 1);

  //Checking is User Signed in
  googleSignIn.onCurrentUserChanged.listen((account){
    handleSignIn(account);
  },onError: (err){
    print('Error singin: $err');
  }
  );

  //Reauthenticate user when app is opened
    googleSignIn.signInSilently(suppressErrors: false).then((account){
      handleSignIn(account);
    }).catchError((err){
      print('Error Signin: $err');
    });
  
}
//Set page index
setPageIndex(){
  if(widget.pageIndex != null){
      pageIndex = widget.pageIndex;
      onPageChanged(pageIndex);
  }else{
      pageIndex = 0;
  }
}

//Login Handle
login(){
  googleSignIn.signIn();
}

logout(){
  googleSignIn.signOut();
}

handleSignIn(GoogleSignInAccount account){
  if(account != null){
    setState(() {
      isAuth = true;
    });
  }else{
    setState(() {
      isAuth = false;
    });
  }
}

//Home Screen Handle 
  @override
void dispose() { 
  pageController.dispose();
  super.dispose();
}

//Set on page changed
onPageChanged(int pageIndex){
  setState(() {
    this.pageIndex = pageIndex;
  });
}

//Animated to next page when taped
onTap(int pageIndex){
  pageController.animateToPage(
    pageIndex,
    duration: Duration(milliseconds: 200),
    curve: Curves.easeInOut
  );
}
//Build UnAuthScreen(If user not logged in or havent sign up yet)
Scaffold buildUnAuthScreen(){
 return Scaffold(
   body: Container(
     alignment: Alignment.center,
    decoration: BoxDecoration(
      gradient: LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
        colors: [
          Theme.of(context).accentColor,
          Theme.of(context).primaryColor,
        ]
      )
     ),child: Column(
       crossAxisAlignment: CrossAxisAlignment.center,
       mainAxisAlignment: MainAxisAlignment.center,
       children: <Widget>[
         SizedBox(height: 150),
         Container(
           height: 150,
           width: 150,
            alignment: Alignment.center,
            child: Image.asset('assets/galonku_Logo.png'),
         ),
         SizedBox(height:500),
         GestureDetector(
            onTap: login,
            child: Container(
              width: 260,
              height: 60,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/googleSignIn_button.png'),
                  fit: BoxFit.cover
                )
              )
            )
          )
       ],
     ),
   )
 ); 
}

//Build AuthScreen (If user already loggedin)
Scaffold buildAuthScreen(){
  return Scaffold(
    body: PageView(
      children: <Widget>[
        Maps(),
        //OnGoingOrder(),
        NotifOrder(),
        Profile(), 
      ],
      controller: pageController ,
      onPageChanged: onPageChanged,
      physics: NeverScrollableScrollPhysics(),
    ),
    bottomNavigationBar: CupertinoTabBar(
      currentIndex: pageIndex,
      onTap: onTap,
      activeColor: Colors.grey[100],
      inactiveColor: Colors.grey[700],
      backgroundColor: Theme.of(context).primaryColor,
      items: [
        BottomNavigationBarItem(icon: Icon(Icons.shopping_cart)),
        BottomNavigationBarItem(icon: Icon(Icons.notifications)),
        BottomNavigationBarItem(icon: Icon(Icons.person))
      ],
    )
  );
}  

@override
Widget build(BuildContext context) {
  return isAuth? buildAuthScreen() :buildUnAuthScreen();
}
}**

答案 1 :(得分:0)

您需要创建一个安装了所有依赖项的环境,然后才能通过CronJob运行脚本。 enter link description here

对于Unix:

Blockquote

对于Windows:

Cron job on windows

虚拟环境:

creating virtual environment

虚拟环境中的

CronJob: running cron through virtual env

答案 2 :(得分:0)

只做一件事!! 您可以每天早上使用AWS并编写脚本来运行代码(您不会在AWS中遇到这些导入错误)。 每天您只需要打开AWS并下载PDF。