0

I have a Python app running on windows that has imports for the following packages:

requests json psycopg2

I copy the entire project (I used Pycharms to write the app and import the packages) to a new machine and expected it would work. The new machine is also windows and I'm trying to run my script from the command line (i.e. no Pycharm on the new machine).

Instead, I get an error saying "ModuleNotFoundError: No module named 'requests'"

If I look at the project, I have the directories:

  venv
    Lib
      site-packages
        requests

What am I missing/doing wrong?

Todd
  • 2,829
  • 5
  • 34
  • 56
  • You seem to use a virtual environment already. It might be best to learn to use them properly. A good tutorial can be found at http://docs.python-guide.org/en/latest/dev/virtualenvs/ – Klaus D. Dec 26 '17 at 01:58
  • It's useful to have a `requirements.txt` file that stashes libraries you are using in your project. That way, you can `pip install -r requirements.txt` when you move to a new environment/setup a virtual environment. – Quentin Dec 26 '17 at 02:27

2 Answers2

1

On the new machine you must source venv/bin/activate so your path environment variables are set properly. In particular, which python should say venv/bin/python rather than /usr/bin/python. Also, take care to conda env update or pip install -r requirements.txt so you'll have suitable venv libraries on the new machine.

J_H
  • 17,926
  • 4
  • 24
  • 44
1

You have a couple of options here but first the problem. You are exporting your code base to a new machine without the required Modules installed on that machine and/or within your Python project's environment. After you have python installed on your new machine, you need to be sure to point your PyCharm Project to the proper environment.

File > Default Preferences > Project Interpreter

The window that appears on the right will contain a drop down menu labeled Project Interpreter. If you click on the drop down, it should reveal a list of the available Python environments on your machine.

enter image description here

Based on your description of your site-packages directory I would assume you do not have your interpreter pointed the proper environment on your new machine. With that said, you would be better served creating a new virtual python environment on your machine and installing each relevant dependency within that environment.

Take a look at this post here for your first best option on re-creating your old python environment on your new machine.

EDIT: I apologize for not reading the question more thoroughly before answering the questions. If this is running on a Windows machine you will need to double check the environment path python is using. It is very easy to install python at a different PATH than the command line environment is checking on a Windows box. If for example your PATH is pointing to a different version of Python and PIP is installing packages somewhere else this issue can occur. Double check your System PATH for python and which version the command line is running.

  • Nicholas, the new machine won't have Pycharm running on it. I want to be able to run is from the command line. – Todd Dec 26 '17 at 04:45
  • 1
    Then insure python is installed on the machine, validate it is in your path, type ```python``` in your CMD line. If it loads python, check for PIP, then use ```pip install requests```, ```pip install json``` etc to install the package(s). Then execute the code by running ```python file.py```. – Nicholas Martinez Dec 26 '17 at 04:47