PTVS + Django: why can i not import python libraries - python

i am following the tutorial found here: http://www.windowsazure.com/en-us/documentation/articles/cloud-services-web-sites-python-django-app-with-ptvs/
I already had django installed on my system. After following the instructions in this tutorial, i noticed that it seemed to have installed django again, but this time under my project's folder (myproject/env/Lib/site-packages).
Why is this?
The other issue i am having is that if i try to import certain libraries using the visual studio editor, it's unable to find the import. However, if do this in the Interactive window, it does recognize the imports. Also, if create a stand-along python (not django) project, i am able to import the libraries fine.
Any thoughts on why this is happening and how to resolve?

The tutorial uses Python virtual environments (virtualenv), which you need in order to deploy your website to Azure. A virtual environment, basically, is an isolated Python environment with its own set of libraries (site-packages etc) that is distinct from your main interpreter. It allows you to have specific versions of packages that you need just for that particular website, and different from the same for some other website of yours.
If you don't plan to deploy it to Azure, you don't need a virtual env - you can skip the step creating it, and instead install Django and other packages directly into your main interpreter. If you do plan to deploy to Azure, you need the virtual env, because the main interpreter installed on the Azure VM will not have any modules other than those in the standard library.
The reason why Python Interactive window lets you import the packages is that the instance you're working with is for your main interpreter. Every registered interpreter, and every virtual env in an opened project, has its own separate Python Interactive window. You can open the window for your virtual env from the Python Environments window, which is available via Tools -> Python Tools -> Python Environments.

Related

Python in VSCode can't find module even changed to a right environment

I'm using VSCode for python programming. The interpreters are from Anaconda (Python 3.7 in the latest version of Anaconda). I've added the Anaconda folder path into the system environment variables:
I've installed packages numpy and numba via conda, and I created an environment which contains those packages:
And the current VSCode is working under that environment:
It works fine in PyCharm by using the same interpreter/environment, but
when I switched to VSCode it always gets me error: ImportError: No module named numba. I don't know how to fix that.
You don’t have to manually add environment in VS Code. Remove all the path you have manually added. Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
Search for Anaconda extension. Install and refresh your VS Code. Open your project folder and click on of your .py file. VS Code will search all available Pythons in your system.
On the bottom right you should be able to select a correct environments

How to run python production on customer environment

I have some python application that should run on customer site. I compile my py files to pyc (python byte code).
What is the standard way to run the app on the customer environment? The options I see are:
As part of my installer, install some python distribution, i.e Anaconda.
Require the customer to have python installed in their environment.
Bring python libraries and executable along with my code and run it directly from my installation dir.
Convert the scripts to exe using some py-to-exe tool.
Application usage: The app is used as a tool to calculate statistics for my main product. The customer won't run it explicitly. It won't have any GUI.
Customer environment will be x64 Windows machine. No other restrictions.
Any recommendations or comments? I couldn't find such discussions on the web.
Given your requirements, the last two options seem most viable:
Bring python libraries and executable along with my code and run it directly from my installation dir.
Convert the scripts to exe using some py-to-exe tool.
You can either package your code, or freeze your code and create an executable for the target OS.
Given your requirements, I'd suggest the latter. You can go through the Python Packaging Guide for more details regarding packaging.

Python Tools for Visual Studio & virtualenv - can use packages only present in deactivated env

I'm working on a Django project in VS 2015 Community with PTVS, which has been very useful for a free tool. I recently realized that I should be using virtual environments during development, and found out that Python 3 includes this feature by default, and PTVS 2.0+ supports it- cool!
I created a couple environments as an experiment, and in one I installed the celery[redis] bundle since I'm trying to figure out how to implement a background task. I was having trouble getting the basic celery tutorial task to work, so I decided to remove the environment from my Django project, deactivate it, and start over.
However, once I removed it via PTVS and ran deactivate from the command line inside the environment directory, I could still run celery commands from my top-level project directory. I've never installed celery globally- only to my test environment via the Python Environments menu in PTVS.
Why is this? Am I thinking of virtual environments as too similar to truly discrete environments such as containers? My impression from reading PTVS venv documentation is that if a package is present in a virtual environment which is then deactivated, I shouldn't be able to use it in other environments (or globally). I thought it might be an issue with my Windows PATH, but I didn't see anything related to Python or celery.
Apologies if this is a duplicate- it's been difficult to find questions on the PTVS implementation of venv rather than Python + virtualenv in general.

IntelliJ IDEA 14 does not recognise Python built-in types when using interpreter in virtualenv

I'm using IntelliJ IDEA 14 on OSX for a Python project that uses a virtual environment. The program runs fine both in the terminal and using the run command inside IDEA, but it is not recognising Python built in types and functions.
I created the virtual environment and added it to my Project Structure following the steps below, which according to posts I read, should work, but I cannot figure out what is going wrong.
Added Python in venv as the Project SDK.
Added Python in venv as Python interpreter in Modules.
Added Python in venv as the Python interpreter in Facets.
Ensured that the appropriate Python interpreter is the selected SDK.
These are the global libraries.
[Update]
Following one of the comments below, I recreated the virtual environment from scratch. The built-in types for Python are now recognised, but the libraries in the venv folder are not.
You need to put the Python 2.7.x interpreter library in the dependencies of your python module. Having the python SDK is not enough.

Importing and debugging a Python Django project made in a different environment

I am working on a Django project that was created by another developer on a different machine. I see that in the root of the application, there is a .virtualenv directory. Is it possible to simply setup this project locally on my Windows machine using the project settings and Python version (the app uses 2.7), so that I can run it like a local Django application so debugging is feasible?
I have access to the development web server and have copied the full source of the app down to my Win7 machine but cannot seem to get things setup correctly to run the app locally so I can debug.
I currently have Python 2.7, 2.7.5 and 3.3.2 installed on my local dev machine. I would call myself pretty new to Django and Virtualenv.
If anyone has any guidance on how I can get my environment straitened out so I can run the app with debugging, I would be very thankful.
Thank you in advance.
Using a virtualenv environment created on a different machine is not recommended. There are things hard-wired for the particular system it was created on, and some apps may have components compiled for that particular system.
You should create a new virtualenv environment on your machine, install dependencies and move the Django project there.
Note on installing dependencies - there might be a file named requirements.txt somewhere. If it's there and it's kept up to date you can install all the dependencies by running a single command while in your virtualenv:
pip -r requirements.txt install
If you can't find it ask the other developer to create it. He just need to do this inside his own environment:
pip freeze > requirements.txt
I once faced the same problem and it took me so much time to configure another environment that I eventually had to create a VM with the same version of OS and libraries. I then made a raw copy of the project and it worked fine.

Categories