简体   繁体   English

在我的虚拟环境中运行我的Python脚本?

[英]Running my Python script in my virtual env?

Total python noob here, I want to run my .py flask api file, i cd to my directory, activate my python virtual env, then i try run.py 总python noob在这里,我想运行我的.py flask api文件,我将其cd到我的目录,激活我的python虚拟env,然后我尝试运行.py

Issue is that it prints: 问题是它打印:

$ python run.py                                             

Traceback (most recent call last): File "run.py", line 1, in from flask_sqlalchemy import SQLAlchemy ImportError: No module named flask_sqlalchemy 追溯(最近一次调用):从flask_sqlalchemy导入SQLAlchemy ImportError,文件“ run.py”,第1行:没有名为flask_sqlalchemy的模块

As far as im aware as im in my virtualenv and have my packages installed, this shouldnt happen. 据我在我的virtualenv中即时消息并安装了我的软件包,这种情况不应该发生。 Trying to pip3 install on any packages shows them as already existing. 尝试在任何软件包上进行pip3 install都会显示它们已经存在。

To further assist with the issue, i tried to run the script inside pycharm and i get the following error prints in the log:`Traceback (most recent call last): 为了进一步解决该问题,我尝试在pycharm中运行该脚本,并在日志中显示以下错误打印信息:Traceback(最近一次调用最近):

  File "/Users/Jack/Developer/Python/lift_api/run.py", line 26, in <module>
    sqlAlchemy.create_all() 
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 1007, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 999, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 941, in get_engine
    return connector.get_engine()
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 543, in get_engine
    self._engine = rv = sqlalchemy.create_engine(info, **options)
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/engine/__init__.py", line 387, in create_engine
    return strategy.create(*args, **kwargs)
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 80, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 554, in dbapi
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'`

Anyone have an idea? 有人有主意吗?

要使用python 3.x运行代码,请使用python3命令。

python3 run.py

What is saying what it needs - psycopg2. 这说明需要什么-psycopg2。 If you are on Ubuntu it can help: 如果您使用的是Ubuntu,它可以帮助您:

sudo apt install libpq-dev python-dev

Then don't forget to install flask-sqlalchemy and other libraries while your virtualenv activated. 然后,不要忘了在激活virtualenv时安装flask-sqlalchemy和其他库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM